Skip to content

Commit 3e23613

Browse files
committed
修复插件BUG:
1.修复GenerateO2O组件在异常场景下使用的BUG
1 parent 125744b commit 3e23613

File tree

8 files changed

+93
-47
lines changed

8 files changed

+93
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ JetBrains Intellij IDEA Obejct辅助插件,包含以下功能:
33

44
- 对象拷贝
55

6-
![](./images/1.gif)
6+
![](https://image.bigcoder.cn/6d6af6fd-255c-4d32-83bb-85b39280460d.gif)
77

88
-

images/1.gif

-77.6 KB
Binary file not shown.

object-helper.jar

282 Bytes
Binary file not shown.

resources/META-INF/plugin.xml

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

77
<description><![CDATA[
8-
这是一个Java对象工具集
9-
<li>对象拷贝</li>
8+
This is a Java object toolset
9+
<li>Copy the object</li>
10+
<img style="width: 400px" src="https://image.bigcoder.cn/6d6af6fd-255c-4d32-83bb-85b39280460d.gif"></img>
1011
]]></description>
1112

1213
<change-notes><![CDATA[
13-
init
14+
<li>Fix bug:fixed bugs in exception scenarios.Improve plugin compatibility.</li>
1415
]]>
1516
</change-notes>
1617

1718
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
18-
<idea-version since-build="173.0"/>
19+
<idea-version since-build="182.0"/>
1920

2021
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
2122
on how to target different products -->

resources/META-INF/pluginIcon.svg

Lines changed: 3 additions & 0 deletions
Loading

src/cn/bigcoder/plugin/objecthelper/GenerateO2O.java renamed to src/cn/bigcoder/plugin/objecthelper/component/GenerateO2O.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@
88
import com.intellij.openapi.actionSystem.PlatformDataKeys;
99
import com.intellij.openapi.command.WriteCommandAction;
1010
import com.intellij.openapi.editor.Editor;
11+
import com.intellij.openapi.project.Project;
1112
import com.intellij.psi.*;
1213
import com.intellij.psi.util.PsiTreeUtil;
1314

1415
public class GenerateO2O extends AnAction {
1516

1617
@Override
17-
public void actionPerformed(AnActionEvent e) {
18-
PsiMethod method = getPsiMethodFromContext(e);
19-
generateO2OMethod(method);
20-
}
21-
22-
/**
23-
* 启动写线程
24-
*
25-
* @param psiMethod
26-
*/
27-
private void generateO2OMethod(PsiMethod psiMethod) {
28-
new WriteCommandAction.Simple(psiMethod.getProject(), psiMethod.getContainingFile()) {
29-
@Override
30-
protected void run() throws Throwable {
31-
generateO2O(psiMethod);
32-
}
33-
}.execute();
18+
public void actionPerformed(AnActionEvent anActionEvent) {
19+
WriteCommandAction.runWriteCommandAction(anActionEvent.getProject(), () -> {
20+
generateO2O(getPsiMethodFromContext(anActionEvent));
21+
});
3422
}
3523

3624
private void generateO2O(PsiMethod psiMethod) {
25+
if (psiMethod == null) {
26+
return;
27+
}
28+
// 初始化生成器
3729
Generator generator = ObjectCopyMethodGenerator.getInstance(psiMethod);
3830
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiMethod.getProject());
31+
// 生成新的PsiMethod
3932
PsiMethod toMethod = elementFactory.createMethodFromText(generator.generate(), psiMethod);
4033
psiMethod.replace(toMethod);
4134
}
@@ -55,7 +48,7 @@ private PsiElement getPsiElement(AnActionEvent e) {
5548
e.getPresentation().setEnabled(false);
5649
return null;
5750
}
58-
//用来获取当前光标处的PsiElement
51+
//获取当前光标处的PsiElement
5952
int offset = editor.getCaretModel().getOffset();
6053
return psiFile.findElementAt(offset);
6154
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
**/
1616
public abstract class AbstractMethodGenerator implements Generator {
1717

18+
protected static final int FIRST_INDEX = 0;
19+
protected static final String EMPTY_BODY = "";
20+
protected static final String BLANK_SEPARATOR = " ";
21+
protected static final String LINE_SEPARATOR = "\n";
22+
protected static final String COMMA_SEPARATOR = ",";
23+
24+
protected static final String VOID_KEYWORD = "void";
25+
1826
/**
1927
* 方法修饰符
2028
*/
@@ -40,18 +48,18 @@ public abstract class AbstractMethodGenerator implements Generator {
4048
public String generate() {
4149
StringBuilder result = generateMethodFirstLine()
4250
.append(generateMethodBody())
43-
.append("}\n");
51+
.append("}");
4452
return result.toString();
4553
}
4654

4755
protected StringBuilder generateMethodFirstLine() {
4856
StringBuilder builder = new StringBuilder();
49-
methodModifies.forEach(e -> builder.append(e.getName() + " "));
50-
builder.append(returnClassName + " ")
57+
methodModifies.forEach(e -> builder.append(e.getName()).append(BLANK_SEPARATOR));
58+
builder.append(returnClassName + BLANK_SEPARATOR)
5159
.append(methodName)
5260
.append("(")
53-
.append(StringUtils.join(parameters, PsiParameter::getText, " ,"))
54-
.append(") {\n");
61+
.append(StringUtils.join(parameters, PsiParameter::getText, COMMA_SEPARATOR))
62+
.append("){");
5563
return builder;
5664
}
5765

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

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.intellij.psi.*;
66
import com.intellij.psi.search.GlobalSearchScope;
77
import org.apache.commons.collections.CollectionUtils;
8+
import org.jetbrains.annotations.NotNull;
89

910
import java.util.ArrayList;
1011
import java.util.Arrays;
@@ -17,9 +18,6 @@
1718
**/
1819
public class ObjectCopyMethodGenerator extends AbstractMethodGenerator {
1920

20-
private ObjectCopyMethodGenerator() {
21-
}
22-
2321
private void init(PsiMethod psiMethod) {
2422
super.methodName = psiMethod.getName();
2523
super.returnClassName = getReturnClassName(psiMethod);
@@ -36,30 +34,77 @@ public static ObjectCopyMethodGenerator getInstance(PsiMethod psiMethod) {
3634

3735
@Override
3836
protected String generateMethodBody() {
39-
if (CollectionUtils.isEmpty(parameters)) {
40-
return "";
37+
if (CollectionUtils.isEmpty(parameters) || VOID_KEYWORD.equals(returnClassName)) {
38+
return EMPTY_BODY;
4139
}
4240
StringBuilder result = new StringBuilder();
4341
String returnObjName = StringUtils.firstLowerCase(returnClassName);
44-
PsiParameter firstParameter = parameters.get(0);
45-
PsiClass firstClass = parameterClass.get(0);
46-
String parameterName = firstParameter.getName();
47-
result.append(generateNullCheck(parameterName));
48-
result.append(returnClassName).append(" ").append(returnObjName).append("= new ").append(returnClassName).append("();\n");
49-
for (PsiField field : firstClass.getFields()) {
42+
PsiParameter firstParameter = parameters.get(FIRST_INDEX);
43+
PsiClass firstParameterClass = parameterClass.get(FIRST_INDEX);
44+
if (firstParameterClass == null) {
45+
return EMPTY_BODY;
46+
}
47+
result.append(generateNullCheck(firstParameter.getName()));
48+
result.append(generateObjectCreateLine(returnObjName));
49+
for (PsiField field : firstParameterClass.getFields()) {
5050
PsiModifierList modifierList = field.getModifierList();
5151
if (modifierList == null ||
5252
modifierList.hasModifierProperty(PsiModifier.STATIC) ||
5353
modifierList.hasModifierProperty(PsiModifier.FINAL) ||
5454
modifierList.hasModifierProperty(PsiModifier.SYNCHRONIZED)) {
5555
continue;
5656
}
57-
result.append(returnObjName + ".set" + StringUtils.firstUpperCase(field.getName()) + "(" + parameterName + ".get" + StringUtils.firstUpperCase(field.getName()) + "());\n");
57+
result.append(generateFieldCopyLine(returnObjName, firstParameter.getName(), field));
5858
}
59-
result.append("return " + returnObjName + ";\n");
59+
result.append(generateReturnLine(returnObjName));
6060
return result.toString();
6161
}
6262

63+
/**
64+
* 生成示例:{@code UserDTO userDTO = new UserDTO();}
65+
*
66+
* @param returnObjName
67+
* @return
68+
*/
69+
@NotNull
70+
private String generateObjectCreateLine(String returnObjName) {
71+
return returnClassName + BLANK_SEPARATOR + returnObjName + "= new " + returnClassName + "();" + LINE_SEPARATOR;
72+
}
73+
74+
/**
75+
* 生成示例:{@code userDTO.setId(user.getId());}
76+
*
77+
* @param returnObjName
78+
* @param parameterName
79+
* @param field
80+
* @return
81+
*/
82+
@NotNull
83+
private String generateFieldCopyLine(String returnObjName, String parameterName, PsiField field) {
84+
return returnObjName + ".set" + StringUtils.firstUpperCase(field.getName()) + "(" + parameterName + ".get" + StringUtils.firstUpperCase(field.getName()) + "());" + LINE_SEPARATOR;
85+
}
86+
87+
/**
88+
* 生成示例:{@code return userDTO;}
89+
*
90+
* @param returnObjName
91+
* @return
92+
*/
93+
@NotNull
94+
private String generateReturnLine(String returnObjName) {
95+
return "return " + returnObjName + ";" + LINE_SEPARATOR;
96+
}
97+
98+
/**
99+
* 生成示例:{@code if (user == null) {return null;}}
100+
*
101+
* @param parameterName
102+
* @return
103+
*/
104+
private String generateNullCheck(String parameterName) {
105+
return "if(" + parameterName + "==null){return null;}";
106+
}
107+
63108
private static String getReturnClassName(PsiMethod psiMethod) {
64109
PsiType returnType = psiMethod.getReturnType();
65110
if (returnType == null) {
@@ -93,8 +138,4 @@ private List<JavaModify> getMethodModifies(PsiModifierList modifierList) {
93138
}
94139
return result;
95140
}
96-
97-
private String generateNullCheck(String parameterName) {
98-
return "if ( " + parameterName + "== null ){\nreturn null;\n}\n";
99-
}
100-
}
141+
}

0 commit comments

Comments
 (0)