From f1b5a2a5115ec7c6397641cc343369d943a39d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E7=83=BD?= Date: Wed, 14 Sep 2022 09:57:49 +0800 Subject: [PATCH 1/5] update --- settings.gradle | 2 +- src/main/resources/META-INF/plugin.xml | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/settings.gradle b/settings.gradle index b021b3f..40ce7f0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ -rootProject.name = 'reverseEngineering' +rootProject.name = 'javaCodeHelper' diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 7ae7fe3..20bfa1d 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,15 +1,17 @@ com.zlf.sql-reverse.plugin - SqlReverse + Java Code Helper 1.0 - zhanglinfeng + zhanglinfeng - 1.基础增删改查、列表的查询 + 定制化代码生成插件,根据建表SQL一键生成mapper、model、VO、service、controller
+ 邮箱704123055@qq.com ]]>
+
  • 基础增删查改
  • +
  • 列表查询
  • +
  • 批量新增
  • ]]>
    com.intellij.modules.platform -- Gitee From ced94f1ba30de0935dbf21b7f52d7bd8e038d382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E7=83=BD?= Date: Wed, 14 Sep 2022 10:28:52 +0800 Subject: [PATCH 2/5] update --- src/main/java/dialog/ToolWindowDialog.java | 9 +++++++-- src/main/java/factory/TemplateFactory.java | 13 ++++--------- src/main/java/util/CreateFileUtil.java | 14 ++++++-------- src/main/resources/META-INF/plugin.xml | 4 ++-- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/dialog/ToolWindowDialog.java b/src/main/java/dialog/ToolWindowDialog.java index 50e1c77..80e9d7f 100644 --- a/src/main/java/dialog/ToolWindowDialog.java +++ b/src/main/java/dialog/ToolWindowDialog.java @@ -18,8 +18,13 @@ public class ToolWindowDialog extends JDialog { setContentPane(contentPane); setModal(true); buttonOK.addActionListener(e -> { - new CreateFileUtil().createFile(authorField.getText(),modelNameField.getText(),packagePathField.getText(),textArea.getText()); - Messages.showMessageDialog(COMMON_CONSTANT.SUCCESS, "", Messages.getInformationIcon()); + try { + new CreateFileUtil().createFile(authorField.getText(),modelNameField.getText(),packagePathField.getText(),textArea.getText()); + Messages.showMessageDialog(COMMON_CONSTANT.SUCCESS, "", Messages.getInformationIcon()); + } catch (Exception ex) { + ex.printStackTrace(); + Messages.showMessageDialog(COMMON_CONSTANT.FAIL, "", Messages.getInformationIcon()); + } }); } diff --git a/src/main/java/factory/TemplateFactory.java b/src/main/java/factory/TemplateFactory.java index 162ddf3..6ec8778 100644 --- a/src/main/java/factory/TemplateFactory.java +++ b/src/main/java/factory/TemplateFactory.java @@ -21,7 +21,7 @@ public class TemplateFactory { private TemplateFactory() { } - public static TemplateFactory getInstance() { + public static TemplateFactory getInstance() throws IOException { if (templateFactory == null) { synchronized (TemplateFactory.class) { if (templateFactory == null) { @@ -33,14 +33,9 @@ public class TemplateFactory { Configuration configuration = new Configuration(Configuration.VERSION_2_3_23); configuration.setClassLoaderForTemplateLoading(COMMON_CONSTANT.class.getClassLoader(), COMMON_CONSTANT.TEMPLATE_PATH); configuration.setDefaultEncoding(COMMON_CONSTANT.ENCODING); - COMMON_CONSTANT.TEMPLATE_NAME_LIST.forEach(t -> { - try { - templateFactory.templateList.add(configuration.getTemplate(t)); - } catch (IOException e) { - e.printStackTrace(); - } - } - ); + for(String templateName : COMMON_CONSTANT.TEMPLATE_NAME_LIST){ + templateFactory.templateList.add(configuration.getTemplate(templateName)); + } } } } diff --git a/src/main/java/util/CreateFileUtil.java b/src/main/java/util/CreateFileUtil.java index 1860780..636e874 100644 --- a/src/main/java/util/CreateFileUtil.java +++ b/src/main/java/util/CreateFileUtil.java @@ -2,6 +2,7 @@ package util; import constant.COMMON_CONSTANT; import factory.TemplateFactory; +import freemarker.template.Template; import freemarker.template.TemplateException; import pojo.TableInfo; @@ -18,7 +19,7 @@ import java.util.Map; public class CreateFileUtil { - public void createFile(String author, String modelName, String packagePath, String createTableSql) { + public void createFile(String author, String modelName, String packagePath, String createTableSql) throws IOException, TemplateException { //初始化路径 COMMON_CONSTANT.init(author, modelName, packagePath); //解析Sql @@ -27,12 +28,9 @@ public class CreateFileUtil { dataMap.putAll(COMMON_CONSTANT.pathMap); //生成文件 TemplateFactory templateFactory = TemplateFactory.getInstance(); - templateFactory.getTemplateList().forEach(t -> { - try { - t.process(dataMap, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tableInfo.getFilePath(t.getName()))))); - } catch (TemplateException | IOException e) { - e.printStackTrace(); - } - }); + for(Template template : templateFactory.getTemplateList()){ + String filePath = tableInfo.getFilePath(template.getName()); + template.process(dataMap, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)))); + } } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 20bfa1d..ad9b648 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,8 +1,8 @@ - com.zlf.sql-reverse.plugin + com.zlf.java-code-helper.plugin Java Code Helper 1.0 - zhanglinfeng + zhanginfeng 邮箱704123055@qq.com -- Gitee From 2cc778a11545bd3976db6789896ca148996145fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E7=83=BD?= Date: Wed, 14 Sep 2022 10:53:50 +0800 Subject: [PATCH 3/5] update --- src/main/java/factory/TemplateFactory.java | 10 ++++++++++ src/main/java/pojo/TableInfo.java | 5 ----- src/main/java/util/CreateFileUtil.java | 9 +-------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/factory/TemplateFactory.java b/src/main/java/factory/TemplateFactory.java index 6ec8778..4e32544 100644 --- a/src/main/java/factory/TemplateFactory.java +++ b/src/main/java/factory/TemplateFactory.java @@ -3,9 +3,13 @@ package factory; import constant.COMMON_CONSTANT; import freemarker.template.Configuration; import freemarker.template.Template; +import freemarker.template.TemplateException; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; @@ -46,4 +50,10 @@ public class TemplateFactory { return templateList; } + public void create(Object dataModel,String fileBasicName) throws IOException, TemplateException { + for(Template template : templateFactory.getTemplateList()){ + String filePath = COMMON_CONSTANT.FULL_PATH + fileBasicName + template.getName().replaceAll(COMMON_CONSTANT.TEMPLATE_SUFFIX,"").replaceAll(COMMON_CONSTANT.MODEL,""); + template.process(dataModel, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)))); + } + } } diff --git a/src/main/java/pojo/TableInfo.java b/src/main/java/pojo/TableInfo.java index 4791087..bf1c3c5 100644 --- a/src/main/java/pojo/TableInfo.java +++ b/src/main/java/pojo/TableInfo.java @@ -1,7 +1,6 @@ package pojo; import com.google.gson.Gson; -import constant.COMMON_CONSTANT; import util.StringUtil; import java.util.Arrays; @@ -89,8 +88,4 @@ public class TableInfo { Gson gs = new Gson(); return gs.fromJson(gs.toJson(this), Map.class); } - - public String getFilePath(String templateFileName) { - return COMMON_CONSTANT.FULL_PATH + this.tableName + templateFileName.replaceAll(COMMON_CONSTANT.TEMPLATE_SUFFIX,"").replaceAll(COMMON_CONSTANT.MODEL,""); - } } diff --git a/src/main/java/util/CreateFileUtil.java b/src/main/java/util/CreateFileUtil.java index 636e874..cc13b5e 100644 --- a/src/main/java/util/CreateFileUtil.java +++ b/src/main/java/util/CreateFileUtil.java @@ -2,14 +2,10 @@ package util; import constant.COMMON_CONSTANT; import factory.TemplateFactory; -import freemarker.template.Template; import freemarker.template.TemplateException; import pojo.TableInfo; -import java.io.BufferedWriter; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import java.util.Map; /** @@ -28,9 +24,6 @@ public class CreateFileUtil { dataMap.putAll(COMMON_CONSTANT.pathMap); //生成文件 TemplateFactory templateFactory = TemplateFactory.getInstance(); - for(Template template : templateFactory.getTemplateList()){ - String filePath = tableInfo.getFilePath(template.getName()); - template.process(dataMap, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)))); - } + templateFactory.create(dataMap,tableInfo.getTableName()); } } -- Gitee From 6b379cd84b9be1db79e541f399b654b47c25374b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E7=83=BD?= Date: Wed, 14 Sep 2022 17:52:09 +0800 Subject: [PATCH 4/5] update --- README.md | 10 +-- build.gradle | 5 +- .../java/action/ClassConversionAction.java | 73 +++++++++++++++++++ src/main/java/constant/COMMON_CONSTANT.java | 6 +- src/main/java/pojo/ColumnInfo.java | 4 +- src/main/java/pojo/TableInfo.java | 3 +- ...{TypeConversionUtil.java => TypeUtil.java} | 10 ++- src/main/resources/META-INF/plugin.xml | 31 ++++++-- 8 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 src/main/java/action/ClassConversionAction.java rename src/main/java/util/{TypeConversionUtil.java => TypeUtil.java} (57%) diff --git a/README.md b/README.md index 73d4cae..8fc802d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,3 @@ #### idea 插件简介 -定制化生成代码,根据建表SQL一键生成mapper、model、VO、service、controller,包括以下功能 -1. 基础增删改查、批量新增、列表的查询 - - -#### 注意事项 -sql 一定要带上 COMMENT - -#### 版本 -1.0 测试版 \ No newline at end of file +定制化java代码开发插件,详情请在idea中搜索Java Code Helper diff --git a/build.gradle b/build.gradle index ca113c0..ef2f867 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ plugins { - id 'org.jetbrains.intellij' version '1.8.0' + id 'org.jetbrains.intellij' version '1.9.0' id 'java' } group 'org.example' -version '1.0-SNAPSHOT' +version '1.1.0' repositories { mavenCentral() @@ -18,6 +18,7 @@ dependencies { intellij { version = '2021.2.3' + plugins = ['java'] } test { diff --git a/src/main/java/action/ClassConversionAction.java b/src/main/java/action/ClassConversionAction.java new file mode 100644 index 0000000..0763395 --- /dev/null +++ b/src/main/java/action/ClassConversionAction.java @@ -0,0 +1,73 @@ +package action; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElementFactory; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiJavaFile; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiParameter; +import com.intellij.psi.util.PsiUtilBase; +import constant.COMMON_CONSTANT; +import util.StringUtil; +import util.TypeUtil; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class ClassConversionAction extends AnAction { + + @Override + public void actionPerformed(AnActionEvent event) { + //获取当前的编辑器对象 + Editor editor = event.getRequiredData(CommonDataKeys.EDITOR); + //获取当前项目 + Project project = event.getData(CommonDataKeys.PROJECT); + if (null == project) { + return; + } + PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); + PsiJavaFile psiJavaFile = (PsiJavaFile) file; + if (null == psiJavaFile) { + return; + } + PsiClass psiClass = psiJavaFile.getClasses()[0]; + WriteCommandAction.runWriteCommandAction(project, () -> { + //获取构造方法 + PsiMethod[] constructorMethods = psiClass.getConstructors(); + if (constructorMethods.length > 0) { + for (PsiMethod psiMethod : constructorMethods) { + //获取方法为空的构造方法 + if (!psiMethod.getText().contains("this.")) { + PsiParameter[] parameterArr = psiMethod.getParameterList().getParameters(); + for (PsiParameter parameter : parameterArr) { + if (TypeUtil.isObject(parameter.getType().getCanonicalText())) { + String parametersStr = Arrays.stream(parameterArr).map(PsiParameter::getText).collect(Collectors.joining(",")); + String objectStr = parameter.getText().split(COMMON_CONSTANT.SPACE)[1]; + createConstructorMethod(psiClass, project, parametersStr, objectStr, psiMethod); + psiMethod.delete(); + return; + } + } + } + } + } + createConstructorMethod(psiClass, project, "Object obj", "obj", null); + }); + } + + private void createConstructorMethod(PsiClass psiClass, Project project, String parametersStr, String objectStr, PsiMethod afterMethod) { + PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); + StringBuilder constructorMethodSb = new StringBuilder("public ").append(psiClass.getName()).append("(").append(parametersStr).append(") {"); + constructorMethodSb.append(Arrays.stream(psiClass.getFields()).map(f -> "this." + f.getName() + " = " + objectStr + ".get" + StringUtil.toUpperCaseFirst(f.getName()) + "();").collect(Collectors.joining())); + constructorMethodSb.append("}"); + PsiMethod newConstructor = factory.createMethodFromText(constructorMethodSb.toString(), psiClass); + psiClass.addAfter(newConstructor, afterMethod); + } +} diff --git a/src/main/java/constant/COMMON_CONSTANT.java b/src/main/java/constant/COMMON_CONSTANT.java index 46d13ff..ffbbcad 100644 --- a/src/main/java/constant/COMMON_CONSTANT.java +++ b/src/main/java/constant/COMMON_CONSTANT.java @@ -22,13 +22,15 @@ public class COMMON_CONSTANT { public static final String FAIL = "失败"; public static final String ENCODING = "UTF-8"; public static final String DOUBLE_BACKSLASH = "\\"; + public static final String SPACE = "\\s+"; public static final String SLASH = "/"; public static final String JAVA_FILE_PATH = "\\src\\main\\java\\"; public static final String MODEL = "Model"; public static final String TEMPLATE_SUFFIX = ".ftl"; public static final String TEMPLATE_PATH = "./templates"; - public static final List TEMPLATE_NAME_LIST = Arrays.asList("Model.java.ftl", "Mapper.java.ftl", "Service.java.ftl", "ServiceImpl.java.ftl", - "VO.java.ftl", "Controller.java.ftl"); + public static final List BASIC_TYPE_LIST = Arrays.asList("int,short,long,byte,float,double,boolean、char,Integer,Short,Long,Byte,Float,Double,Boolean,Character".split(",")); + public static final List COMMON_TYPE_LIST = Arrays.asList("String,Date,Timestamp,BigDecimal".split(",")); + public static final List TEMPLATE_NAME_LIST = Arrays.asList("Model.java.ftl", "Mapper.java.ftl", "Service.java.ftl", "ServiceImpl.java.ftl", "VO.java.ftl", "Controller.java.ftl"); public static void init(String author, String modelName, String packagePath){ pathMap = new HashMap<>(); diff --git a/src/main/java/pojo/ColumnInfo.java b/src/main/java/pojo/ColumnInfo.java index b797b73..8b36c3f 100644 --- a/src/main/java/pojo/ColumnInfo.java +++ b/src/main/java/pojo/ColumnInfo.java @@ -1,7 +1,7 @@ package pojo; import util.StringUtil; -import util.TypeConversionUtil; +import util.TypeUtil; import java.util.Arrays; import java.util.List; @@ -31,7 +31,7 @@ public class ColumnInfo { this.sqlColumnName = valueList.get(0); this.firstUpperColumnName = Arrays.stream(this.sqlColumnName.split("_")).map(StringUtil::toUpperCaseFirst).collect(Collectors.joining()); this.columnName = StringUtil.toLowerCaseFirst(this.firstUpperColumnName); - this.columnType = TypeConversionUtil.conversion(valueList.get(1)); + this.columnType = TypeUtil.toJavaType(valueList.get(1)); this.columnComment = valueList.get(valueList.size() - 1); } diff --git a/src/main/java/pojo/TableInfo.java b/src/main/java/pojo/TableInfo.java index bf1c3c5..7bcb584 100644 --- a/src/main/java/pojo/TableInfo.java +++ b/src/main/java/pojo/TableInfo.java @@ -1,6 +1,7 @@ package pojo; import com.google.gson.Gson; +import constant.COMMON_CONSTANT; import util.StringUtil; import java.util.Arrays; @@ -31,7 +32,7 @@ public class TableInfo { public TableInfo(String createTableSql) { List lineList = List.of(createTableSql.split("\\r?\\n")); - this.sqlTableName = lineList.get(0).split("\\s+")[2]; + this.sqlTableName = lineList.get(0).split(COMMON_CONSTANT.SPACE)[2]; if (this.sqlTableName.contains(".")) { this.sqlTableName = this.sqlTableName.split("\\.")[1].replaceAll("['`]", ""); } diff --git a/src/main/java/util/TypeConversionUtil.java b/src/main/java/util/TypeUtil.java similarity index 57% rename from src/main/java/util/TypeConversionUtil.java rename to src/main/java/util/TypeUtil.java index 192dcda..77007be 100644 --- a/src/main/java/util/TypeConversionUtil.java +++ b/src/main/java/util/TypeUtil.java @@ -1,12 +1,14 @@ package util; +import constant.COMMON_CONSTANT; + /** * @Author zhanglinfeng * @Date create in 2022/9/8 17:37 */ -public class TypeConversionUtil { +public class TypeUtil { - public static String conversion(String sqlType) { + public static String toJavaType(String sqlType) { if (sqlType.contains("int")) { return "Integer"; } else if (sqlType.contains("timestamp")) { @@ -18,4 +20,8 @@ public class TypeConversionUtil { } } + public static boolean isObject(String type){ + return !COMMON_CONSTANT.BASIC_TYPE_LIST.contains(type) && !COMMON_CONSTANT.COMMON_TYPE_LIST.contains(type); + } + } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index ad9b648..cf00fcb 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,23 +1,38 @@ com.zlf.java-code-helper.plugin Java Code Helper - 1.0 - zhanginfeng + 1.1.0 + zhanglinfeng - 邮箱704123055@qq.com +

    定制化java代码开发插件,邮箱704123055@qq.com

    +
  • 根据建表SQL(带上COMMENT)一键生成mapper、model、VO、service、controller代码。支持基础增删查改、列表查询、批量新增
  • +
  • 一键生成类转换的构造参数,例:Object1 to Object2
  • ]]>
    -
  • 基础增删查改
  • -
  • 列表查询
  • -
  • 批量新增
  • +

    1.0.0

    +
      +
    • 基础增删查改
    • +
    • 列表查询
    • +
    • 批量新增
    • +
    +

    1.1.0

    +
      +
    • 一键生成类转换的构造参数,例:Object1 to Object2
    • +
    ]]>
    com.intellij.modules.platform + com.intellij.modules.lang + com.intellij.modules.java + + + + + +
    \ No newline at end of file -- Gitee From 13a8dfbb86e68e65fbc7ce897feb256dd899133a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E7=83=BD?= Date: Wed, 14 Sep 2022 18:13:45 +0800 Subject: [PATCH 5/5] update --- .../java/action/ClassConversionAction.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main/java/action/ClassConversionAction.java b/src/main/java/action/ClassConversionAction.java index 0763395..a8166a4 100644 --- a/src/main/java/action/ClassConversionAction.java +++ b/src/main/java/action/ClassConversionAction.java @@ -38,36 +38,46 @@ public class ClassConversionAction extends AnAction { return; } PsiClass psiClass = psiJavaFile.getClasses()[0]; + if (psiClass.getFields().length == 0) { + return; + } WriteCommandAction.runWriteCommandAction(project, () -> { + String parametersStr = "Object obj"; + String objectStr = "obj"; + PsiMethod deleteMethod = null; //获取构造方法 PsiMethod[] constructorMethods = psiClass.getConstructors(); if (constructorMethods.length > 0) { + outCycle: for (PsiMethod psiMethod : constructorMethods) { //获取方法为空的构造方法 if (!psiMethod.getText().contains("this.")) { PsiParameter[] parameterArr = psiMethod.getParameterList().getParameters(); for (PsiParameter parameter : parameterArr) { if (TypeUtil.isObject(parameter.getType().getCanonicalText())) { - String parametersStr = Arrays.stream(parameterArr).map(PsiParameter::getText).collect(Collectors.joining(",")); - String objectStr = parameter.getText().split(COMMON_CONSTANT.SPACE)[1]; - createConstructorMethod(psiClass, project, parametersStr, objectStr, psiMethod); - psiMethod.delete(); - return; + parametersStr = Arrays.stream(parameterArr).map(PsiParameter::getText).collect(Collectors.joining(",")); + objectStr = parameter.getText().split(COMMON_CONSTANT.SPACE)[1]; + deleteMethod = psiMethod; + break outCycle; } } } } } - createConstructorMethod(psiClass, project, "Object obj", "obj", null); + String finalObjectStr = objectStr; + PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); + StringBuilder constructorMethodSb = new StringBuilder("public ").append(psiClass.getName()).append("(").append(parametersStr).append(") {"); + constructorMethodSb.append(Arrays.stream(psiClass.getFields()).map(f -> "this." + f.getName() + " = " + finalObjectStr + ".get" + StringUtil.toUpperCaseFirst(f.getName()) + "();").collect(Collectors.joining())); + constructorMethodSb.append("}"); + PsiMethod newConstructor = factory.createMethodFromText(constructorMethodSb.toString(), psiClass); + if (null != deleteMethod) { + psiClass.addAfter(newConstructor, deleteMethod); + deleteMethod.delete(); + } else if (constructorMethods.length > 0) { + psiClass.addAfter(newConstructor, psiClass.getConstructors()[0]); + } else { + psiClass.addBefore(newConstructor, psiClass.getMethods()[0]); + } }); } - - private void createConstructorMethod(PsiClass psiClass, Project project, String parametersStr, String objectStr, PsiMethod afterMethod) { - PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); - StringBuilder constructorMethodSb = new StringBuilder("public ").append(psiClass.getName()).append("(").append(parametersStr).append(") {"); - constructorMethodSb.append(Arrays.stream(psiClass.getFields()).map(f -> "this." + f.getName() + " = " + objectStr + ".get" + StringUtil.toUpperCaseFirst(f.getName()) + "();").collect(Collectors.joining())); - constructorMethodSb.append("}"); - PsiMethod newConstructor = factory.createMethodFromText(constructorMethodSb.toString(), psiClass); - psiClass.addAfter(newConstructor, afterMethod); - } } -- Gitee