Skip to content

Commit d97ed8b

Browse files
committed
修复插件BUG:
1.修复GenerateO2O组件不能生成父类中字段代码的问题 2.优化插件使用体验
1 parent c78a0be commit d97ed8b

File tree

8 files changed

+164
-49
lines changed

8 files changed

+164
-49
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# object-helper-plugin
2+
![](https://img.shields.io/badge/version-v1.0.2-blue)
3+
![](https://img.shields.io/badge/license-Apache%202-red)
4+
![](https://img.shields.io/badge/download-100%2B-green)
5+
26
JetBrains Intellij IDEA Obejct辅助插件,包含以下功能:
37

48
- 对象拷贝

object-helper.jar

1.28 KB
Binary file not shown.

resources/META-INF/plugin.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>cn.bigcoder.plugin.objecthelper</id>
33
<name>ObjectHelper</name>
4-
<version>1.0.2</version>
4+
<version>1.0.3</version>
55
<vendor email="bigcoder84@gmail.com" url="https://github.com/tianjindong/object-helper-plugin">HearingSmile</vendor>
66

77
<description><![CDATA[
@@ -11,8 +11,7 @@
1111
]]></description>
1212

1313
<change-notes><![CDATA[
14-
<li>Fix bug:Fixed location visibility of GenerateO2O components outside of methods</li>
15-
<li>Add logo</li>
14+
<li>Fix bug:Fixed parent field not being generated</li>
1615
]]>
1716
</change-notes>
1817

@@ -29,7 +28,7 @@
2928
</extensions>
3029

3130
<actions>
32-
<action id="ObjectHelper" class="cn.bigcoder.plugin.objecthelper.component.GenerateO2O" text="GenerateO2O">
31+
<action id="cn.bigcoder.plugin.objecthelper.action.GenerateO2O" class="cn.bigcoder.plugin.objecthelper.action.GenerateO2O" text="GenerateO2O">
3332
<add-to-group group-id="GenerateGroup" anchor="last"/>
3433
</action>
3534
</actions>
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
package cn.bigcoder.plugin.objecthelper.component;
1+
package cn.bigcoder.plugin.objecthelper.action;
22

33
import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils;
4+
import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
45
import cn.bigcoder.plugin.objecthelper.generator.Generator;
56
import cn.bigcoder.plugin.objecthelper.generator.method.ObjectCopyMethodGenerator;
67
import com.intellij.openapi.actionSystem.AnAction;
78
import com.intellij.openapi.actionSystem.AnActionEvent;
89
import com.intellij.openapi.command.WriteCommandAction;
910
import com.intellij.psi.JavaPsiFacade;
10-
import com.intellij.psi.PsiElement;
1111
import com.intellij.psi.PsiElementFactory;
1212
import com.intellij.psi.PsiMethod;
13-
import com.intellij.psi.util.PsiTreeUtil;
1413
import org.jetbrains.annotations.NotNull;
1514

15+
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.*;
16+
1617
public class GenerateO2O extends AnAction {
1718

1819
@Override
1920
public void actionPerformed(AnActionEvent anActionEvent) {
2021
WriteCommandAction.runWriteCommandAction(anActionEvent.getProject(), () -> {
21-
generateO2O(getPsiMethodFromContext(anActionEvent));
22+
generateO2O(PsiUtils.getCursorPsiMethod(anActionEvent));
2223
});
2324
}
2425

2526
@Override
2627
public void update(@NotNull AnActionEvent anActionEvent) {
2728
// 如果当前光标不在方法中,则不显示GernerateO2O组件
28-
if (getPsiMethodFromContext(anActionEvent) == null) {
29-
anActionEvent.getPresentation().setEnabled(false);
29+
if (!check(PsiUtils.getCursorPsiMethod(anActionEvent))) {
30+
PsiUtils.setActionDisabled(anActionEvent);
3031
}
3132
super.update(anActionEvent);
3233
}
@@ -37,17 +38,31 @@ private void generateO2O(PsiMethod psiMethod) {
3738
}
3839
// 初始化生成器
3940
Generator generator = ObjectCopyMethodGenerator.getInstance(psiMethod);
41+
String methodCode = generator.generate();
42+
if (StringUtils.isEmpty(methodCode)) {
43+
return;
44+
}
4045
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiMethod.getProject());
4146
// 生成新的PsiMethod
4247
PsiMethod toMethod = elementFactory.createMethodFromText(generator.generate(), psiMethod);
4348
psiMethod.replace(toMethod);
4449
}
4550

46-
private PsiMethod getPsiMethodFromContext(AnActionEvent e) {
47-
PsiElement elementAt = PsiUtils.getCursorPsiElement(e);
48-
if (elementAt == null) {
49-
return null;
51+
/**
52+
* 检查当前光标
53+
* 1. 是否在方法中
54+
* 2. 方法是否有入参
55+
* 3. 方法是否有返回值
56+
*
57+
* @param psiMethod
58+
* @return
59+
*/
60+
private boolean check(PsiMethod psiMethod) {
61+
if (psiMethod == null
62+
|| PsiUtils.getPsiParameters(psiMethod).size() == 0
63+
|| VOID.equals(PsiUtils.getMethodReturnClassName(psiMethod))) {
64+
return false;
5065
}
51-
return PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
66+
return true;
5267
}
5368
}

src/cn/bigcoder/plugin/objecthelper/common/constant/JavaSeparator.java renamed to src/cn/bigcoder/plugin/objecthelper/common/constant/JavaKeyWord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author: Jindong.Tian
55
* @date: 2021-01-31
66
**/
7-
public class JavaSeparator {
7+
public class JavaKeyWord {
88
public static final String EMPTY_BODY = "";
99
public static final String BLANK_SEPARATOR = " ";
1010
public static final String LINE_SEPARATOR = "\n";

src/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cn.bigcoder.plugin.objecthelper.common.util;
22

3-
import cn.bigcoder.plugin.objecthelper.common.constant.JavaSeparator;
3+
import cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord;
44
import cn.bigcoder.plugin.objecthelper.common.enums.JavaModify;
55
import com.intellij.openapi.actionSystem.AnActionEvent;
66
import com.intellij.openapi.actionSystem.LangDataKeys;
@@ -9,6 +9,8 @@
99
import com.intellij.openapi.project.Project;
1010
import com.intellij.psi.*;
1111
import com.intellij.psi.search.GlobalSearchScope;
12+
import com.intellij.psi.util.PsiTreeUtil;
13+
import org.apache.commons.compress.utils.Lists;
1214
import org.jetbrains.annotations.NotNull;
1315

1416
import java.util.ArrayList;
@@ -21,8 +23,22 @@
2123
**/
2224
public class PsiUtils {
2325

26+
/**
27+
* 获取光标处上下文的{@code PsiMethod}
28+
* @param actionEvent
29+
* @return
30+
*/
31+
public static PsiMethod getCursorPsiMethod(AnActionEvent actionEvent) {
32+
PsiElement elementAt = PsiUtils.getCursorPsiElement(actionEvent);
33+
if (elementAt == null) {
34+
return null;
35+
}
36+
return PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
37+
}
38+
2439
/**
2540
* 获取光标所在处的{@code PsiElement}
41+
*
2642
* @param anActionEvent
2743
* @return
2844
*/
@@ -38,6 +54,14 @@ public static PsiElement getCursorPsiElement(AnActionEvent anActionEvent) {
3854
return psiFile.findElementAt(offset);
3955
}
4056

57+
/**
58+
* 设置当前组件不可用(不显示)
59+
* @param anActionEvent
60+
*/
61+
public static void setActionDisabled(AnActionEvent anActionEvent){
62+
anActionEvent.getPresentation().setEnabled(false);
63+
}
64+
4165
/**
4266
* 根据{@code PsiType}获取对应的{@code PsiClass}
4367
*
@@ -58,6 +82,7 @@ public static PsiClass getPsiClass(PsiType psiType, Project project) {
5882

5983
/**
6084
* 获取方法名称
85+
*
6186
* @param psiMethod
6287
* @return
6388
*/
@@ -70,25 +95,27 @@ public static String getMethodName(PsiMethod psiMethod) {
7095

7196
/**
7297
* 获取方法返回名称
98+
*
7399
* @param psiMethod
74100
* @return
75101
*/
76102
@NotNull
77103
public static String getMethodReturnClassName(PsiMethod psiMethod) {
78104
PsiType returnType = psiMethod.getReturnType();
79105
if (returnType == null) {
80-
return JavaSeparator.VOID;
106+
return JavaKeyWord.VOID;
81107
}
82108
return returnType.getPresentableText();
83109
}
84110

85111
/**
86112
* 获取方法的参数列表
113+
*
87114
* @param psiMethod
88115
* @return
89116
*/
90117
@NotNull
91-
public static List<PsiParameter> getPsiParameters(PsiMethod psiMethod){
118+
public static List<PsiParameter> getPsiParameters(PsiMethod psiMethod) {
92119
return Arrays.asList(psiMethod.getParameterList().getParameters());
93120
}
94121

@@ -116,4 +143,30 @@ public static List<JavaModify> getMethodModifies(PsiModifierList modifierList) {
116143
}
117144
return result;
118145
}
146+
147+
/**
148+
* 获取{@code PsiClass}所有字段(包含父类字段)
149+
*
150+
* @param psiClass
151+
* @return
152+
*/
153+
@NotNull
154+
public static List<PsiField> getAllPsiFields(PsiClass psiClass) {
155+
List<PsiField> result = Lists.newArrayList();
156+
recursiveAllFields(psiClass, result);
157+
return result;
158+
}
159+
160+
/**
161+
* 递归获取类中所有字段
162+
* @param psiClass
163+
* @param psiFields
164+
*/
165+
private static void recursiveAllFields(PsiClass psiClass, List<PsiField> psiFields) {
166+
if (psiClass == null) {
167+
return;
168+
}
169+
psiFields.addAll(Arrays.asList(psiClass.getFields()));
170+
recursiveAllFields(psiClass.getSuperClass(), psiFields);
171+
}
119172
}

src/cn/bigcoder/plugin/objecthelper/generator/method/AbstractMethodGenerator.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import com.intellij.openapi.project.Project;
77
import com.intellij.psi.PsiMethod;
88
import com.intellij.psi.PsiParameter;
9+
import org.apache.commons.collections.CollectionUtils;
910

1011
import java.util.List;
1112

12-
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaSeparator.*;
13+
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.*;
1314
import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.*;
1415

1516
/**
@@ -24,14 +25,15 @@ public abstract class AbstractMethodGenerator implements Generator {
2425
protected PsiMethod psiMethod;
2526

2627
public String generate() {
28+
if (!check()) {
29+
return null;
30+
}
2731
StringBuilder result = generateMethodFirstLine()
2832
.append(generateMethodBody())
2933
.append("}");
3034
return result.toString();
3135
}
3236

33-
abstract String generateMethodBody();
34-
3537
protected StringBuilder generateMethodFirstLine() {
3638
StringBuilder builder = new StringBuilder();
3739
PsiUtils.getMethodModifies(psiMethod.getModifierList()).forEach(e -> builder.append(e.getName()).append(BLANK_SEPARATOR));
@@ -43,6 +45,24 @@ protected StringBuilder generateMethodFirstLine() {
4345
return builder;
4446
}
4547

48+
/**
49+
* 生成方法体
50+
* @return
51+
*/
52+
abstract String generateMethodBody();
53+
54+
/**
55+
* 检查是否具备生成方法所需要的环境
56+
*
57+
* @return true代表校验通过
58+
*/
59+
protected boolean check() {
60+
if (CollectionUtils.isEmpty(getParameters()) || VOID.equals(getMethodReturnClassName(psiMethod))) {
61+
return false;
62+
}
63+
return true;
64+
}
65+
4666
protected List<PsiParameter> getParameters() {
4767
return getPsiParameters(psiMethod);
4868
}

0 commit comments

Comments
 (0)