Skip to content

Commit e4dba99

Browse files
committed
fix for kotlin using setter method to check.
1 parent b36d049 commit e4dba99

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ targetCompatibility = 1.8
3030

3131
intellij {
3232
// IC-2016.1
33-
version 'IU-2018.1' //IntelliJ IDEA dependency
33+
version 'IU-2018.3' //IntelliJ IDEA dependency
3434
plugins 'coverage' //Bundled plugin dependencies
3535
pluginName 'GenerateAllSetter'
3636
updateSinceUntilBuild false
@@ -39,7 +39,7 @@ intellij {
3939
// username 'AmailP'
4040
// pluginId '7415'
4141
// }
42-
plugins 'Kotlin','PsiViewer:2018.1.2'
42+
plugins 'Kotlin'
4343

4444
sandboxDirectory 'sandbox'
4545
}

src/main/java/com/bruce/intellijplugin/generatesetter/koltinActions/GenerateAllSetterForPropertiesAction.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.bruce.intellijplugin.generatesetter.CommonConstants;
1818
import com.bruce.intellijplugin.generatesetter.context.KotlinContext;
19+
import com.bruce.intellijplugin.generatesetter.utils.PsiClassUtils;
1920
import com.bruce.intellijplugin.generatesetter.utils.PsiDocumentUtils;
2021
import com.bruce.intellijplugin.generatesetter.utils.PsiToolUtils;
2122
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
@@ -28,6 +29,8 @@
2829
import org.jetbrains.annotations.NotNull;
2930
import org.jetbrains.kotlin.psi.*;
3031

32+
import java.util.Locale;
33+
3134
/**
3235
* @author bruce ge
3336
*/
@@ -40,16 +43,19 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
4043
KtProperty property = currentElementContext.getProperty();
4144
String propertyName = property.getName();
4245

43-
PsiField[] allFields = currentElementContext.getCurrentClass().getAllFields();
46+
PsiMethod[] methods = currentElementContext.getCurrentClass().getAllMethods();
4447

4548
PsiDocumentManager instance = PsiDocumentManager.getInstance(project);
4649
Document document = instance.getDocument(element.getContainingFile());
4750

4851
String splitText = PsiToolUtils.calculateSplitText(document, property.getTextRange().getStartOffset());
4952

5053
StringBuilder builder = new StringBuilder();
51-
for (PsiField allField : allFields) {
52-
builder.append(splitText + propertyName + "." + allField.getName() + " = ");
54+
for (PsiMethod m : methods) {
55+
if(PsiClassUtils.isValidSetMethod(m)){
56+
String methodPropName = methodToProperty(m.getName());
57+
builder.append(splitText + propertyName + "." + methodPropName + " = ");
58+
}
5359
}
5460

5561
document.insertString(property.getTextRange().getEndOffset(),
@@ -58,6 +64,20 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
5864
PsiDocumentUtils.commitAndSaveDocument(instance, document);
5965
}
6066

67+
public static String methodToProperty(String name) {
68+
if (name.startsWith("set")) {
69+
name = name.substring(3);
70+
} else {
71+
// throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
72+
return "";
73+
}
74+
75+
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
76+
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
77+
}
78+
return name;
79+
}
80+
6181
@Override
6282
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
6383
KotlinContext currentElementContext =

src/main/resources/META-INF/plugin.xml

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<description><![CDATA[
77
<p><a href="https://github.com/gejun123456/intellij-generateAllSetMethod">GitHub</a>
88
|&nbsp<a href="https://github.com/gejun123456/intellij-generateAllSetMethod/issues">Issues</a></p>
9-
109
<p> generate call to class all setter method by alt+enter on the variable class</p>
1110
<p> generate a converter two object when they have same field</p>
1211
<p> generate default value when returnType is List Set Map </p>
@@ -17,19 +16,63 @@
1716
<p> user.setName("");</p>
1817
<p> user.setPassword("");</p>
1918
<p> support all your class set method including super class</p>
19+
<p> support kotlin
2020
<p> view more on <a href ="https://github.com/gejun123456/intellij-generateAllSetMethod">https://github.com/gejun123456/intellij-generateAllSetMethod</a></p>
2121
]]></description>
2222

2323
<change-notes><![CDATA[
24-
<li>1.9 fix maps import and add default value for some package</li>
25-
<li>1.8 support byte type</li>
26-
<li>1.7 support two list convert</li>
27-
<p>1.6 fix exception when the cursor on constructor method</p>
28-
<p>1.5 generate default value for returnType is List Set Map</p>
29-
<p>1.4 could use on method, and if method parameter contain same value, then will pass to it</p>
30-
<p>1.3 fix doc</p>
31-
<p>1.2 fix suffix for long and double</p>
32-
<p>1.1 auto add import</p>
24+
<strong>2.1</strong>
25+
<ul>
26+
<li>[NEW]add base support for kotlin</li>
27+
<li>[IMPROVE]generate all setter name to Generate all setter</li>
28+
<li>[IMPROVE]add space in new operator</li>
29+
</ul>
30+
<strong>1.9</strong>
31+
<ul>
32+
<li>[IMPROVE]maps import and add default value for some package</li>
33+
</ul>
34+
35+
<strong>1.8</strong>
36+
<ul>
37+
<li>[IMPROVE]support byte type</li>
38+
</ul>
39+
40+
<strong>1.7</strong>
41+
<ul>
42+
<li>[NEW]support two list convert</li>
43+
</ul>
44+
45+
<strong>1.6</strong>
46+
<ul>
47+
<li>[FIX]fix exception when the cursor on constructor method</li>
48+
</ul>
49+
50+
<strong>1.5</strong>
51+
<ul>
52+
<li>[NEW]generate default value for returnType is List Set Map</li>
53+
</ul>
54+
55+
<strong>1.4</strong>
56+
<ul>
57+
<li>[NEW]could use on method, and if method parameter contain same value, then will pass to it</li>
58+
</ul>
59+
60+
<strong>1.3</strong>
61+
<ul>
62+
<li>[FIX]fix doc</li>
63+
</ul>
64+
65+
66+
<strong>1.2</strong>
67+
<ul>
68+
<li>[NEW]fix doc</li>
69+
<li>[FIX]fix suffix for long and double</li>
70+
</ul>
71+
72+
<strong>1.1</strong>
73+
<ul>
74+
<li>[NEW]auto add import to class</li>
75+
</ul>
3376
]]>
3477
</change-notes>
3578

0 commit comments

Comments
 (0)