diff --git a/README.md b/README.md
index e80575f..2e927f6 100644
--- a/README.md
+++ b/README.md
@@ -5,10 +5,10 @@
JetBrains Intellij IDEA ObjectHelper 插件旨在减少开发者重复低效的劳动,使开发者能够更专注于业务逻辑的开发。
@@ -16,6 +16,7 @@ JetBrains Intellij IDEA ObjectHelper 插件旨在减少开发者重复低效的
该插件包含以下功能:
- 对象拷贝
+ set模式:

@@ -23,6 +24,14 @@ JetBrains Intellij IDEA ObjectHelper 插件旨在减少开发者重复低效的

+ 当对象中包含`builder` 或者 `newBuilder`方法时,则插件默认会采用 builder 模式生成代码:
+
+ 
+
+ 如果你的builder类生成的方法名与插件默认生成的不同,可以在设置中更改:
+
+ 
+
- Java类转JSON

@@ -42,7 +51,7 @@ File->Settings->Tools->Object Helper 即可进入插件的配置页面

- `generate field mode = target` 代表以方法返回类型的字段为基础生成对象拷贝;
-`generate field mode = source` 代表以方法入参类型的字段为基础生成对象拷贝。
+ `generate field mode = source` 代表以方法入参类型的字段为基础生成对象拷贝。
- `non-existent field generate annotation = yes` 代表当目标字段在源对象中不存在时,是否以注释的形式生成代码,如果为 `no`,则代表不生成这一个字段拷贝代码。
@@ -53,6 +62,7 @@ object-helper插件未来功能支持计划:
- [x] Class 转 IDL(Class To Thrift IDL)
- [x] Class 转 XML(Class To XML)
- [x] 个性化配置
+- [x] Object Copy Method 功能支持 Builder 模式
- [ ] Object Copy Method 功能支持 Lambda 表达式
- [ ] JSON 转 Class(JSON To Class)
- [ ] Class 转 Protobuf IDL(JSON To Class)
diff --git a/build.gradle b/build.gradle
index 95e9899..196c339 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ plugins {
}
group 'cn.bigcoder.plugin'
-version '1.3.2'
+version '1.3.3'
repositories {
mavenCentral()
@@ -29,8 +29,9 @@ intellij {
}
patchPluginXml {
sinceBuild = '211'
+ untilBuild = '281'
changeNotes = """
- 1. fix feature:Resolve version compatibility issues and adapt new versions of the IDE
+ 1. new feature: Object Copy Method support builder mode.
"""
}
test {
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/ObjectCopyStrategyEnum.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/ObjectCopyStrategyEnum.java
new file mode 100644
index 0000000..2cb0bd6
--- /dev/null
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/ObjectCopyStrategyEnum.java
@@ -0,0 +1,17 @@
+package cn.bigcoder.plugin.objecthelper.common.enums;
+
+/**
+ * @author: Jindong.Tian
+ * @date: 2023-12-24
+ **/
+public enum ObjectCopyStrategyEnum {
+ /**
+ * 普通SET模式
+ */
+ SET,
+ /**
+ * builder模式
+ */
+ BUILDER,
+ ;
+}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/exception/ObjectHeplerPluginException.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/exception/ObjectHeplerPluginException.java
new file mode 100644
index 0000000..08d0c3d
--- /dev/null
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/exception/ObjectHeplerPluginException.java
@@ -0,0 +1,28 @@
+package cn.bigcoder.plugin.objecthelper.common.exception;
+
+/**
+ * @author: Jindong.Tian
+ * @date: 2024-05-05
+ **/
+public class ObjectHeplerPluginException extends RuntimeException{
+
+ public ObjectHeplerPluginException() {
+ }
+
+ public ObjectHeplerPluginException(String message) {
+ super(message);
+ }
+
+ public ObjectHeplerPluginException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ObjectHeplerPluginException(Throwable cause) {
+ super(cause);
+ }
+
+ public ObjectHeplerPluginException(String message, Throwable cause, boolean enableSuppression,
+ boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
+}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java
index c243b0e..c0cc060 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java
@@ -12,6 +12,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
@@ -25,6 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import org.jetbrains.annotations.Nullable;
/**
* @author: Jindong.Tian
@@ -153,6 +155,21 @@ public static String getMethodReturnClassName(PsiMethod psiMethod) {
return returnType.getPresentableText();
}
+ /**
+ * 获取PsiClass 名称
+ *
+ * @param psiClass
+ * @return
+ */
+ @Nullable
+ public static String getPsiClassName(PsiClass psiClass) {
+ PsiIdentifier nameIdentifier = psiClass.getNameIdentifier();
+ if (nameIdentifier == null) {
+ return null;
+ }
+ return nameIdentifier.getText();
+ }
+
/**
* 获取方法的参数列表
*
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java b/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java
index 39e1853..46b9b29 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java
@@ -50,6 +50,7 @@ public void apply() {
instance.setObjectCopySwitch(currentConfigModel.getObjectCopySwitch());
instance.setObjectCopyMethodFieldGenerateAnnotation(currentConfigModel.getObjectCopyMethodFieldGenerateAnnotation());
instance.setObjectCopyMethodFieldGenerateMode(currentConfigModel.getObjectCopyMethodFieldGenerateMode());
+ instance.setBuilderInstanceMethodName(currentConfigModel.getBuilderInstanceMethodName());
}
}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigModel.java b/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigModel.java
index 36d3b94..66fc4b6 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigModel.java
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigModel.java
@@ -37,6 +37,11 @@ public class PluginConfigModel {
*/
private WhetherEnum objectCopyMethodFieldGenerateAnnotation = WhetherEnum.YES;
+ /**
+ * Object Copy Method 功能中,使用builder模式生成拷贝代码时的判断依据,当目标对象类中包含正则所指定的方法,则默认按照builder模式生成,否则使用set模式生成
+ */
+ private String builderInstanceMethodName = ".*(builder|newBuilder).*";
+
public FunctionSwitchEnum getJsonSwitch() {
return jsonSwitch;
}
@@ -87,6 +92,14 @@ public void setObjectCopyMethodFieldGenerateAnnotation(
this.objectCopyMethodFieldGenerateAnnotation = objectCopyMethodFieldGenerateAnnotation;
}
+ public String getBuilderInstanceMethodName() {
+ return builderInstanceMethodName;
+ }
+
+ public void setBuilderInstanceMethodName(String builderInstanceMethodName) {
+ this.builderInstanceMethodName = builderInstanceMethodName;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
@@ -97,14 +110,15 @@ public boolean equals(Object o) {
}
PluginConfigModel that = (PluginConfigModel) o;
return jsonSwitch == that.jsonSwitch && thriftSwitch == that.thriftSwitch && xmlSwitch == that.xmlSwitch
- && objectCopySwitch == that.objectCopySwitch
- && objectCopyMethodFieldGenerateMode == that.objectCopyMethodFieldGenerateMode
- && objectCopyMethodFieldGenerateAnnotation == that.objectCopyMethodFieldGenerateAnnotation;
+ && objectCopySwitch == that.objectCopySwitch
+ && objectCopyMethodFieldGenerateMode == that.objectCopyMethodFieldGenerateMode
+ && objectCopyMethodFieldGenerateAnnotation == that.objectCopyMethodFieldGenerateAnnotation
+ && Objects.equals(builderInstanceMethodName, that.builderInstanceMethodName);
}
@Override
public int hashCode() {
return Objects.hash(jsonSwitch, thriftSwitch, xmlSwitch, objectCopySwitch, objectCopyMethodFieldGenerateMode,
- objectCopyMethodFieldGenerateAnnotation);
+ objectCopyMethodFieldGenerateAnnotation, builderInstanceMethodName);
}
}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/AbstractObjectCopyStrategy.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/AbstractObjectCopyStrategy.java
new file mode 100644
index 0000000..5e27888
--- /dev/null
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/AbstractObjectCopyStrategy.java
@@ -0,0 +1,98 @@
+package cn.bigcoder.plugin.objecthelper.generator.copy;
+
+import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getPsiClassName;
+
+import cn.bigcoder.plugin.objecthelper.common.enums.FieldGenerateModeEnum;
+import cn.bigcoder.plugin.objecthelper.common.enums.WhetherEnum;
+import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils;
+import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
+import cn.bigcoder.plugin.objecthelper.config.PluginConfigState;
+import cn.bigcoder.plugin.objecthelper.generator.Generator;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiField;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * @author: Jindong.Tian
+ * @date: 2024-05-05
+ **/
+public abstract class AbstractObjectCopyStrategy implements Generator {
+
+ /**
+ * 对象拷贝源对象类型
+ */
+ protected PsiClass sourceClass;
+ /**
+ * 对象拷贝目标对象类型
+ */
+ protected PsiClass targetClass;
+ /**
+ * 对象拷贝源参数名称
+ */
+ protected String sourceParamName;
+ /**
+ * 对象拷贝目标参数名称
+ */
+ protected String targetParamName;
+
+ public AbstractObjectCopyStrategy(PsiClass sourceClass, PsiClass targetClass, String sourceParamName) {
+ this.sourceClass = sourceClass;
+ this.targetClass = targetClass;
+ this.sourceParamName = sourceParamName;
+ // 生成参数名
+ this.targetParamName = StringUtils.firstLowerCase(Objects.requireNonNull(getPsiClassName(targetClass)));
+ // 防止方法入参和返回参数名称一致
+ if (sourceParamName.equals(this.targetParamName)) {
+ this.targetParamName = this.targetParamName + "Res";
+ }
+ }
+
+ @Override
+ public String generate() {
+ StringBuilder result = new StringBuilder();
+ // 生成前缀
+ result.append(generatePrefix());
+
+ // 字段copy模式
+ FieldGenerateModeEnum generateModeEnum = PluginConfigState.getInstance().getObjectCopyMethodFieldGenerateMode();
+
+ // mainClass 代表以哪个类字段为基础生成字段拷贝代码
+ PsiClass mainClass = targetClass;
+ PsiClass secondClass = sourceClass;
+ if (generateModeEnum == FieldGenerateModeEnum.SOURCE) {
+ // 字段拷贝使用源字段为蓝本拷贝
+ mainClass = sourceClass;
+ secondClass = targetClass;
+ }
+
+ Set secondFieldNames = PsiUtils.getAllPsiFields(secondClass).stream().filter(PsiUtils::isMemberField)
+ .map(PsiField::getName).collect(Collectors.toSet());
+
+ List annotationLine = new LinkedList<>();
+ for (PsiField field : PsiUtils.getAllPsiFields(mainClass)) {
+ if (!PsiUtils.isMemberField(field)) {
+ continue;
+ }
+ if (secondFieldNames.contains(field.getName())) {
+ result.append(generateFiledCopy(field));
+ } else if (PluginConfigState.getInstance().getObjectCopyMethodFieldGenerateAnnotation()
+ == WhetherEnum.YES) {
+ // 如果源对象没有该字段,且开启了以注释模式生成代码的开关,则生成注释
+ annotationLine.add("// " + generateFiledCopy(field));
+ }
+ }
+ annotationLine.forEach(result::append);
+ result.append(generateSuffix());
+ return result.toString();
+ }
+
+ protected abstract String generatePrefix();
+
+ protected abstract String generateFiledCopy(PsiField field);
+
+ protected abstract String generateSuffix();
+}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/BuilderObjectCopyStrategy.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/BuilderObjectCopyStrategy.java
new file mode 100644
index 0000000..fb72f61
--- /dev/null
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/BuilderObjectCopyStrategy.java
@@ -0,0 +1,60 @@
+package cn.bigcoder.plugin.objecthelper.generator.copy.strategy;
+
+import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.BLANK_SEPARATOR;
+import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.LINE_SEPARATOR;
+import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getPsiClassName;
+
+import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
+import cn.bigcoder.plugin.objecthelper.generator.copy.AbstractObjectCopyStrategy;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiField;
+
+/**
+ * @author: Jindong.Tian
+ * @date: 2024-05-05
+ **/
+public class BuilderObjectCopyStrategy extends AbstractObjectCopyStrategy {
+
+ public BuilderObjectCopyStrategy(PsiClass sourceClass, PsiClass targetClass, String sourceParamName) {
+ super(sourceClass, targetClass, sourceParamName);
+ }
+
+
+ /**
+ * 生成类似如下代码:
+ *
+ * if (user == null) {
+ * return null;
+ * }
+ * return UserDto.builder()
+ *
+ * @return
+ */
+ @Override
+ protected String generatePrefix() {
+ return generateNullCheck(sourceParamName) + LINE_SEPARATOR +
+ "return" + BLANK_SEPARATOR + getPsiClassName(targetClass) + ".builder()" + LINE_SEPARATOR;
+ }
+
+ @Override
+ protected String generateFiledCopy(PsiField field) {
+ return "." + field.getName() + "("
+ + sourceParamName + ".get" + StringUtils.firstUpperCase(field.getName()) + "())"
+ + LINE_SEPARATOR;
+ }
+
+
+ @Override
+ protected String generateSuffix() {
+ return ".build();" + LINE_SEPARATOR;
+ }
+
+ /**
+ * 生成示例:{@code if (user == null) {return null;}}
+ *
+ * @return
+ */
+ private String generateNullCheck(String sourceParamName) {
+ return "if(" + sourceParamName + "==null){return null;}";
+ }
+}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/SetObjectCopyStrategy.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/SetObjectCopyStrategy.java
new file mode 100644
index 0000000..48dc32a
--- /dev/null
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/copy/strategy/SetObjectCopyStrategy.java
@@ -0,0 +1,59 @@
+package cn.bigcoder.plugin.objecthelper.generator.copy.strategy;
+
+import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.BLANK_SEPARATOR;
+import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.LINE_SEPARATOR;
+import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.SEMICOLON_SEPARATOR;
+import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getPsiClassName;
+
+import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
+import cn.bigcoder.plugin.objecthelper.generator.copy.AbstractObjectCopyStrategy;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiField;
+
+/**
+ * @author: Jindong.Tian
+ * @date: 2024-05-05
+ **/
+public class SetObjectCopyStrategy extends AbstractObjectCopyStrategy {
+
+ public SetObjectCopyStrategy(PsiClass sourceClass, PsiClass targetClass, String sourceParamName) {
+ super(sourceClass, targetClass, sourceParamName);
+ }
+
+ /**
+ * 生成类似如下代码:
+ *
+ * if (user == null) {
+ * return null;
+ * }
+ * UserDto userDto = new UserDto();
+ *
+ * @return
+ */
+ @Override
+ protected String generatePrefix() {
+ String psiClassName = getPsiClassName(targetClass);
+ return generateNullCheck(sourceParamName) + LINE_SEPARATOR +
+ psiClassName + BLANK_SEPARATOR + targetParamName + "= new " + psiClassName + "();" + LINE_SEPARATOR;
+ }
+
+ @Override
+ protected String generateFiledCopy(PsiField field) {
+ return targetParamName + ".set" + StringUtils.firstUpperCase(field.getName()) + "(" + sourceParamName + ".get" + StringUtils.firstUpperCase(field.getName()) + "());" + LINE_SEPARATOR;
+ }
+
+
+ @Override
+ protected String generateSuffix() {
+ return "return " + targetParamName + SEMICOLON_SEPARATOR;
+ }
+
+ /**
+ * 生成示例:{@code if (user == null) {return null;}}
+ *
+ * @return
+ */
+ private String generateNullCheck(String sourceParamName) {
+ return "if(" + sourceParamName + "==null){return null;}";
+ }
+}
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java
index 0799f9a..63a4b78 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java
@@ -1,26 +1,16 @@
package cn.bigcoder.plugin.objecthelper.generator.method;
-import cn.bigcoder.plugin.objecthelper.common.enums.FieldGenerateModeEnum;
-import cn.bigcoder.plugin.objecthelper.common.enums.WhetherEnum;
-import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils;
-import cn.bigcoder.plugin.objecthelper.common.util.StringUtils;
+import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getPsiClass;
+
import cn.bigcoder.plugin.objecthelper.config.PluginConfigState;
import cn.bigcoder.plugin.objecthelper.generator.AbstractMethodGenerator;
+import cn.bigcoder.plugin.objecthelper.generator.copy.AbstractObjectCopyStrategy;
+import cn.bigcoder.plugin.objecthelper.generator.copy.strategy.BuilderObjectCopyStrategy;
+import cn.bigcoder.plugin.objecthelper.generator.copy.strategy.SetObjectCopyStrategy;
import com.intellij.psi.PsiClass;
-import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
-import com.intellij.psi.PsiModifier;
-import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiParameter;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
-import org.jetbrains.annotations.NotNull;
-
-import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.*;
-import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getMethodReturnClassName;
-import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getPsiClass;
+import java.util.regex.Pattern;
/**
* @author: Jindong.Tian
@@ -32,10 +22,6 @@ public class ObjectCopyMethodGenerator extends AbstractMethodGenerator {
* 方法第一个参数名称
*/
private String firstParameterName;
- /**
- * 方法返回局部变量名称
- */
- private String returnObjName;
private void init(PsiMethod psiMethod) {
if (psiMethod == null) {
@@ -44,11 +30,6 @@ private void init(PsiMethod psiMethod) {
super.project = psiMethod.getProject();
super.psiMethod = psiMethod;
this.firstParameterName = getFirstParameter().getName();
- this.returnObjName = StringUtils.firstLowerCase(getMethodReturnClassName(psiMethod));
- // 防止方法入参和返回参数名称一致
- if (firstParameterName.equals(returnObjName)) {
- this.returnObjName = this.returnObjName + "1";
- }
}
public static ObjectCopyMethodGenerator getInstance(PsiMethod psiMethod) {
@@ -64,78 +45,23 @@ public static ObjectCopyMethodGenerator getInstance(PsiMethod psiMethod) {
*/
@Override
protected String generateMethodBody() {
- StringBuilder result = new StringBuilder();
- result.append(generateNullCheck());
- result.append(generateObjectCreateLine());
- FieldGenerateModeEnum generateModeEnum = PluginConfigState.getInstance()
- .getObjectCopyMethodFieldGenerateMode();
- // mainClass 代表以哪个类字段为基础生成字段拷贝代码
- PsiClass mainClass = getReturnClass();
- PsiClass secondClass = getFirstParameterClass();
- if (generateModeEnum == FieldGenerateModeEnum.SOURCE) {
- mainClass = getFirstParameterClass();
- secondClass = getReturnClass();
- }
-
- Set secondFieldNames = PsiUtils.getAllPsiFields(secondClass).stream().filter(e -> PsiUtils.isMemberField(e))
- .map(PsiField::getName).collect(Collectors.toSet());
+ AbstractObjectCopyStrategy copyStrategy = getCopyStrategy();
+ return copyStrategy.generate();
+ }
- List annotationLine = new LinkedList<>();
- for (PsiField field : PsiUtils.getAllPsiFields(mainClass)) {
- if (!PsiUtils.isMemberField(field)) {
- continue;
- }
- if (secondFieldNames.contains(field.getName())) {
- result.append(generateFieldCopyLine(field));
- } else if (PluginConfigState.getInstance().getObjectCopyMethodFieldGenerateAnnotation()
- == WhetherEnum.YES) {
- // 如果源对象没有该字段,且开启了以注释模式生成代码的开关,则生成注释
- annotationLine.add("// "+ generateFieldCopyLine(field));
+ private AbstractObjectCopyStrategy getCopyStrategy() {
+ // mainClass 代表以哪个类字段为基础生成字段拷贝代码
+ PsiClass returnClass = getReturnClass();
+ PsiClass sourceClass = getFirstParameterClass();
+ String builderRegex = PluginConfigState.getInstance().getBuilderInstanceMethodName();
+
+ Pattern pattern = Pattern.compile(builderRegex);
+ for (PsiMethod method : returnClass.getMethods()) {
+ if (pattern.matcher(method.getName()).matches()) {
+ return new BuilderObjectCopyStrategy(sourceClass, returnClass, this.firstParameterName);
}
}
- annotationLine.forEach(result::append);
- result.append(generateReturnLine());
- return result.toString();
- }
-
- /**
- * 生成示例:{@code UserDTO userDTO = new UserDTO();}
- *
- * @return
- */
- @NotNull
- private String generateObjectCreateLine() {
- return getMethodReturnClassName(psiMethod) + BLANK_SEPARATOR + returnObjName + "= new " + getMethodReturnClassName(psiMethod) + "();" + LINE_SEPARATOR;
- }
-
- /**
- * 生成示例:{@code userDTO.setId(user.getId());}
- *
- * @param field
- * @return
- */
- @NotNull
- private String generateFieldCopyLine(PsiField field) {
- return returnObjName + ".set" + StringUtils.firstUpperCase(field.getName()) + "(" + firstParameterName + ".get" + StringUtils.firstUpperCase(field.getName()) + "());" + LINE_SEPARATOR;
- }
-
- /**
- * 生成示例:{@code return userDTO;}
- *
- * @return
- */
- @NotNull
- private String generateReturnLine() {
- return "return " + returnObjName + SEMICOLON_SEPARATOR;
- }
-
- /**
- * 生成示例:{@code if (user == null) {return null;}}
- *
- * @return
- */
- private String generateNullCheck() {
- return "if(" + getFirstParameter().getName() + "==null){return null;}";
+ return new SetObjectCopyStrategy(sourceClass, returnClass, this.firstParameterName);
}
/**
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form
index 7e08139..ec970d0 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form
@@ -14,6 +14,8 @@
+
+
@@ -181,6 +183,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java
index 033ca7e..985abb4 100644
--- a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java
+++ b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java
@@ -7,7 +7,6 @@
import cn.bigcoder.plugin.objecthelper.config.PluginConfigModel;
import java.util.Optional;
-import java.util.function.Function;
import javax.swing.*;
/**
@@ -24,6 +23,7 @@ public class ConfigPage {
private JButton tipsButton;
private JComboBox objectCopyMethodGenerateMode;
private JComboBox objectCopyMethodGenerateAnnotation;
+ private JTextField builderInstanceMethodName;
public JPanel getMainPanel() {
initField();
@@ -41,6 +41,7 @@ public void initField() {
this.objectCopyMethodSwitch.setSelectedItem(instance.getObjectCopySwitch().getCode());
this.objectCopyMethodGenerateAnnotation.setSelectedItem(instance.getObjectCopyMethodFieldGenerateAnnotation().getCode());
this.objectCopyMethodGenerateMode.setSelectedItem(instance.getObjectCopyMethodFieldGenerateMode().getCode());
+ this.builderInstanceMethodName.setText(instance.getBuilderInstanceMethodName());
}
/**
@@ -75,6 +76,9 @@ public PluginConfigModel getCurrentConfigModel() {
pluginConfigModel.setObjectCopyMethodFieldGenerateAnnotation(WhetherEnum.nameOf(e.toString()));
}
);
+ Optional.ofNullable(this.builderInstanceMethodName.getText()).ifPresent(
+ pluginConfigModel::setBuilderInstanceMethodName
+ );
return pluginConfigModel;
}