diff --git a/.gitignore b/.gitignore index 154f8bc..15a3152 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.class .idea/ out/ +.gradle/ +build/ diff --git a/README.md b/README.md index ccf3df9..2e927f6 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,70 @@ # object-helper-plugin -![](https://img.shields.io/badge/version-v1.2.0-blue) -![](https://img.shields.io/badge/license-Apache%202-red) -![](https://img.shields.io/badge/size-36.43%20kB-yellowgreen) -![](https://img.shields.io/badge/download-500%2B-green) -插件地址:[https://plugins.jetbrains.com/plugin/15788-objecthelper](https://plugins.jetbrains.com/plugin/15788-objecthelper) +
+ +
+
+
+ + + + +
-JetBrains Intellij IDEA ObjectHelper插件,包含以下功能: +JetBrains Intellij IDEA ObjectHelper 插件旨在减少开发者重复低效的劳动,使开发者能够更专注于业务逻辑的开发。 + +该插件包含以下功能: - 对象拷贝 + set模式: ![](https://image.bigcoder.cn/7fce876e-fa94-4780-bb14-584068c35963.gif) + 对象拷贝的快捷键默认是 `Alt+Insert`,如果该快捷键无效,可以在settings->keymap中搜索“Generate”关键字查看具体的快捷键: + + ![](https://image.bigcoder.cn/20220916173117.png) + + 当对象中包含`builder` 或者 `newBuilder`方法时,则插件默认会采用 builder 模式生成代码: + + ![](https://image.bigcoder.cn/20240505142735.gif) + + 如果你的builder类生成的方法名与插件默认生成的不同,可以在设置中更改: + + ![](https://image.bigcoder.cn/20240505143027.png) + - Java类转JSON - - ![](https://image.bigcoder.cn/20210227223302.gif) - -- Java类转IDL + + ![](https://image.bigcoder.cn/20231224171155.gif) + +- Java类转Thrift IDL + ![](https://image.bigcoder.cn/6eee7a02-8e4e-4f11-9b8c-81d661a920c5.gif) - + +- Java类转XML + + ![](https://image.bigcoder.cn/20231224171113.gif) + +- 插件配置 + +File->Settings->Tools->Object Helper 即可进入插件的配置页面 + +![](https://image.bigcoder.cn/20231224170305.png) + +- `generate field mode = target` 代表以方法返回类型的字段为基础生成对象拷贝; + `generate field mode = source` 代表以方法入参类型的字段为基础生成对象拷贝。 + +- `non-existent field generate annotation = yes` 代表当目标字段在源对象中不存在时,是否以注释的形式生成代码,如果为 `no`,则代表不生成这一个字段拷贝代码。 + ## 未来功能支持计划 object-helper插件未来功能支持计划: -- [x] Class转IDL(Class To IDL) -- [ ] Class转XML(Class To XML) -- [ ] JSON转Class(JSON TO Class) +- [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) - [ ] All Setter - [ ] 菜单分组显示 -- [ ] 个性化配置 \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..196c339 --- /dev/null +++ b/build.gradle @@ -0,0 +1,39 @@ +plugins { + // https://github.com/JetBrains/gradle-intellij-plugin/blob/master/README.md + id 'org.jetbrains.intellij' version '1.16.1' + id 'java' +} + +group 'cn.bigcoder.plugin' +version '1.3.3' + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.apache.commons:commons-lang3:3.13.0' + / object 转 xml 依赖 开始/ + implementation 'com.fasterxml.jackson.core:jackson-core:2.13.3' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3' + implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.3' + / object 转 xml 依赖 结束/ + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' +} + +// See https://github.com/JetBrains/gradle-intellij-plugin/ +intellij { + version = '2023.3.1' + plugins = ['com.intellij.java'] +} +patchPluginXml { + sinceBuild = '211' + untilBuild = '281' + changeNotes = """ + 1. new feature: Object Copy Method support builder mode. + """ +} +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..69a9715 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..744e882 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MSYS* | MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/object-helper.jar b/object-helper.jar deleted file mode 100644 index 79aa458..0000000 Binary files a/object-helper.jar and /dev/null differ diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml deleted file mode 100644 index 78bf8fb..0000000 --- a/resources/META-INF/plugin.xml +++ /dev/null @@ -1,51 +0,0 @@ - - cn.bigcoder.plugin.objecthelper - ObjectHelper - 1.2.0 - HearingSmile - - GitHub -
  • Copy the object
  • - -
  • Class to JSON
  • - -
  • Class to IDL
  • - - ]]>
    - - New feature:Added the ability to quickly convert Java classes to IDL - ]]> - - - - - - - - com.intellij.modules.java - - - - - - - - - - - - - - - - - - -
    \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..66ce9df --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'object-helper-plugin' + diff --git a/src/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java b/src/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java deleted file mode 100644 index fc7bb62..0000000 --- a/src/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java +++ /dev/null @@ -1,105 +0,0 @@ -package cn.bigcoder.plugin.objecthelper.generator.json; - -import cn.bigcoder.plugin.objecthelper.common.util.PsiTypeUtils; -import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils; -import cn.bigcoder.plugin.objecthelper.generator.Generator; -import com.google.common.collect.Maps; -import com.google.gson.Gson; -import com.intellij.psi.PsiArrayType; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiField; -import com.intellij.psi.PsiType; -import com.intellij.psi.impl.source.PsiClassReferenceType; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.compress.utils.Lists; -import org.apache.commons.compress.utils.Sets; -import org.apache.commons.lang.ArrayUtils; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * @author: Jindong.Tian - * @date: 2021-02-11 - **/ -public class ClassJsonGenerator implements Generator { - - private PsiClass psiClass; - - /** - * 保存已经解析过的自定义类型名称,防止出现递归嵌套的情况 - */ - private Set recursiveCache = Sets.newHashSet(); - - private Gson gson; - - private ClassJsonGenerator(PsiClass psiClass, Gson gson) { - this.psiClass = psiClass; - this.gson = gson; - } - - /** - * 获取ClassJsonGenerator实例 - * - * @param psiClass JSON生成的目标类 - * @param gson Gson对象 - * @return - */ - public static ClassJsonGenerator getInstance(PsiClass psiClass, Gson gson) { - return new ClassJsonGenerator(psiClass, gson); - } - - @Override - public String generate() { - Map jsonMap = processFields(psiClass); - return gson.toJson(jsonMap); - } - - private Map processFields(PsiClass psiClass) { - Map result = Maps.newLinkedHashMap(); - // 当前类所有字段 - List allPsiFields = PsiUtils.getAllPsiFields(psiClass); - if (CollectionUtils.isEmpty(allPsiFields)) { - return result; - } - for (PsiField psiField : allPsiFields) { - result.put(psiField.getName(), processField(psiField.getType())); - } - return result; - } - - private Object processField(PsiType psiType) { - Object defaultValue = null; - if (PsiTypeUtils.isDataType(psiType)) { - //如果是数据类型 - defaultValue = PsiTypeUtils.getDataTypeDefaultValue(psiType.getCanonicalText()); - } else if (PsiTypeUtils.isArrayType(psiType)) { - //如果是数组类型 - List list = Lists.newArrayList(); - PsiClass arrayContentClass = PsiUtils.getPsiClass(((PsiArrayType) psiType).getComponentType(), psiClass.getProject()); - list.add(PsiTypeUtils.getDataTypeDefaultValue(arrayContentClass.getQualifiedName())); - defaultValue = list; - } else if (PsiTypeUtils.isCollectionType(psiType)) { - defaultValue = Lists.newArrayList(); - //如果是集合类型 - PsiType[] parameters = ((PsiClassReferenceType) psiType).getParameters(); - if (ArrayUtils.isEmpty(parameters)) { - return defaultValue; - } - //获取泛型 - PsiType genericType = parameters[0]; - ((List) defaultValue).add(processField(genericType)); - } else if (!PsiTypeUtils.isJavaOfficialType(psiType)) { - //如果是自定义类型 - if (recursiveCache.contains(psiType.getCanonicalText())) { - //出现递归嵌套 - return null; - } - recursiveCache.add(psiType.getCanonicalText()); - //如果是自定义类 - defaultValue = processFields(PsiUtils.getPsiClass(psiType, psiClass.getProject())); - } - return defaultValue; - } -} \ No newline at end of file diff --git a/src/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java b/src/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java deleted file mode 100644 index 921e9d7..0000000 --- a/src/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java +++ /dev/null @@ -1,128 +0,0 @@ -package cn.bigcoder.plugin.objecthelper.generator.method; - -import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils; -import cn.bigcoder.plugin.objecthelper.common.util.StringUtils; -import com.intellij.psi.*; -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; - -/** - * @author: Jindong.Tian - * @date: 2021-01-09 - **/ -public class ObjectCopyMethodGenerator extends AbstractMethodGenerator { - - /** - * 方法第一个参数名称 - */ - private String firstParameterName; - /** - * 方法返回局部变量名称 - */ - private String returnObjName; - - private void init(PsiMethod psiMethod) { - if (psiMethod == null) { - return; - } - 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) { - ObjectCopyMethodGenerator objectCopyMethodGenerator = new ObjectCopyMethodGenerator(); - objectCopyMethodGenerator.init(psiMethod); - return objectCopyMethodGenerator; - } - - /** - * 此方法中不应该存在判空的操作,依赖环境的建议重写父类的check方法,在生成早期拦截异常情况 - * - * @return - */ - @Override - protected String generateMethodBody() { - StringBuilder result = new StringBuilder(); - result.append(generateNullCheck()); - result.append(generateObjectCreateLine()); - for (PsiField field : PsiUtils.getAllPsiFields(getFirstParameterClass())) { - PsiModifierList modifierList = field.getModifierList(); - if (modifierList == null || - modifierList.hasModifierProperty(PsiModifier.STATIC) || - modifierList.hasModifierProperty(PsiModifier.FINAL) || - modifierList.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { - continue; - } - result.append(generateFieldCopyLine(field)); - } - 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;}"; - } - - /** - * 获取参数列表第一个参数的{@code PsiParameter} - * - * @return - */ - private PsiParameter getFirstParameter() { - return getParameters().get(FIRST_INDEX); - } - - /** - * 获取参数列表第一个参数的{@code PsiClass} - * - * @return - */ - private PsiClass getFirstParameterClass() { - return getPsiClass(getFirstParameter().getType(), project); - } -} \ No newline at end of file diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/action/AbstractClassAnAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/AbstractClassAnAction.java new file mode 100644 index 0000000..365eeb0 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/action/AbstractClassAnAction.java @@ -0,0 +1,28 @@ +package cn.bigcoder.plugin.objecthelper.action; + +import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.setActionInvisible; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import org.jetbrains.annotations.NotNull; + +/** + * @author: Jindong.Tian + * @date: 2023-12-24 + **/ +public abstract class AbstractClassAnAction extends AnAction { + + /** + * 是否开启该功能 + * @return true代表开启该功能,false表示关闭该功能 + */ + public abstract boolean actionShow(AnActionEvent anActionEvent); + + @Override + public void update(@NotNull AnActionEvent anActionEvent) { + if (!actionShow(anActionEvent)) { + setActionInvisible(anActionEvent); + } + super.update(anActionEvent); + } +} diff --git a/src/cn/bigcoder/plugin/objecthelper/action/ClassToFormatJsonAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToFormatJsonAction.java similarity index 100% rename from src/cn/bigcoder/plugin/objecthelper/action/ClassToFormatJsonAction.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToFormatJsonAction.java diff --git a/src/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java similarity index 67% rename from src/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java index 7619892..8e96e8c 100644 --- a/src/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToJsonAction.java @@ -1,22 +1,21 @@ package cn.bigcoder.plugin.objecthelper.action; +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; import cn.bigcoder.plugin.objecthelper.common.util.NotificationUtils; +import cn.bigcoder.plugin.objecthelper.config.PluginConfigState; import cn.bigcoder.plugin.objecthelper.generator.Generator; import cn.bigcoder.plugin.objecthelper.generator.json.ClassJsonGenerator; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.psi.PsiClass; -import org.jetbrains.annotations.NotNull; import java.awt.datatransfer.StringSelection; import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getOperatePsiClass; -import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.setActionInvisible; -public class ClassToJsonAction extends AnAction { +public class ClassToJsonAction extends AbstractClassAnAction { @Override public void actionPerformed(AnActionEvent anAction) { @@ -26,16 +25,13 @@ public void actionPerformed(AnActionEvent anAction) { } String json = getGenerator(psiClass).generate(); CopyPasteManager.getInstance().setContents(new StringSelection(json)); - NotificationUtils.notifyInfo(anAction.getProject(), "JSON字符串成功置入剪贴板:
    " + json); + NotificationUtils.notifyInfo(anAction.getProject(), "JSON字符串成功置入剪贴板:
    " + json); } @Override - public void update(@NotNull AnActionEvent anActionEvent) { - // 如果当前光标不在方法中,则不显示ConvertToJson组件 - if (getOperatePsiClass(anActionEvent) == null) { - setActionInvisible(anActionEvent); - } - super.update(anActionEvent); + public boolean actionShow(AnActionEvent anActionEvent) { + return PluginConfigState.getInstance().getJsonSwitch() == FunctionSwitchEnum.OPEN + && getOperatePsiClass(anActionEvent) != null; } protected Generator getGenerator(PsiClass psiClass) { diff --git a/src/cn/bigcoder/plugin/objecthelper/action/ClassToIDLAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToThriftIDLAction.java similarity index 56% rename from src/cn/bigcoder/plugin/objecthelper/action/ClassToIDLAction.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToThriftIDLAction.java index aff3631..d25b2bd 100644 --- a/src/cn/bigcoder/plugin/objecthelper/action/ClassToIDLAction.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToThriftIDLAction.java @@ -1,8 +1,9 @@ package cn.bigcoder.plugin.objecthelper.action; +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; import cn.bigcoder.plugin.objecthelper.common.util.NotificationUtils; -import cn.bigcoder.plugin.objecthelper.generator.idl.IDLGenerator; -import com.intellij.openapi.actionSystem.AnAction; +import cn.bigcoder.plugin.objecthelper.config.PluginConfigState; +import cn.bigcoder.plugin.objecthelper.generator.idl.ThriftIDLGenerator; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.psi.PsiClass; @@ -11,30 +12,26 @@ import java.awt.datatransfer.StringSelection; import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getOperatePsiClass; -import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.setActionInvisible; /** * @author: Jindong.Tian * @date: 2021-08-21 **/ -public class ClassToIDLAction extends AnAction { +public class ClassToThriftIDLAction extends AbstractClassAnAction { @Override public void actionPerformed(@NotNull AnActionEvent anActionEvent) { PsiClass psiClass = getOperatePsiClass(anActionEvent); if (psiClass == null) { return; } - String idl = IDLGenerator.getInstance(psiClass).generate(); + String idl = ThriftIDLGenerator.getInstance(psiClass).generate(); CopyPasteManager.getInstance().setContents(new StringSelection(idl)); - NotificationUtils.notifyInfo(anActionEvent.getProject(), "IDL代码成功置入剪贴板:
    " + idl); + NotificationUtils.notifyInfo(anActionEvent.getProject(), "Thrift IDL代码成功置入剪贴板:
    " + idl); } @Override - public void update(@NotNull AnActionEvent anActionEvent) { - // 如果当前光标不在方法中,则不显示ConvertToJson组件 - if (getOperatePsiClass(anActionEvent) == null) { - setActionInvisible(anActionEvent); - } - super.update(anActionEvent); + public boolean actionShow(@NotNull AnActionEvent anActionEvent) { + return PluginConfigState.getInstance().getThriftSwitch() == FunctionSwitchEnum.OPEN + && getOperatePsiClass(anActionEvent) != null; } } \ No newline at end of file diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToXMLAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToXMLAction.java new file mode 100644 index 0000000..82cb64c --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ClassToXMLAction.java @@ -0,0 +1,40 @@ +package cn.bigcoder.plugin.objecthelper.action; + +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; +import cn.bigcoder.plugin.objecthelper.common.util.NotificationUtils; +import cn.bigcoder.plugin.objecthelper.config.PluginConfigState; +import cn.bigcoder.plugin.objecthelper.generator.Generator; +import cn.bigcoder.plugin.objecthelper.generator.xml.ClassXMLGenerator; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.ide.CopyPasteManager; +import com.intellij.psi.PsiClass; + +import groovy.json.StringEscapeUtils; +import java.awt.datatransfer.StringSelection; +import org.jetbrains.annotations.NotNull; + +import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getOperatePsiClass; + +public class ClassToXMLAction extends AbstractClassAnAction { + + @Override + public void actionPerformed(AnActionEvent anAction) { + PsiClass psiClass = getOperatePsiClass(anAction); + if (psiClass == null) { + return; + } + String xmlStr = getGenerator(psiClass).generate(); + CopyPasteManager.getInstance().setContents(new StringSelection(xmlStr)); + NotificationUtils.notifyInfo(anAction.getProject(), "XML字符串成功置入剪贴板"); + } + + @Override + public boolean actionShow(@NotNull AnActionEvent anActionEvent) { + return PluginConfigState.getInstance().getXmlSwitch() == FunctionSwitchEnum.OPEN + && getOperatePsiClass(anActionEvent) != null; + } + + protected Generator getGenerator(PsiClass psiClass) { + return ClassXMLGenerator.getInstance(psiClass); + } +} diff --git a/src/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java similarity index 75% rename from src/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java index 2ab5a9d..8b8fe9e 100644 --- a/src/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java @@ -1,20 +1,20 @@ package cn.bigcoder.plugin.objecthelper.action; +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; 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 cn.bigcoder.plugin.objecthelper.generator.method.ObjectCopyMethodGenerator; -import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.PsiElementFactory; import com.intellij.psi.PsiMethod; -import org.jetbrains.annotations.NotNull; import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.VOID; -public class ObjectCopyMethodAction extends AnAction { +public class ObjectCopyMethodAction extends AbstractClassAnAction { @Override public void actionPerformed(AnActionEvent anActionEvent) { @@ -24,12 +24,9 @@ public void actionPerformed(AnActionEvent anActionEvent) { } @Override - public void update(@NotNull AnActionEvent anActionEvent) { - // 如果当前光标不在方法中,则不显示Object Copy组件 - if (!check(PsiUtils.getCursorPsiMethod(anActionEvent))) { - PsiUtils.setActionDisabled(anActionEvent); - } - super.update(anActionEvent); + public boolean actionShow(AnActionEvent anActionEvent) { + return PluginConfigState.getInstance().getObjectCopySwitch() == FunctionSwitchEnum.OPEN + && check(PsiUtils.getCursorPsiMethod(anActionEvent)); } private void generateO2O(PsiMethod psiMethod) { @@ -59,8 +56,8 @@ private void generateO2O(PsiMethod psiMethod) { */ private boolean check(PsiMethod psiMethod) { if (psiMethod == null - || PsiUtils.getPsiParameters(psiMethod).size() == 0 - || VOID.equals(PsiUtils.getMethodReturnClassName(psiMethod))) { + || PsiUtils.getPsiParameters(psiMethod).isEmpty() + || VOID.equals(PsiUtils.getMethodReturnClassName(psiMethod))) { return false; } return true; diff --git a/src/cn/bigcoder/plugin/objecthelper/common/constant/IDLTypeName.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/IDLTypeName.java similarity index 100% rename from src/cn/bigcoder/plugin/objecthelper/common/constant/IDLTypeName.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/IDLTypeName.java diff --git a/src/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java similarity index 73% rename from src/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java index 9f18611..297cf03 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/JavaClassName.java @@ -15,6 +15,13 @@ public class JavaClassName { public static final String DATE_TYPE = "java.util.Date"; public static final String LOCAL_DATE_TYPE = "java.time.LocalDate"; public static final String LOCAL_DATE_TIME_TYPE = "java.time.LocalDateTime"; + public static final String BIG_DECIMAL = "java.math.BigDecimal"; + public static final String BASE_INT_TYPE = "int"; + public static final String BASE_LONG_TYPE = "long"; + public static final String BASE_SHORT_TYPE = "short"; + public static final String BASE_BYTE_TYPE = "byte"; + public static final String BASE_DOUBLE_TYPE = "double"; + public static final String BASE_FLOAT_TYPE = "float"; public static final String COLLECTION_TYPE = "java.util.Collection"; public static final String MAP_TYPE = "java.util.Map"; diff --git a/src/cn/bigcoder/plugin/objecthelper/common/constant/JavaKeyWord.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/JavaKeyWord.java similarity index 100% rename from src/cn/bigcoder/plugin/objecthelper/common/constant/JavaKeyWord.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/constant/JavaKeyWord.java diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/CommonEnum.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/CommonEnum.java new file mode 100644 index 0000000..f470499 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/CommonEnum.java @@ -0,0 +1,11 @@ +package cn.bigcoder.plugin.objecthelper.common.enums; + +/** + * @author: Jindong.Tian + * @date: 2023-12-24 + * @description: + **/ +public interface CommonEnum { + + String getCode(); +} diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FieldGenerateModeEnum.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FieldGenerateModeEnum.java new file mode 100644 index 0000000..4ff4899 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FieldGenerateModeEnum.java @@ -0,0 +1,34 @@ +package cn.bigcoder.plugin.objecthelper.common.enums; + +/** + * @author: Jindong.Tian + * @date: 2023-12-24 + **/ +public enum FieldGenerateModeEnum implements CommonEnum { + SOURCE("source"), + TARGET("target"), + ; + private String code; + + FieldGenerateModeEnum(String code) { + this.code = code; + } + + + public static FieldGenerateModeEnum nameOf(String modify) { + if (modify == null) { + return null; + } + for (FieldGenerateModeEnum item : values()) { + if (modify.equals(item.getCode())) { + return item; + } + } + return null; + } + + @Override + public String getCode() { + return code; + } +} diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FunctionSwitchEnum.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FunctionSwitchEnum.java new file mode 100644 index 0000000..866c40f --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/FunctionSwitchEnum.java @@ -0,0 +1,32 @@ +package cn.bigcoder.plugin.objecthelper.common.enums; + +/** + * @author: Jindong.Tian + * @date: 2023-12-24 + **/ +public enum FunctionSwitchEnum implements CommonEnum { + OPEN("open"), + CLOSE("close"), + ; + private String code; + + FunctionSwitchEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static FunctionSwitchEnum nameOf(String modify) { + if (modify == null) { + return null; + } + for (FunctionSwitchEnum item : values()) { + if (modify.equals(item.getCode())) { + return item; + } + } + return null; + } +} diff --git a/src/cn/bigcoder/plugin/objecthelper/common/enums/JavaModify.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/JavaModifyEnum.java similarity index 77% rename from src/cn/bigcoder/plugin/objecthelper/common/enums/JavaModify.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/JavaModifyEnum.java index f5d106a..c599104 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/enums/JavaModify.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/JavaModifyEnum.java @@ -4,7 +4,7 @@ * @author: Jindong.Tian * @date: 2021-01-09 **/ -public enum JavaModify { +public enum JavaModifyEnum { PUBLIC("public", 1), PROTECTED("protected", 1), PRIVATE("private", 1), @@ -14,7 +14,7 @@ public enum JavaModify { private String name; private Integer priority; - JavaModify(String name, Integer priority) { + JavaModifyEnum(String name, Integer priority) { this.name = name; this.priority = priority; } @@ -23,11 +23,11 @@ public String getName() { return name; } - public JavaModify nameOf(String modify){ + public JavaModifyEnum nameOf(String modify){ if (modify == null){ return null; } - for (JavaModify item : values()) { + for (JavaModifyEnum item : values()) { if (modify.equals(item.getName())){ return item; } 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/enums/WhetherEnum.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/WhetherEnum.java new file mode 100644 index 0000000..918c484 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/enums/WhetherEnum.java @@ -0,0 +1,32 @@ +package cn.bigcoder.plugin.objecthelper.common.enums; + +/** + * @author: Jindong.Tian + * @date: 2023-12-24 + **/ +public enum WhetherEnum implements CommonEnum { + YES("yes"), + NO("no"), + ; + private String code; + + WhetherEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static WhetherEnum nameOf(String modify) { + if (modify == null) { + return null; + } + for (WhetherEnum item : values()) { + if (modify.equals(item.getCode())) { + return item; + } + } + return null; + } +} 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/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java similarity index 90% rename from src/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java index 3264f6b..6e1f698 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/IDLUtils.java @@ -1,7 +1,7 @@ package cn.bigcoder.plugin.objecthelper.common.util; -import static cn.bigcoder.plugin.objecthelper.common.constant.JavaClassName.*; import static cn.bigcoder.plugin.objecthelper.common.constant.IDLTypeName.*; +import static cn.bigcoder.plugin.objecthelper.common.constant.JavaClassName.*; /** * @author: Jindong.Tian @@ -20,25 +20,22 @@ public static String convertJavaTypeToIDLType(String canonicalText){ } switch (canonicalText) { case STRING_TYPE: + case BIG_DECIMAL: return STRING; + case BYTE_TYPE: + return BYTE; + case SHORT_TYPE: + return I16; case INTEGER_TYPE: return I32; case LONG_TYPE: + case LOCAL_DATE_TYPE: + case DATE_TYPE: + case LOCAL_DATE_TIME_TYPE: return I64; - case SHORT_TYPE: - return I16; - case BYTE_TYPE: - return BYTE; case DOUBLE_TYPE: - return DOUBLE; case FLOAT_TYPE: return DOUBLE; - case DATE_TYPE: - return I64; - case LOCAL_DATE_TYPE: - return I64; - case LOCAL_DATE_TIME_TYPE: - return I64; default: return null; } diff --git a/src/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java similarity index 86% rename from src/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java index 78b2453..f0b9d48 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/NotificationUtils.java @@ -1,7 +1,7 @@ package cn.bigcoder.plugin.objecthelper.common.util; -import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.NotificationGroup; +import com.intellij.notification.NotificationGroupManager; import com.intellij.notification.NotificationType; import com.intellij.openapi.project.Project; @@ -14,7 +14,8 @@ public class NotificationUtils { /** * 从2020.3版本方式,通知组改由Plugin.xml注册。详见:https://plugins.jetbrains.com/docs/intellij/notifications.html#top-level-notifications */ - private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup("ObjectHelper Notification Group", NotificationDisplayType.BALLOON, true); + private static final NotificationGroup NOTIFICATION_GROUP = NotificationGroupManager.getInstance() + .getNotificationGroup("ObjectHelper Notification Group"); public static void notifyInfo(Project project, String content) { NOTIFICATION_GROUP.createNotification(content, NotificationType.INFORMATION).notify(project); diff --git a/src/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java similarity index 76% rename from src/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java index 9286362..9dccef7 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiTypeUtils.java @@ -4,14 +4,17 @@ import com.intellij.psi.PsiClass; import com.intellij.psi.PsiClassType; import com.intellij.psi.PsiType; -import org.apache.commons.lang.time.DateFormatUtils; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import org.apache.commons.lang3.time.DateFormatUtils; import static cn.bigcoder.plugin.objecthelper.common.constant.JavaClassName.*; +import static cn.bigcoder.plugin.objecthelper.common.constant.JavaClassName.BASE_BYTE_TYPE; /** * @author: Jindong.Tian @@ -19,6 +22,29 @@ **/ public class PsiTypeUtils { + private static final Set DATA_TYPES; + + static { + DATA_TYPES = new HashSet<>(); + DATA_TYPES.add(STRING_TYPE); + DATA_TYPES.add(INTEGER_TYPE); + DATA_TYPES.add(BASE_INT_TYPE); + DATA_TYPES.add(LONG_TYPE); + DATA_TYPES.add(BASE_LONG_TYPE); + DATA_TYPES.add(SHORT_TYPE); + DATA_TYPES.add(BASE_SHORT_TYPE); + DATA_TYPES.add(BYTE_TYPE); + DATA_TYPES.add(BASE_BYTE_TYPE); + DATA_TYPES.add(DOUBLE_TYPE); + DATA_TYPES.add(BASE_DOUBLE_TYPE); + DATA_TYPES.add(FLOAT_TYPE); + DATA_TYPES.add(BASE_FLOAT_TYPE); + DATA_TYPES.add(DATE_TYPE); + DATA_TYPES.add(LOCAL_DATE_TYPE); + DATA_TYPES.add(LOCAL_DATE_TIME_TYPE); + DATA_TYPES.add(BIG_DECIMAL); + } + /** * 获取数据类型的默认值 * @@ -33,17 +59,23 @@ public static Object getDataTypeDefaultValue(String canonicalText) { case STRING_TYPE: return ""; case INTEGER_TYPE: + case BASE_INT_TYPE: return 1; case LONG_TYPE: + case BASE_LONG_TYPE: return 1L; case SHORT_TYPE: + case BASE_SHORT_TYPE: return (short) 1; case BYTE_TYPE: + case BASE_BYTE_TYPE: return (byte) 1; case DOUBLE_TYPE: - return 1.0; + case BASE_DOUBLE_TYPE: + return 1.1; case FLOAT_TYPE: - return 1.0f; + case BASE_FLOAT_TYPE: + return 1.1f; case DATE_TYPE: return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); case LOCAL_DATE_TYPE: @@ -62,20 +94,7 @@ public static Object getDataTypeDefaultValue(String canonicalText) { * @return */ public static boolean isDataType(PsiType psiType) { - String canonicalName = psiType.getCanonicalText(); - if (STRING_TYPE.equals(canonicalName) - || INTEGER_TYPE.equals(canonicalName) - || LONG_TYPE.equals(canonicalName) - || SHORT_TYPE.equals(canonicalName) - || BYTE_TYPE.equals(canonicalName) - || DOUBLE_TYPE.equals(canonicalName) - || FLOAT_TYPE.equals(canonicalName) - || DATE_TYPE.equals(canonicalName) - || LOCAL_DATE_TYPE.equals(canonicalName) - || LOCAL_DATE_TIME_TYPE.equals(canonicalName)) { - return true; - } - return false; + return DATA_TYPES.contains(psiType.getCanonicalText()); } /** @@ -131,11 +150,12 @@ public static boolean isMapType(PsiType psiType) { /** * 判断一个类是否是指定类型子类 - * @param psiType psiType + * + * @param psiType psiType * @param qualifiedName 全限定名称 * @return */ - public static boolean isSpecifiedType(PsiType psiType, String qualifiedName){ + public static boolean isSpecifiedType(PsiType psiType, String qualifiedName) { if (!(psiType instanceof PsiClassType)) { return false; } diff --git a/src/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java similarity index 71% rename from src/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java index 474adbe..c0cc060 100644 --- a/src/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/PsiUtils.java @@ -1,13 +1,23 @@ package cn.bigcoder.plugin.objecthelper.common.util; import cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord; -import cn.bigcoder.plugin.objecthelper.common.enums.JavaModify; +import cn.bigcoder.plugin.objecthelper.common.enums.JavaModifyEnum; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.*; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +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; +import com.intellij.psi.PsiParameter; +import com.intellij.psi.PsiType; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.PsiTreeUtil; import org.apache.commons.compress.utils.Lists; @@ -16,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.jetbrains.annotations.Nullable; /** * @author: Jindong.Tian @@ -144,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(); + } + /** * 获取方法的参数列表 * @@ -162,20 +188,20 @@ public static List getPsiParameters(PsiMethod psiMethod) { * @return */ @NotNull - public static List getMethodModifies(PsiModifierList modifierList) { - List result = new ArrayList<>(); - if (modifierList.hasModifierProperty(JavaModify.PUBLIC.getName())) { - result.add(JavaModify.PUBLIC); - } else if (modifierList.hasModifierProperty(JavaModify.PROTECTED.getName())) { - result.add(JavaModify.PROTECTED); - } else if (modifierList.hasModifierProperty(JavaModify.PRIVATE.getName())) { - result.add(JavaModify.PRIVATE); + public static List getMethodModifies(PsiModifierList modifierList) { + List result = new ArrayList<>(); + if (modifierList.hasModifierProperty(JavaModifyEnum.PUBLIC.getName())) { + result.add(JavaModifyEnum.PUBLIC); + } else if (modifierList.hasModifierProperty(JavaModifyEnum.PROTECTED.getName())) { + result.add(JavaModifyEnum.PROTECTED); + } else if (modifierList.hasModifierProperty(JavaModifyEnum.PRIVATE.getName())) { + result.add(JavaModifyEnum.PRIVATE); } - if (modifierList.hasModifierProperty(JavaModify.STATIC.getName())) { - result.add(JavaModify.STATIC); + if (modifierList.hasModifierProperty(JavaModifyEnum.STATIC.getName())) { + result.add(JavaModifyEnum.STATIC); } - if (modifierList.hasModifierProperty(JavaModify.FINAL.getName())) { - result.add(JavaModify.FINAL); + if (modifierList.hasModifierProperty(JavaModifyEnum.FINAL.getName())) { + result.add(JavaModifyEnum.FINAL); } return result; } @@ -193,6 +219,23 @@ public static List getAllPsiFields(PsiClass psiClass) { return result; } + /** + * 判断一个字段是否是类的成员属性 + * + * @param psiField + * @return + */ + public static boolean isMemberField(PsiField psiField) { + PsiModifierList modifierList = psiField.getModifierList(); + if (modifierList == null || + modifierList.hasModifierProperty(PsiModifier.STATIC) || + modifierList.hasModifierProperty(PsiModifier.FINAL) || + modifierList.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { + return false; + } + return true; + } + /** * 递归获取类中所有字段 * diff --git a/src/cn/bigcoder/plugin/objecthelper/common/util/StringUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/StringUtils.java similarity index 100% rename from src/cn/bigcoder/plugin/objecthelper/common/util/StringUtils.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/common/util/StringUtils.java diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/XMLUtils.java b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/XMLUtils.java new file mode 100644 index 0000000..d55f516 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/common/util/XMLUtils.java @@ -0,0 +1,44 @@ +package cn.bigcoder.plugin.objecthelper.common.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; +import java.util.Map; + +/** + * @author: Jindong.Tian + * @date: 2022-08-26 + **/ +public class XMLUtils { + + /** + * 将Map转换为XML格式的字符串 + * + * @param data Map类型数据 + * @return XML格式的字符串 + * @throws Exception + */ + public static String mapToXml(Map data) { + ObjectMapper objectMapper = new XmlMapper(); + // 修正根元素名称 + objectMapper.addMixIn(Map.class, MapMixin.class); + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + String xmlString = ""; + try { + xmlString = objectMapper.writeValueAsString(data); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + return xmlString; + } + + /** + * 用于修正根元素名称 + */ + @JacksonXmlRootElement(localName = "root") + public static abstract class MapMixin { + + } +} diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java b/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java new file mode 100644 index 0000000..46b9b29 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/ObjectHelperConfigurable.java @@ -0,0 +1,56 @@ +package cn.bigcoder.plugin.objecthelper.config; + +import cn.bigcoder.plugin.objecthelper.ui.ConfigPage; +import com.intellij.openapi.options.SearchableConfigurable; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +/** + * 该类是配置页面的入口 + * https://plugins.jetbrains.com/docs/intellij/settings-tutorial.html + * + * @author: Jindong.Tian + * @date: 2022-08-31 + **/ +public class ObjectHelperConfigurable implements SearchableConfigurable { + private ConfigPage configPage = new ConfigPage(); + + @Override + public @NotNull String getId() { + return "cn.bigcoder.plugin.objecthelper.config.SettingFactory"; + } + + @Override + public @Nls(capitalization = Nls.Capitalization.Title) String getDisplayName() { + return "Object Helper"; + } + + @Override + public @Nullable JComponent createComponent() { + return configPage.getMainPanel(); + } + + @Override + public boolean isModified() { + PluginConfigModel instance = PluginConfigState.getInstance(); + PluginConfigModel currentConfigModel = configPage.getCurrentConfigModel(); + return !instance.equals(currentConfigModel); + } + + @Override + public void apply() { + PluginConfigModel instance = PluginConfigState.getInstance(); + PluginConfigModel currentConfigModel = configPage.getCurrentConfigModel(); + instance.setJsonSwitch(currentConfigModel.getJsonSwitch()); + instance.setThriftSwitch(currentConfigModel.getThriftSwitch()); + instance.setXmlSwitch(currentConfigModel.getXmlSwitch()); + 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 new file mode 100644 index 0000000..66fc4b6 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigModel.java @@ -0,0 +1,124 @@ +package cn.bigcoder.plugin.objecthelper.config; + +import cn.bigcoder.plugin.objecthelper.common.enums.FieldGenerateModeEnum; +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; +import cn.bigcoder.plugin.objecthelper.common.enums.WhetherEnum; +import java.util.Objects; + +/** + * @author: Jindong.Tian + * @date: 2022-08-31 + **/ +public class PluginConfigModel { + + /** + * 是否开启 Class To Json 功能,默认为开启状态 + */ + private FunctionSwitchEnum jsonSwitch = FunctionSwitchEnum.OPEN; + /** + * 是否开启 Class To Thrift IDL 功能,默认为开启状态 + */ + private FunctionSwitchEnum thriftSwitch = FunctionSwitchEnum.OPEN; + /** + * 是否开启 Class To XML 功能,默认为开启状态 + */ + private FunctionSwitchEnum xmlSwitch = FunctionSwitchEnum.OPEN; + /** + * 是否开启 Object Copy Method 功能,默认为开启状态 + */ + private FunctionSwitchEnum objectCopySwitch = FunctionSwitchEnum.OPEN; + /** + * Object Copy Method 功能中,以Source/Target对象为基础生成字段拷贝 + */ + private FieldGenerateModeEnum objectCopyMethodFieldGenerateMode = FieldGenerateModeEnum.TARGET; + + /** + * Object Copy Method 功能中,Source 和 Target 对象之间差异的字段,是否以代码注释的形式生成代码 + */ + private WhetherEnum objectCopyMethodFieldGenerateAnnotation = WhetherEnum.YES; + + /** + * Object Copy Method 功能中,使用builder模式生成拷贝代码时的判断依据,当目标对象类中包含正则所指定的方法,则默认按照builder模式生成,否则使用set模式生成 + */ + private String builderInstanceMethodName = ".*(builder|newBuilder).*"; + + public FunctionSwitchEnum getJsonSwitch() { + return jsonSwitch; + } + + public void setJsonSwitch(FunctionSwitchEnum jsonSwitch) { + this.jsonSwitch = jsonSwitch; + } + + public FunctionSwitchEnum getThriftSwitch() { + return thriftSwitch; + } + + public void setThriftSwitch(FunctionSwitchEnum thriftSwitch) { + this.thriftSwitch = thriftSwitch; + } + + public FunctionSwitchEnum getXmlSwitch() { + return xmlSwitch; + } + + public void setXmlSwitch(FunctionSwitchEnum xmlSwitch) { + this.xmlSwitch = xmlSwitch; + } + + public FunctionSwitchEnum getObjectCopySwitch() { + return objectCopySwitch; + } + + public void setObjectCopySwitch(FunctionSwitchEnum objectCopySwitch) { + this.objectCopySwitch = objectCopySwitch; + } + + public FieldGenerateModeEnum getObjectCopyMethodFieldGenerateMode() { + return objectCopyMethodFieldGenerateMode; + } + + public void setObjectCopyMethodFieldGenerateMode( + FieldGenerateModeEnum objectCopyMethodFieldGenerateMode) { + this.objectCopyMethodFieldGenerateMode = objectCopyMethodFieldGenerateMode; + } + + public WhetherEnum getObjectCopyMethodFieldGenerateAnnotation() { + return objectCopyMethodFieldGenerateAnnotation; + } + + public void setObjectCopyMethodFieldGenerateAnnotation( + WhetherEnum objectCopyMethodFieldGenerateAnnotation) { + 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) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PluginConfigModel that = (PluginConfigModel) o; + return jsonSwitch == that.jsonSwitch && thriftSwitch == that.thriftSwitch && xmlSwitch == that.xmlSwitch + && 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, builderInstanceMethodName); + } +} diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigState.java b/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigState.java new file mode 100644 index 0000000..3f47118 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/PluginConfigState.java @@ -0,0 +1,36 @@ +package cn.bigcoder.plugin.objecthelper.config; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.PersistentStateComponent; +import com.intellij.openapi.components.State; +import com.intellij.openapi.components.Storage; +import com.intellij.util.xmlb.XmlSerializerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * 该类用于持久化配置对象,每个想要在IDE重启时保持状态的组件都应该实现 PersistentStateComponent 接口 + * + * @author: Jindong.Tian + * @date: 2022-08-31 + **/ +@State( + name = "cn.bigcoder.plugin.objecthelper.config.PluginConfigState", + storages = @Storage("object-helper.xml") +) +public class PluginConfigState implements PersistentStateComponent { + + @Override + public @Nullable PluginConfigModel getState() { + return new PluginConfigModel(); + } + + @Override + public void loadState(@NotNull PluginConfigModel state) { + XmlSerializerUtil.copyBean(state, this); + } + + public static PluginConfigModel getInstance() { + return ApplicationManager.getApplication().getService(PluginConfigModel.class); + } +} diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/config/package-info.java b/src/main/java/cn/bigcoder/plugin/objecthelper/config/package-info.java new file mode 100644 index 0000000..4086cd7 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/config/package-info.java @@ -0,0 +1,7 @@ +package cn.bigcoder.plugin.objecthelper.config; + +/** + * 该包主要用于插件配置化实现,用户可以根据需求,配置部分功能的 隐藏/展示 + *

    + * 官方文档:https://plugins.jetbrains.com/docs/intellij/settings-tutorial.html + */ \ No newline at end of file diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractDataObjectGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractDataObjectGenerator.java new file mode 100644 index 0000000..d490bfe --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractDataObjectGenerator.java @@ -0,0 +1,88 @@ +package cn.bigcoder.plugin.objecthelper.generator; + +import cn.bigcoder.plugin.objecthelper.common.util.PsiTypeUtils; +import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils; +import com.google.common.collect.Maps; +import com.intellij.psi.PsiArrayType; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiField; +import com.intellij.psi.PsiType; +import com.intellij.psi.impl.source.PsiClassReferenceType; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.compress.utils.Lists; +import org.apache.commons.compress.utils.Sets; +import java.util.List; +import java.util.Map; + +import java.util.Set; +import org.apache.commons.lang3.ArrayUtils; + +/** + * @author: Jindong.Tian + * @date: 2022-08-26 + **/ +public abstract class AbstractDataObjectGenerator implements Generator { + + private PsiClass psiClass; + + public AbstractDataObjectGenerator(PsiClass psiClass) { + this.psiClass = psiClass; + } + + /** + * 保存已经解析过的自定义类型名称,防止出现递归嵌套的情况 + */ + private Set recursiveCache = Sets.newHashSet(); + + protected Map processFields() { + return processFields(psiClass); + } + + protected Map processFields(PsiClass psiClass) { + Map result = Maps.newLinkedHashMap(); + // 当前类所有字段 + List allPsiFields = PsiUtils.getAllPsiFields(psiClass); + if (CollectionUtils.isEmpty(allPsiFields)) { + return result; + } + for (PsiField psiField : allPsiFields) { + result.put(psiField.getName(), processField(psiField.getType())); + } + return result; + } + + private Object processField(PsiType psiType) { + Object defaultValue = null; + if (PsiTypeUtils.isDataType(psiType)) { + //如果是数据类型 + defaultValue = PsiTypeUtils.getDataTypeDefaultValue(psiType.getCanonicalText()); + } else if (PsiTypeUtils.isArrayType(psiType)) { + //如果是数组类型 + List list = Lists.newArrayList(); + PsiClass arrayContentClass = PsiUtils.getPsiClass(((PsiArrayType) psiType).getComponentType(), + psiClass.getProject()); + list.add(PsiTypeUtils.getDataTypeDefaultValue(arrayContentClass.getQualifiedName())); + defaultValue = list; + } else if (PsiTypeUtils.isCollectionType(psiType)) { + defaultValue = Lists.newArrayList(); + //如果是集合类型 + PsiType[] parameters = ((PsiClassReferenceType) psiType).getParameters(); + if (ArrayUtils.isEmpty(parameters)) { + return defaultValue; + } + //获取泛型 + PsiType genericType = parameters[0]; + ((List) defaultValue).add(processField(genericType)); + } else if (!PsiTypeUtils.isJavaOfficialType(psiType)) { + //如果是自定义类型 + if (recursiveCache.contains(psiType.getCanonicalText())) { + //出现递归嵌套 + return null; + } + recursiveCache.add(psiType.getCanonicalText()); + //如果是自定义类 + defaultValue = processFields(PsiUtils.getPsiClass(psiType, psiClass.getProject())); + } + return defaultValue; + } +} diff --git a/src/cn/bigcoder/plugin/objecthelper/generator/method/AbstractMethodGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractMethodGenerator.java similarity index 90% rename from src/cn/bigcoder/plugin/objecthelper/generator/method/AbstractMethodGenerator.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractMethodGenerator.java index a3e08c8..c3912c2 100644 --- a/src/cn/bigcoder/plugin/objecthelper/generator/method/AbstractMethodGenerator.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/AbstractMethodGenerator.java @@ -1,4 +1,4 @@ -package cn.bigcoder.plugin.objecthelper.generator.method; +package cn.bigcoder.plugin.objecthelper.generator; import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils; import cn.bigcoder.plugin.objecthelper.common.util.StringUtils; @@ -6,6 +6,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiParameter; +import com.intellij.psi.PsiType; import org.apache.commons.collections.CollectionUtils; import java.util.List; @@ -49,7 +50,7 @@ protected StringBuilder generateMethodFirstLine() { * 生成方法体 * @return */ - abstract String generateMethodBody(); + protected abstract String generateMethodBody(); /** * 检查是否具备生成方法所需要的环境 @@ -66,4 +67,8 @@ protected boolean check() { protected List getParameters() { return getPsiParameters(psiMethod); } + + protected PsiType getReturnType() { + return psiMethod.getReturnType(); + } } diff --git a/src/cn/bigcoder/plugin/objecthelper/generator/Generator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/Generator.java similarity index 100% rename from src/cn/bigcoder/plugin/objecthelper/generator/Generator.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/generator/Generator.java 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/cn/bigcoder/plugin/objecthelper/generator/idl/IDLGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/idl/ThriftIDLGenerator.java similarity index 92% rename from src/cn/bigcoder/plugin/objecthelper/generator/idl/IDLGenerator.java rename to src/main/java/cn/bigcoder/plugin/objecthelper/generator/idl/ThriftIDLGenerator.java index bfad7d2..0679bdd 100644 --- a/src/cn/bigcoder/plugin/objecthelper/generator/idl/IDLGenerator.java +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/idl/ThriftIDLGenerator.java @@ -3,6 +3,7 @@ import cn.bigcoder.plugin.objecthelper.common.util.IDLUtils; import cn.bigcoder.plugin.objecthelper.common.util.PsiTypeUtils; import cn.bigcoder.plugin.objecthelper.common.util.PsiUtils; +import cn.bigcoder.plugin.objecthelper.common.util.StringUtils; import cn.bigcoder.plugin.objecthelper.generator.Generator; import com.intellij.psi.PsiArrayType; import com.intellij.psi.PsiClass; @@ -11,16 +12,16 @@ import com.intellij.psi.impl.source.PsiClassReferenceType; import org.apache.commons.compress.utils.Lists; import org.apache.commons.compress.utils.Sets; -import org.apache.commons.lang.ArrayUtils; import java.util.List; import java.util.Set; +import org.apache.commons.lang3.ArrayUtils; /** * @author: Jindong.Tian * @date: 2021-08-20 **/ -public class IDLGenerator implements Generator { +public class ThriftIDLGenerator implements Generator { private PsiClass psiClass; @@ -29,7 +30,7 @@ public class IDLGenerator implements Generator { */ private Set recursiveCache = Sets.newHashSet(); - private IDLGenerator(PsiClass psiClass) { + private ThriftIDLGenerator(PsiClass psiClass) { this.psiClass = psiClass; } @@ -45,6 +46,9 @@ public String generate() { * @return */ private StringBuilder generateStructCode(PsiClass psiClass) { + if (psiClass == null) { + return new StringBuilder(); + } if (recursiveCache.contains(psiClass.getQualifiedName())) { return new StringBuilder(); } @@ -54,7 +58,10 @@ private StringBuilder generateStructCode(PsiClass psiClass) { StringBuilder code = new StringBuilder(); for (PsiField allPsiField : allPsiFields) { code = processAssociateClass(allPsiField.getType(), code); - fields.add(processPsiField(allPsiField)); + String field = processPsiField(allPsiField); + if (StringUtils.isNotEmpty(field)) { + fields.add(field); + } } return code.append(generateStructCode(psiClass.getName(), fields)); } @@ -179,7 +186,7 @@ private String processType(PsiType psiType) { return null; } - public static IDLGenerator getInstance(PsiClass psiClass) { - return new IDLGenerator(psiClass); + public static ThriftIDLGenerator getInstance(PsiClass psiClass) { + return new ThriftIDLGenerator(psiClass); } } diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java new file mode 100644 index 0000000..701f494 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/json/ClassJsonGenerator.java @@ -0,0 +1,38 @@ +package cn.bigcoder.plugin.objecthelper.generator.json; + +import cn.bigcoder.plugin.objecthelper.generator.AbstractDataObjectGenerator; +import com.google.gson.Gson; +import com.intellij.psi.PsiClass; + +import java.util.Map; + +/** + * @author: Jindong.Tian + * @date: 2021-02-11 + **/ +public class ClassJsonGenerator extends AbstractDataObjectGenerator { + + private Gson gson; + + private ClassJsonGenerator(PsiClass psiClass, Gson gson) { + super(psiClass); + this.gson = gson; + } + + /** + * 获取ClassJsonGenerator实例 + * + * @param psiClass JSON生成的目标类 + * @param gson Gson对象 + * @return + */ + public static ClassJsonGenerator getInstance(PsiClass psiClass, Gson gson) { + return new ClassJsonGenerator(psiClass, gson); + } + + @Override + public String generate() { + Map jsonMap = processFields(); + return gson.toJson(jsonMap); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..63a4b78 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/method/ObjectCopyMethodGenerator.java @@ -0,0 +1,89 @@ +package cn.bigcoder.plugin.objecthelper.generator.method; + +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.PsiMethod; +import com.intellij.psi.PsiParameter; +import java.util.regex.Pattern; + +/** + * @author: Jindong.Tian + * @date: 2021-01-09 + **/ +public class ObjectCopyMethodGenerator extends AbstractMethodGenerator { + + /** + * 方法第一个参数名称 + */ + private String firstParameterName; + + private void init(PsiMethod psiMethod) { + if (psiMethod == null) { + return; + } + super.project = psiMethod.getProject(); + super.psiMethod = psiMethod; + this.firstParameterName = getFirstParameter().getName(); + } + + public static ObjectCopyMethodGenerator getInstance(PsiMethod psiMethod) { + ObjectCopyMethodGenerator objectCopyMethodGenerator = new ObjectCopyMethodGenerator(); + objectCopyMethodGenerator.init(psiMethod); + return objectCopyMethodGenerator; + } + + /** + * 此方法中不应该存在判空的操作,依赖环境的建议重写父类的check方法,在生成早期拦截异常情况 + * + * @return + */ + @Override + protected String generateMethodBody() { + AbstractObjectCopyStrategy copyStrategy = getCopyStrategy(); + return copyStrategy.generate(); + } + + 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); + } + } + return new SetObjectCopyStrategy(sourceClass, returnClass, this.firstParameterName); + } + + /** + * 获取参数列表第一个参数的{@code PsiParameter} + * + * @return + */ + private PsiParameter getFirstParameter() { + return getParameters().get(FIRST_INDEX); + } + + + /** + * 获取参数列表第一个参数的{@code PsiClass} + * + * @return + */ + private PsiClass getFirstParameterClass() { + return getPsiClass(getFirstParameter().getType(), project); + } + + private PsiClass getReturnClass() { + return getPsiClass(getReturnType(), project); + } +} \ No newline at end of file diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/generator/xml/ClassXMLGenerator.java b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/xml/ClassXMLGenerator.java new file mode 100644 index 0000000..abcdd28 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/generator/xml/ClassXMLGenerator.java @@ -0,0 +1,34 @@ +package cn.bigcoder.plugin.objecthelper.generator.xml; + +import cn.bigcoder.plugin.objecthelper.common.util.XMLUtils; +import cn.bigcoder.plugin.objecthelper.generator.AbstractDataObjectGenerator; +import com.intellij.psi.PsiClass; + +import java.util.Map; + +/** + * @author: Jindong.Tian + * @date: 2021-02-11 + **/ +public class ClassXMLGenerator extends AbstractDataObjectGenerator { + + private ClassXMLGenerator(PsiClass psiClass) { + super(psiClass); + } + + /** + * 获取ClassJsonGenerator实例 + * + * @param psiClass JSON生成的目标类 + * @return + */ + public static ClassXMLGenerator getInstance(PsiClass psiClass) { + return new ClassXMLGenerator(psiClass); + } + + @Override + public String generate() { + Map jsonMap = processFields(); + return XMLUtils.mapToXml(jsonMap); + } +} \ No newline at end of file diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form new file mode 100644 index 0000000..ec970d0 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.form @@ -0,0 +1,206 @@ + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    diff --git a/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java new file mode 100644 index 0000000..985abb4 --- /dev/null +++ b/src/main/java/cn/bigcoder/plugin/objecthelper/ui/ConfigPage.java @@ -0,0 +1,85 @@ +package cn.bigcoder.plugin.objecthelper.ui; + +import cn.bigcoder.plugin.objecthelper.common.enums.FieldGenerateModeEnum; +import cn.bigcoder.plugin.objecthelper.common.enums.FunctionSwitchEnum; +import cn.bigcoder.plugin.objecthelper.common.enums.WhetherEnum; +import cn.bigcoder.plugin.objecthelper.config.PluginConfigState; +import cn.bigcoder.plugin.objecthelper.config.PluginConfigModel; + +import java.util.Optional; +import javax.swing.*; + +/** + * @author: Jindong.Tian + * @date: 2022-08-26 + **/ +public class ConfigPage { + + private JPanel mainPanel; + private JComboBox classToJsonSwitch; + private JComboBox classToThriftSwitch; + private JComboBox classToXmlSwitch; + private JComboBox objectCopyMethodSwitch; + private JButton tipsButton; + private JComboBox objectCopyMethodGenerateMode; + private JComboBox objectCopyMethodGenerateAnnotation; + private JTextField builderInstanceMethodName; + + public JPanel getMainPanel() { + initField(); + return mainPanel; + } + + /** + * 根据持久化配置,初始化面板 + */ + public void initField() { + PluginConfigModel instance = PluginConfigState.getInstance(); + this.classToJsonSwitch.setSelectedItem(instance.getJsonSwitch().getCode()); + this.classToThriftSwitch.setSelectedItem(instance.getThriftSwitch().getCode()); + this.classToXmlSwitch.setSelectedItem(instance.getXmlSwitch().getCode()); + this.objectCopyMethodSwitch.setSelectedItem(instance.getObjectCopySwitch().getCode()); + this.objectCopyMethodGenerateAnnotation.setSelectedItem(instance.getObjectCopyMethodFieldGenerateAnnotation().getCode()); + this.objectCopyMethodGenerateMode.setSelectedItem(instance.getObjectCopyMethodFieldGenerateMode().getCode()); + this.builderInstanceMethodName.setText(instance.getBuilderInstanceMethodName()); + } + + /** + * 获取页面当前的配置信息 + * + * @return + */ + public PluginConfigModel getCurrentConfigModel() { + PluginConfigModel pluginConfigModel = new PluginConfigModel(); + + Optional.ofNullable(this.classToJsonSwitch.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setJsonSwitch(FunctionSwitchEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.classToThriftSwitch.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setThriftSwitch(FunctionSwitchEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.classToXmlSwitch.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setXmlSwitch(FunctionSwitchEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.objectCopyMethodSwitch.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setObjectCopySwitch(FunctionSwitchEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.objectCopyMethodGenerateMode.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setObjectCopyMethodFieldGenerateMode(FieldGenerateModeEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.objectCopyMethodGenerateAnnotation.getSelectedItem()).ifPresent(e -> { + pluginConfigModel.setObjectCopyMethodFieldGenerateAnnotation(WhetherEnum.nameOf(e.toString())); + } + ); + Optional.ofNullable(this.builderInstanceMethodName.getText()).ifPresent( + pluginConfigModel::setBuilderInstanceMethodName + ); + return pluginConfigModel; + } + +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml new file mode 100644 index 0000000..1581439 --- /dev/null +++ b/src/main/resources/META-INF/plugin.xml @@ -0,0 +1,70 @@ + + cn.bigcoder.plugin.objecthelper + ObjectHelper + 1.3.2 + HearingSmile + + GitHub +
  • Copy the object
  • + +
  • Class to JSON
  • + +
  • Class to Thrift IDL
  • + +
  • Class to XML
  • + + ]]>
    + + add feature:Optimize the class to xml generation results +
  • fix feature:Optimize the class to xml generation results
  • +
  • fix bug:Fix class to Thrift-IDL function NPE problem
  • + ]]> +
    + + + + + + + com.intellij.modules.java + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    \ No newline at end of file diff --git a/resources/META-INF/pluginIcon.svg b/src/main/resources/META-INF/pluginIcon.svg similarity index 100% rename from resources/META-INF/pluginIcon.svg rename to src/main/resources/META-INF/pluginIcon.svg