|
| 1 | +package cn.bigcoder.plugin.objecthelper; |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.AnAction; |
| 4 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 5 | +import com.intellij.openapi.actionSystem.LangDataKeys; |
| 6 | +import com.intellij.openapi.actionSystem.PlatformDataKeys; |
| 7 | +import com.intellij.openapi.command.WriteCommandAction; |
| 8 | +import com.intellij.openapi.editor.Editor; |
| 9 | +import com.intellij.psi.*; |
| 10 | +import com.intellij.psi.search.GlobalSearchScope; |
| 11 | +import com.intellij.psi.util.PsiTreeUtil; |
| 12 | +import com.intellij.ui.CollectionListModel; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class ObjectHelper extends AnAction { |
| 17 | + |
| 18 | + @Override |
| 19 | + public void actionPerformed(AnActionEvent e) { |
| 20 | + System.out.println("789"); |
| 21 | + PsiMethod method = getPsiMethodFromContext(e); |
| 22 | + generateO2OMethod(method); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * 启动写线程 |
| 27 | + * |
| 28 | + * @param psiMethod |
| 29 | + */ |
| 30 | + private void generateO2OMethod(PsiMethod psiMethod) { |
| 31 | + new WriteCommandAction.Simple(psiMethod.getProject(), psiMethod.getContainingFile()) { |
| 32 | + @Override |
| 33 | + protected void run() throws Throwable { |
| 34 | + createO2O(psiMethod); |
| 35 | + } |
| 36 | + }.execute(); |
| 37 | + } |
| 38 | + |
| 39 | + private void createO2O(PsiMethod psiMethod) { |
| 40 | + String methodName = psiMethod.getName(); |
| 41 | + PsiType returnType = psiMethod.getReturnType(); |
| 42 | + if (returnType == null) { |
| 43 | + return; |
| 44 | + } |
| 45 | + String returnClassName = returnType.getPresentableText(); |
| 46 | + PsiParameter psiParameter = psiMethod.getParameterList().getParameters()[0]; |
| 47 | + //带package的class名称 |
| 48 | + String parameterClassWithPackage = psiParameter.getType().getInternalCanonicalText(); |
| 49 | + //为了解析字段,这里需要加载参数的class |
| 50 | + JavaPsiFacade facade = JavaPsiFacade.getInstance(psiMethod.getProject()); |
| 51 | + PsiClass parameterClass = facade.findClass(parameterClassWithPackage, GlobalSearchScope.allScope(psiMethod.getProject())); |
| 52 | + if (parameterClass == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + List<PsiField> parameterFields = new CollectionListModel<PsiField>(parameterClass.getFields()).getItems(); |
| 56 | + String methodText = getMethodText(methodName, returnClassName, psiParameter, parameterFields); |
| 57 | + PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiMethod.getProject()); |
| 58 | + PsiMethod toMethod = elementFactory.createMethodFromText(methodText, psiMethod); |
| 59 | + psiMethod.replace(toMethod); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param methodName 方法名称 |
| 64 | + * @param returnClassName 返回的值的class名称 |
| 65 | + * @param psiParameter 方法参数第一个值 |
| 66 | + * @param parameterFields 方法参数的class里field 列表 |
| 67 | + * @return 方法体的字符串 |
| 68 | + */ |
| 69 | + private String getMethodText(String methodName, String returnClassName, PsiParameter psiParameter, List<PsiField> parameterFields) { |
| 70 | + String returnObjName = returnClassName.substring(0, 1).toLowerCase() + returnClassName.substring(1); |
| 71 | + String parameterClass = psiParameter.getText(); |
| 72 | + String parameterName = psiParameter.getName(); |
| 73 | + StringBuilder builder = new StringBuilder("public static " + returnClassName + " " + methodName + " ("); |
| 74 | + builder.append(parameterClass + " ) {\n"); |
| 75 | + builder.append("if ( " + parameterName + "== null ){\n").append("return null;\n}").append(returnClassName + " " + returnObjName + "= new " + returnClassName + "();\n"); |
| 76 | + for (PsiField field : parameterFields) { |
| 77 | + PsiModifierList modifierList = field.getModifierList(); |
| 78 | + if (modifierList == null || modifierList.hasModifierProperty(PsiModifier.STATIC) || modifierList.hasModifierProperty(PsiModifier.FINAL) || modifierList.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + builder.append(returnObjName + ".set" + getFirstUpperCase(field.getName()) + "(" + parameterName + ".get" + getFirstUpperCase(field.getName()) + "());\n"); |
| 82 | + } |
| 83 | + builder.append("return " + returnObjName + ";\n"); |
| 84 | + builder.append("}\n"); |
| 85 | + return builder.toString(); |
| 86 | + } |
| 87 | + |
| 88 | + private String getFirstUpperCase(String oldStr) { |
| 89 | + return oldStr.substring(0, 1).toUpperCase() + oldStr.substring(1); |
| 90 | + } |
| 91 | + |
| 92 | + private PsiMethod getPsiMethodFromContext(AnActionEvent e) { |
| 93 | + PsiElement elementAt = getPsiElement(e); |
| 94 | + if (elementAt == null) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + return PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class); |
| 98 | + } |
| 99 | + |
| 100 | + private PsiElement getPsiElement(AnActionEvent e) { |
| 101 | + PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); |
| 102 | + Editor editor = e.getData(PlatformDataKeys.EDITOR); |
| 103 | + if (psiFile == null || editor == null) { |
| 104 | + e.getPresentation().setEnabled(false); |
| 105 | + return null; |
| 106 | + } |
| 107 | + //用来获取当前光标处的PsiElement |
| 108 | + int offset = editor.getCaretModel().getOffset(); |
| 109 | + return psiFile.findElementAt(offset); |
| 110 | + } |
| 111 | +} |
0 commit comments