项目初始化
This commit is contained in:
parent
927e62f0ea
commit
9d50266c1f
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 liuzhihang
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
34
README.md
Normal file
34
README.md
Normal file
@ -0,0 +1,34 @@
|
||||
Toolkit
|
||||
=======
|
||||
|
||||
[](https://plugins.jetbrains.com/plugin/13606-copy-as-json)
|
||||
[](https://plugins.jetbrains.com/plugin/13606-copy-as-json/versions)
|
||||
[](https://plugins.jetbrains.com/plugin/13606-copy-as-json)
|
||||
[](https://github.com/liuzhihangs/copy-as-json/blob/master/LICENSE)
|
||||
|
||||
|
||||
复制JavaBean为Json
|
||||
|
||||
特征
|
||||
----
|
||||
|
||||
- 复制JavaBean为Json
|
||||
|
||||
|
||||
安装
|
||||
----
|
||||
- **在线安装:**
|
||||
- `File` -> `Setting` -> `Plugins` -> 搜索 `Copy as Json`
|
||||
|
||||
- **手动安装:**
|
||||
- [下载插件](https://github.com/liuzhihangs/copy-as-json/releases) -> `File` -> `Setting` -> `Plugins` -> `Install Plugin from Disk...`
|
||||
|
||||
使用
|
||||
----
|
||||
- 右键菜单选择 `Copy as Json`
|
||||
- 快捷键 `ctrl shift J`
|
||||
|
||||
更新
|
||||
----
|
||||
|
||||
|
||||
32
build.gradle
Normal file
32
build.gradle
Normal file
@ -0,0 +1,32 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.jetbrains.intellij' version '0.4.15'
|
||||
}
|
||||
|
||||
group 'com.liuzhihang.toolkit'
|
||||
version '1.0.0'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
maven { url "https://maven.aliyun.com/repository/public" }
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
|
||||
// See https://github.com/JetBrains/gradle-intellij-plugin/
|
||||
intellij {
|
||||
version '2019.1.1'
|
||||
pluginName 'Copy as Json'
|
||||
updateSinceUntilBuild false
|
||||
sameSinceUntilBuild false
|
||||
}
|
||||
patchPluginXml {
|
||||
pluginDescription(file(descriptionFile).text)
|
||||
changeNotes(file(changesFile).text)
|
||||
}
|
||||
2
gradle.properties
Normal file
2
gradle.properties
Normal file
@ -0,0 +1,2 @@
|
||||
descriptionFile=parts/pluginDescription.html
|
||||
changesFile=parts/changeNotes.html
|
||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
172
gradlew
vendored
Executable file
172
gradlew
vendored
Executable file
@ -0,0 +1,172 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## 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"'
|
||||
|
||||
# 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
|
||||
;;
|
||||
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, switch paths to Windows format before running java
|
||||
if $cygwin ; 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=$((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"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
84
gradlew.bat
vendored
Normal file
84
gradlew.bat
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
@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 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"
|
||||
|
||||
@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 init
|
||||
|
||||
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 init
|
||||
|
||||
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
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
: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 %CMD_LINE_ARGS%
|
||||
|
||||
: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
|
||||
9
parts/changeNotes.html
Normal file
9
parts/changeNotes.html
Normal file
@ -0,0 +1,9 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
|
||||
<ul>
|
||||
<li>1.0.0
|
||||
<ol>
|
||||
<li>在Java Entity 中右键复制为Json</li>
|
||||
</ol>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
21
parts/pluginDescription.html
Normal file
21
parts/pluginDescription.html
Normal file
@ -0,0 +1,21 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
|
||||
<h1>IntelliJ Copy as Json plugin</h1>
|
||||
<br/>
|
||||
<a href="https://github.com/liuzhihangs/copy-as-json">GitHub</a> |
|
||||
<a href="https://github.com/liuzhihangs/copy-as-json/issues">Issues</a> |
|
||||
<a href="https://liuzhihang.com/">Website</a> |
|
||||
<a href="https://github.com/liuzhihangs/copy-as-json/blob/master/LICENSE">LICENSE</a>
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<b>Copy as Json plugin for IntelliJ IDEA</b>
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<ul>
|
||||
<li>JSON
|
||||
<ul>
|
||||
<li>将JavaBean复制为Json字符串</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
2
settings.gradle
Normal file
2
settings.gradle
Normal file
@ -0,0 +1,2 @@
|
||||
rootProject.name = 'copy-as-json'
|
||||
|
||||
@ -0,0 +1,181 @@
|
||||
package com.liuzhihang.toolkit.action;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.intellij.lang.jvm.JvmClassKind;
|
||||
import com.intellij.notification.*;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
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.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.liuzhihang.toolkit.utils.GsonFormatUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* JavaBean 复制为Json字符串
|
||||
*
|
||||
* @author liuzhihang
|
||||
* @date 2019/5/5 13:42
|
||||
*/
|
||||
public class CopyAsJsonAction extends AnAction {
|
||||
|
||||
private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup("Java2Json.NotificationGroup", NotificationDisplayType.BALLOON, true);
|
||||
|
||||
@NonNls
|
||||
private static final Map<String, Object> PROPERTIES_TYPES = new HashMap<>(16);
|
||||
@NonNls
|
||||
private static final Set<String> ANNOTATION_TYPES = new HashSet<>();
|
||||
|
||||
static {
|
||||
// 包装数据类型
|
||||
PROPERTIES_TYPES.put("Byte", 0);
|
||||
PROPERTIES_TYPES.put("Short", 0);
|
||||
PROPERTIES_TYPES.put("Integer", 0);
|
||||
PROPERTIES_TYPES.put("Long", 0L);
|
||||
PROPERTIES_TYPES.put("Float", 0.0F);
|
||||
PROPERTIES_TYPES.put("Double", 0.0D);
|
||||
PROPERTIES_TYPES.put("Boolean", false);
|
||||
// 其他
|
||||
PROPERTIES_TYPES.put("String", "");
|
||||
PROPERTIES_TYPES.put("BigDecimal", null);
|
||||
PROPERTIES_TYPES.put("Date", null);
|
||||
PROPERTIES_TYPES.put("LocalDate", null);
|
||||
PROPERTIES_TYPES.put("LocalTime", null);
|
||||
PROPERTIES_TYPES.put("LocalDateTime", null);
|
||||
|
||||
// 注解过滤
|
||||
ANNOTATION_TYPES.add("javax.annotation.Resource");
|
||||
ANNOTATION_TYPES.add("org.springframework.beans.factory.annotation.Autowired");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent e) {
|
||||
|
||||
Project project = e.getData(PlatformDataKeys.PROJECT);
|
||||
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
|
||||
Editor editor = e.getData(CommonDataKeys.EDITOR);
|
||||
|
||||
PsiElement referenceAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
|
||||
PsiClass selectedClass = (PsiClass) PsiTreeUtil.getContextOfType(referenceAt, new Class[]{PsiClass.class});
|
||||
try {
|
||||
Map fieldsMap = getFields(selectedClass);
|
||||
|
||||
Gson gson = new GsonBuilder().create();
|
||||
String json = GsonFormatUtil.gsonFormat(gson, fieldsMap);
|
||||
|
||||
// 使用自定义缩进格式 String json = new GsonBuilder().setPrettyPrinting().create().toJson(fieldsMap);
|
||||
StringSelection selection = new StringSelection(json);
|
||||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||
clipboard.setContents(selection, selection);
|
||||
String message = "Convert " + selectedClass.getName() + " to JSON success, copied to clipboard.";
|
||||
Notification success = NOTIFICATION_GROUP.createNotification(message, NotificationType.INFORMATION);
|
||||
Notifications.Bus.notify(success, project);
|
||||
} catch (Exception ex) {
|
||||
Notification error = NOTIFICATION_GROUP.createNotification("Convert to JSON failed.", NotificationType.ERROR);
|
||||
Notifications.Bus.notify(error, project);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Map getFields(PsiClass psiClass) {
|
||||
|
||||
Map<String, Object> fieldMap = new LinkedHashMap<>();
|
||||
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();
|
||||
|
||||
if (psiClass != null && psiClass.getClassKind() == JvmClassKind.CLASS) {
|
||||
for (PsiField field : psiClass.getAllFields()) {
|
||||
PsiType type = field.getType();
|
||||
String name = field.getName();
|
||||
// if (field.getDocComment() != null && StringUtils.isNotBlank(field.getDocComment().getText())) {
|
||||
// String fieldComment = field.getDocComment().getText();
|
||||
// commentFieldMap.put(name, CommentUtils.removeSymbol(fieldComment));
|
||||
// }
|
||||
// 判断注解 javax.annotation.Resource org.springframework.beans.factory.annotation.Autowired
|
||||
PsiAnnotation[] annotations = field.getAnnotations();
|
||||
if (annotations.length > 0 && containsAnnotation(annotations)) {
|
||||
fieldMap.put(name, "");
|
||||
} else if (type instanceof PsiPrimitiveType) {
|
||||
// 基本类型
|
||||
fieldMap.put(name, PsiTypesUtil.getDefaultValue(type));
|
||||
} else {
|
||||
//reference Type
|
||||
String fieldTypeName = type.getPresentableText();
|
||||
// 指定的类型
|
||||
if (PROPERTIES_TYPES.containsKey(fieldTypeName)) {
|
||||
fieldMap.put(name, PROPERTIES_TYPES.get(fieldTypeName));
|
||||
} else if (type instanceof PsiArrayType) {
|
||||
//array type
|
||||
List<Object> list = new ArrayList<>();
|
||||
PsiType deepType = type.getDeepComponentType();
|
||||
String deepTypeName = deepType.getPresentableText();
|
||||
if (deepType instanceof PsiPrimitiveType) {
|
||||
list.add(PsiTypesUtil.getDefaultValue(deepType));
|
||||
} else if (PROPERTIES_TYPES.containsKey(deepTypeName)) {
|
||||
list.add(PROPERTIES_TYPES.get(deepTypeName));
|
||||
} else {
|
||||
list.add(getFields(PsiUtil.resolveClassInType(deepType)));
|
||||
}
|
||||
fieldMap.put(name, list);
|
||||
} else if (fieldTypeName.startsWith("List") || fieldTypeName.startsWith("ArrayList") || fieldTypeName.startsWith("Set") || fieldTypeName.startsWith("HashSet")) {
|
||||
// List Set or HashSet
|
||||
List<Object> list = new ArrayList<>();
|
||||
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
|
||||
PsiClass iterableClass = PsiUtil.resolveClassInClassTypeOnly(iterableType);
|
||||
if (iterableClass != null) {
|
||||
String classTypeName = iterableClass.getName();
|
||||
if (PROPERTIES_TYPES.containsKey(classTypeName)) {
|
||||
list.add(PROPERTIES_TYPES.get(classTypeName));
|
||||
} else {
|
||||
list.add(getFields(iterableClass));
|
||||
}
|
||||
}
|
||||
fieldMap.put(name, list);
|
||||
} else if (fieldTypeName.startsWith("HashMap") || fieldTypeName.startsWith("Map")) {
|
||||
// HashMap or Map
|
||||
fieldMap.put(name, new HashMap<>(4));
|
||||
} else if (PsiUtil.resolveClassInType(type).getClassKind() != JvmClassKind.CLASS) {
|
||||
// enum or interface
|
||||
fieldMap.put(name, "");
|
||||
} else {
|
||||
fieldMap.put(name, getFields(PsiUtil.resolveClassInType(type)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// json 串中的注释字段 暂时不添加
|
||||
// if (commentFieldMap.size() > 0) {
|
||||
// fieldMap.put("@comment", commentFieldMap);
|
||||
// }
|
||||
}
|
||||
return fieldMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含指定的注解
|
||||
*
|
||||
* @param annotations
|
||||
* @return
|
||||
*/
|
||||
private static boolean containsAnnotation(PsiAnnotation[] annotations) {
|
||||
for (PsiAnnotation annotation : annotations) {
|
||||
if (ANNOTATION_TYPES.contains(annotation.getQualifiedName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package com.liuzhihang.toolkit.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* 对 Gson 进行修改主要是修改 newJsonWriter中的缩进
|
||||
*
|
||||
* @author liuzhihang
|
||||
* @date 2019/5/9 11:07
|
||||
*/
|
||||
public class GsonFormatUtil {
|
||||
|
||||
@NotNull
|
||||
public static String gsonFormat(Gson gson, JsonElement jsonElement) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(Streams.writerForAppendable(writer)));
|
||||
gson.toJson(jsonElement, jsonWriter);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String gsonFormat(Gson gson, Object src) throws IOException {
|
||||
if (src == null) {
|
||||
return gson.toJson(JsonNull.INSTANCE);
|
||||
}
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(Streams.writerForAppendable(writer)));
|
||||
gson.toJson(src, src.getClass(), jsonWriter);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新JsonWriter的缩进
|
||||
* 具体请查看相关源码
|
||||
*
|
||||
* @param writer
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @see Gson#newJsonWriter(Writer)
|
||||
*/
|
||||
private static JsonWriter newJsonWriter(Writer writer) throws IOException {
|
||||
JsonWriter jsonWriter = new JsonWriter(writer);
|
||||
// 修改此处缩进为四个空格
|
||||
jsonWriter.setIndent(" ");
|
||||
jsonWriter.setSerializeNulls(false);
|
||||
return jsonWriter;
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/resources/META-INF/plugin.xml
Normal file
26
src/main/resources/META-INF/plugin.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<idea-plugin>
|
||||
<id>com.liuzhihang.toolkit.copyasjson</id>
|
||||
<name>Copy as Json</name>
|
||||
<vendor email="liuzhihangs@qq.com" url="https://liuzhihang.com">Liu ZhiHang</vendor>
|
||||
|
||||
<description><![CDATA[ Description will be added by gradle build]]></description>
|
||||
|
||||
|
||||
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
|
||||
on how to target different products -->
|
||||
<depends>com.intellij.modules.java</depends>
|
||||
|
||||
<idea-version since-build="181.00"/>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- Add your extensions here -->
|
||||
</extensions>
|
||||
|
||||
<actions>
|
||||
<action id="Toolkit.Json.CopyAsJsonAction" class="com.liuzhihang.toolkit.action.CopyAsJsonAction"
|
||||
text="Copy As Json" description="Copy As Json">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
||||
<keyboard-shortcut keymap="$default" first-keystroke="ctrl shift J"/>
|
||||
</action>
|
||||
</actions>
|
||||
</idea-plugin>
|
||||
Loading…
Reference in New Issue
Block a user