Skip to content

Commit 125744b

Browse files
committed
refactor generateo2o
1 parent 1010e0e commit 125744b

File tree

9 files changed

+342
-112
lines changed

9 files changed

+342
-112
lines changed

object-helper.jar

8.91 KB
Binary file not shown.

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</extensions>
2828

2929
<actions>
30-
<action id="ObjectHelper" class="cn.bigcoder.plugin.objecthelper.ObjectHelper" text="generateo2o">
30+
<action id="ObjectHelper" class="cn.bigcoder.plugin.objecthelper.GenerateO2O" text="generateo2o">
3131
<add-to-group group-id="GenerateGroup" anchor="last"/>
3232
</action>
3333
</actions>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cn.bigcoder.plugin.objecthelper;
2+
3+
import cn.bigcoder.plugin.objecthelper.generator.Generator;
4+
import cn.bigcoder.plugin.objecthelper.generator.method.ObjectCopyMethodGenerator;
5+
import com.intellij.openapi.actionSystem.AnAction;
6+
import com.intellij.openapi.actionSystem.AnActionEvent;
7+
import com.intellij.openapi.actionSystem.LangDataKeys;
8+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
9+
import com.intellij.openapi.command.WriteCommandAction;
10+
import com.intellij.openapi.editor.Editor;
11+
import com.intellij.psi.*;
12+
import com.intellij.psi.util.PsiTreeUtil;
13+
14+
public class GenerateO2O extends AnAction {
15+
16+
@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();
34+
}
35+
36+
private void generateO2O(PsiMethod psiMethod) {
37+
Generator generator = ObjectCopyMethodGenerator.getInstance(psiMethod);
38+
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiMethod.getProject());
39+
PsiMethod toMethod = elementFactory.createMethodFromText(generator.generate(), psiMethod);
40+
psiMethod.replace(toMethod);
41+
}
42+
43+
private PsiMethod getPsiMethodFromContext(AnActionEvent e) {
44+
PsiElement elementAt = getPsiElement(e);
45+
if (elementAt == null) {
46+
return null;
47+
}
48+
return PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
49+
}
50+
51+
private PsiElement getPsiElement(AnActionEvent e) {
52+
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
53+
Editor editor = e.getData(PlatformDataKeys.EDITOR);
54+
if (psiFile == null || editor == null) {
55+
e.getPresentation().setEnabled(false);
56+
return null;
57+
}
58+
//用来获取当前光标处的PsiElement
59+
int offset = editor.getCaretModel().getOffset();
60+
return psiFile.findElementAt(offset);
61+
}
62+
}

src/cn/bigcoder/plugin/objecthelper/ObjectHelper.java

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.bigcoder.plugin.objecthelper.common.enums;
2+
3+
/**
4+
* @author: Jindong.Tian
5+
* @date: 2021-01-09
6+
**/
7+
public enum JavaModify {
8+
PUBLIC("public", 1),
9+
PROTECTED("protected", 1),
10+
PRIVATE("private", 1),
11+
STATIC("static", 2),
12+
FINAL("final", 3),
13+
;
14+
private String name;
15+
private Integer priority;
16+
17+
JavaModify(String name, Integer priority) {
18+
this.name = name;
19+
this.priority = priority;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public JavaModify nameOf(String modify){
27+
if (modify == null){
28+
return null;
29+
}
30+
for (JavaModify item : values()) {
31+
if (modify.equals(item.getName())){
32+
return item;
33+
}
34+
}
35+
return null;
36+
}
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.bigcoder.plugin.objecthelper.common.util;
2+
3+
import java.util.Collection;
4+
import java.util.Locale;
5+
import java.util.function.Function;
6+
7+
/**
8+
* @author: Jindong.Tian
9+
* @date: 2021-01-09
10+
**/
11+
public class StringUtils {
12+
13+
public static String join(Collection<String> items, String separator) {
14+
return join(items, Function.identity(), separator);
15+
}
16+
17+
public static <T> String join(Collection<T> items, Function<T, String> handle, String separator) {
18+
if (items == null || items.isEmpty()) {
19+
return "";
20+
}
21+
StringBuilder result = new StringBuilder();
22+
int index = 0;
23+
for (T item : items) {
24+
result.append(handle.apply(item));
25+
if (index < items.size() - 1) {
26+
result.append(separator);
27+
}
28+
index++;
29+
}
30+
return result.toString();
31+
}
32+
33+
public static boolean isEmpty(String str) {
34+
if (str == null || " ".equals(str)) {
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
public static boolean isNotEmpty(String str) {
41+
return !isEmpty(str);
42+
}
43+
44+
/**
45+
* 将首字母转为大写
46+
* @param oldStr
47+
* @return
48+
*/
49+
public static String firstUpperCase(String oldStr) {
50+
return oldStr.substring(0, 1).toUpperCase() + oldStr.substring(1);
51+
}
52+
53+
/**
54+
* 将首字母转为小写
55+
* @param oldStr
56+
* @return
57+
*/
58+
public static String firstLowerCase(String oldStr) {
59+
return oldStr.substring(0, 1).toLowerCase(Locale.ROOT) + oldStr.substring(1);
60+
}
61+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.bigcoder.plugin.objecthelper.generator;
2+
3+
/**
4+
* @author: Jindong.Tian
5+
* @date: 2021-01-09
6+
* @description:
7+
**/
8+
public interface Generator {
9+
String generate();
10+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cn.bigcoder.plugin.objecthelper.generator.method;
2+
3+
import cn.bigcoder.plugin.objecthelper.common.enums.JavaModify;
4+
import cn.bigcoder.plugin.objecthelper.generator.Generator;
5+
import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
6+
import com.intellij.psi.PsiClass;
7+
import com.intellij.psi.PsiParameter;
8+
import org.apache.commons.collections.CollectionUtils;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @author: Jindong.Tian
14+
* @date: 2021-01-09
15+
**/
16+
public abstract class AbstractMethodGenerator implements Generator {
17+
18+
/**
19+
* 方法修饰符
20+
*/
21+
protected List<JavaModify> methodModifies;
22+
/**
23+
* 方法返回类型名称
24+
*/
25+
protected String returnClassName;
26+
/**
27+
* 方法名称
28+
*/
29+
protected String methodName;
30+
/**
31+
* 方法参数
32+
*/
33+
protected List<PsiParameter> parameters;
34+
/**
35+
* 方法参数
36+
*/
37+
protected List<PsiClass> parameterClass;
38+
39+
40+
public String generate() {
41+
StringBuilder result = generateMethodFirstLine()
42+
.append(generateMethodBody())
43+
.append("}\n");
44+
return result.toString();
45+
}
46+
47+
protected StringBuilder generateMethodFirstLine() {
48+
StringBuilder builder = new StringBuilder();
49+
methodModifies.forEach(e -> builder.append(e.getName() + " "));
50+
builder.append(returnClassName + " ")
51+
.append(methodName)
52+
.append("(")
53+
.append(StringUtils.join(parameters, PsiParameter::getText, " ,"))
54+
.append(") {\n");
55+
return builder;
56+
}
57+
58+
protected boolean hasModifierProperty(String modify) {
59+
if (CollectionUtils.isEmpty(methodModifies)) {
60+
return false;
61+
}
62+
for (JavaModify methodModify : methodModifies) {
63+
if (methodModify.getName().equals(modify)) {
64+
return true;
65+
}
66+
}
67+
return false;
68+
}
69+
70+
abstract String generateMethodBody();
71+
}

0 commit comments

Comments
 (0)