Skip to content

Commit a85a616

Browse files
committed
新增插件功能,更改插件配置:
1.新增Java类转JSON功能 2.将GenerateO2O重命名为Object Copy Method
1 parent abfcfca commit a85a616

File tree

11 files changed

+411
-19
lines changed

11 files changed

+411
-19
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# object-helper-plugin
2-
![](https://img.shields.io/badge/version-v1.0.3-blue)
2+
![](https://img.shields.io/badge/version-v1.1.0-blue)
33
![](https://img.shields.io/badge/license-Apache%202-red)
4-
![](https://img.shields.io/badge/download-100%2B-green)
4+
![](https://img.shields.io/badge/size-28%20kB-yellowgreen)
5+
![](https://img.shields.io/badge/download-200%2B-green)
56

6-
JetBrains Intellij IDEA Obejct辅助插件,包含以下功能:
7+
JetBrains Intellij IDEA Object插件,包含以下功能:
78

89
- 对象拷贝
910

10-
![](https://image.bigcoder.cn/6d6af6fd-255c-4d32-83bb-85b39280460d.gif)
11+
![](https://image.bigcoder.cn/7fce876e-fa94-4780-bb14-584068c35963.gif)
1112

12-
-
13+
- Java类转JSON
14+
15+
![](https://image.bigcoder.cn/20210227223302.gif)

object-helper.jar

7.72 KB
Binary file not shown.

resources/META-INF/plugin.xml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<idea-plugin>
22
<id>cn.bigcoder.plugin.objecthelper</id>
33
<name>ObjectHelper</name>
4-
<version>1.0.3</version>
5-
<vendor email="[email protected]" url="https://github.com/tianjindong/object-helper-plugin">HearingSmile</vendor>
4+
<version>1.1.0</version>
5+
<vendor email="[email protected]" url="https://github.com/bigcoder84/object-helper-plugin">HearingSmile</vendor>
66

77
<description><![CDATA[
8-
This is a Java object toolset
8+
This is a Java object toolset.Using document:<a href="https://github.com/bigcoder84/object-helper-plugin">GitHub</a>
99
<li>Copy the object</li>
10-
<img style="width: 400px" src="https://image.bigcoder.cn/6d6af6fd-255c-4d32-83bb-85b39280460d.gif"></img>
10+
<img style="width: 300px" src="https://image.bigcoder.cn/7fce876e-fa94-4780-bb14-584068c35963.gif"></img>
11+
<li>Class to JSON</li>
12+
<img style="width: 300px" src="https://image.bigcoder.cn/20210227223302.gif"></img>
1113
]]></description>
1214

1315
<change-notes><![CDATA[
14-
<li>Fix bug:Fixed parent field not being generated</li>
16+
<li>New feature:Added the ability to quickly convert Java classes to JSON</li>
17+
<li>Update feature:Rename GenerateO2O to Object Method Copy</li>
1518
]]>
1619
</change-notes>
1720

@@ -28,9 +31,20 @@
2831
</extensions>
2932

3033
<actions>
31-
<action id="cn.bigcoder.plugin.objecthelper.action.GenerateO2O" class="cn.bigcoder.plugin.objecthelper.action.GenerateO2O" text="GenerateO2O">
34+
<action id="cn.bigcoder.plugin.objecthelper.action.ObjectCopyMethodAction" class="cn.bigcoder.plugin.objecthelper.action.ObjectCopyMethodAction" text="Object Copy Method">
3235
<add-to-group group-id="GenerateGroup" anchor="last"/>
3336
</action>
37+
<action id="cn.bigcoder.plugin.objecthelper.action.ClassToFormatJsonAction"
38+
class="cn.bigcoder.plugin.objecthelper.action.ClassToFormatJsonAction" text="Class To JSON(Format)"
39+
description="Converts the object to a JSON string">
40+
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
41+
<keyboard-shortcut keymap="$default" first-keystroke="shift alt P"/>
42+
</action>
43+
<action id="cn.bigcoder.plugin.objecthelper.action.ClassToJsonAction"
44+
class="cn.bigcoder.plugin.objecthelper.action.ClassToJsonAction" text="Class To JSON"
45+
description="Converts the object to a JSON string">
46+
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
47+
</action>
3448
</actions>
3549

3650
</idea-plugin>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.bigcoder.plugin.objecthelper.action;
2+
3+
import cn.bigcoder.plugin.objecthelper.generator.Generator;
4+
import cn.bigcoder.plugin.objecthelper.generator.json.ClassJsonGenerator;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
7+
import com.intellij.psi.PsiClass;
8+
9+
public class ClassToFormatJsonAction extends ClassToJsonAction {
10+
11+
@Override
12+
protected Generator getGenerator(PsiClass psiClass) {
13+
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
14+
return ClassJsonGenerator.getInstance(psiClass, gson);
15+
}
16+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.bigcoder.plugin.objecthelper.action;
2+
3+
import cn.bigcoder.plugin.objecthelper.common.util.NotificationUtils;
4+
import cn.bigcoder.plugin.objecthelper.generator.Generator;
5+
import cn.bigcoder.plugin.objecthelper.generator.json.ClassJsonGenerator;
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
import com.intellij.openapi.actionSystem.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.ide.CopyPasteManager;
11+
import com.intellij.psi.PsiClass;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
import java.awt.datatransfer.StringSelection;
15+
16+
import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.getOperatePsiClass;
17+
import static cn.bigcoder.plugin.objecthelper.common.util.PsiUtils.setActionInvisible;
18+
19+
public class ClassToJsonAction extends AnAction {
20+
21+
@Override
22+
public void actionPerformed(AnActionEvent anAction) {
23+
PsiClass psiClass = getOperatePsiClass(anAction);
24+
if (psiClass == null) {
25+
return;
26+
}
27+
String json = getGenerator(psiClass).generate();
28+
CopyPasteManager.getInstance().setContents(new StringSelection(json));
29+
NotificationUtils.notifyInfo(anAction.getProject(), "JSON字符串成功置入剪贴板:<br>" + json);
30+
}
31+
32+
@Override
33+
public void update(@NotNull AnActionEvent anActionEvent) {
34+
// 如果当前光标不在方法中,则不显示ConvertToJson组件
35+
if (getOperatePsiClass(anActionEvent) == null) {
36+
setActionInvisible(anActionEvent);
37+
}
38+
super.update(anActionEvent);
39+
}
40+
41+
protected Generator getGenerator(PsiClass psiClass) {
42+
Gson gson = new GsonBuilder().serializeNulls().create();
43+
return ClassJsonGenerator.getInstance(psiClass, gson);
44+
}
45+
}

src/cn/bigcoder/plugin/objecthelper/action/GenerateO2O.java renamed to src/cn/bigcoder/plugin/objecthelper/action/ObjectCopyMethodAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import com.intellij.psi.PsiMethod;
1313
import org.jetbrains.annotations.NotNull;
1414

15-
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.*;
15+
import static cn.bigcoder.plugin.objecthelper.common.constant.JavaKeyWord.VOID;
1616

17-
public class GenerateO2O extends AnAction {
17+
public class ObjectCopyMethodAction extends AnAction {
1818

1919
@Override
2020
public void actionPerformed(AnActionEvent anActionEvent) {
@@ -25,7 +25,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
2525

2626
@Override
2727
public void update(@NotNull AnActionEvent anActionEvent) {
28-
// 如果当前光标不在方法中,则不显示GernerateO2O组件
28+
// 如果当前光标不在方法中,则不显示Object Copy组件
2929
if (!check(PsiUtils.getCursorPsiMethod(anActionEvent))) {
3030
PsiUtils.setActionDisabled(anActionEvent);
3131
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.bigcoder.plugin.objecthelper.common.util;
2+
3+
import com.intellij.notification.NotificationDisplayType;
4+
import com.intellij.notification.NotificationGroup;
5+
import com.intellij.notification.NotificationType;
6+
import com.intellij.openapi.project.Project;
7+
8+
/**
9+
* @author: Jindong.Tian
10+
* @date: 2021-02-11
11+
**/
12+
public class NotificationUtils {
13+
14+
/**
15+
* 从2020.3版本方式,通知组改由Plugin.xml注册。详见:https://plugins.jetbrains.com/docs/intellij/notifications.html#top-level-notifications
16+
*/
17+
private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup("ObjectHelper Notification Group", NotificationDisplayType.BALLOON, true);
18+
19+
public static void notifyInfo(Project project, String content) {
20+
NOTIFICATION_GROUP.createNotification(content, NotificationType.INFORMATION).notify(project);
21+
}
22+
23+
public static void notifyWarning(Project project, String content) {
24+
NOTIFICATION_GROUP.createNotification(content, NotificationType.WARNING).notify(project);
25+
}
26+
27+
public static void notifyError(Project project, String content) {
28+
NOTIFICATION_GROUP.createNotification(content, NotificationType.ERROR).notify(project);
29+
}
30+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package cn.bigcoder.plugin.objecthelper.common.util;
2+
3+
import com.intellij.psi.PsiArrayType;
4+
import com.intellij.psi.PsiClass;
5+
import com.intellij.psi.PsiClassType;
6+
import com.intellij.psi.PsiType;
7+
import org.apache.commons.lang.time.DateFormatUtils;
8+
9+
import java.time.LocalDate;
10+
import java.time.LocalDateTime;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.Date;
13+
14+
/**
15+
* @author: Jindong.Tian
16+
* @date: 2021-02-12
17+
**/
18+
public class PsiTypeUtils {
19+
private static final String STRING_TYPE = "java.lang.String";
20+
private static final String INTEGER_TYPE = "java.lang.Integer";
21+
private static final String LONG_TYPE = "java.lang.Long";
22+
private static final String SHORT_TYPE = "java.lang.Short";
23+
private static final String BYTE_TYPE = "java.lang.Byte";
24+
private static final String DOUBLE_TYPE = "java.lang.Double";
25+
private static final String FLOAT_TYPE = "java.lang.Float";
26+
private static final String DATE_TYPE = "java.util.Date";
27+
private static final String LOCAL_DATE_TYPE = "java.time.LocalDate";
28+
private static final String LOCAL_DATE_TIME_TYPE = "java.time.LocalDateTime";
29+
30+
private static final String COLLECTION_TYPE = "java.util.Collection";
31+
private static final String OBJECT_TYPE = "java.lang.Object";
32+
33+
/**
34+
* 获取数据类型的默认值
35+
*
36+
* @param canonicalText 类的全限定名称
37+
* @return
38+
*/
39+
public static Object getDataTypeDefaultValue(String canonicalText) {
40+
if (StringUtils.isEmpty(canonicalText)) {
41+
return null;
42+
}
43+
switch (canonicalText) {
44+
case STRING_TYPE:
45+
return "";
46+
case INTEGER_TYPE:
47+
return 1;
48+
case LONG_TYPE:
49+
return 1L;
50+
case SHORT_TYPE:
51+
return (short) 1;
52+
case BYTE_TYPE:
53+
return (byte) 1;
54+
case DOUBLE_TYPE:
55+
return 1.0;
56+
case FLOAT_TYPE:
57+
return 1.0f;
58+
case DATE_TYPE:
59+
return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
60+
case LOCAL_DATE_TYPE:
61+
return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
62+
case LOCAL_DATE_TIME_TYPE:
63+
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
64+
default:
65+
return null;
66+
}
67+
}
68+
69+
/**
70+
* 判断是否是数据类型
71+
*
72+
* @param psiType
73+
* @return
74+
*/
75+
public static boolean isDataType(PsiType psiType) {
76+
String canonicalName = psiType.getCanonicalText();
77+
if (STRING_TYPE.equals(canonicalName)
78+
|| INTEGER_TYPE.equals(canonicalName)
79+
|| LONG_TYPE.equals(canonicalName)
80+
|| SHORT_TYPE.equals(canonicalName)
81+
|| BYTE_TYPE.equals(canonicalName)
82+
|| DOUBLE_TYPE.equals(canonicalName)
83+
|| FLOAT_TYPE.equals(canonicalName)
84+
|| DATE_TYPE.equals(canonicalName)
85+
|| LOCAL_DATE_TYPE.equals(canonicalName)
86+
|| LOCAL_DATE_TIME_TYPE.equals(canonicalName)) {
87+
return true;
88+
}
89+
return false;
90+
}
91+
92+
/**
93+
* 判断是否是数组类型
94+
*
95+
* @param psiType
96+
* @return
97+
*/
98+
public static boolean isArrayType(PsiType psiType) {
99+
return psiType instanceof PsiArrayType;
100+
}
101+
102+
/**
103+
* 判断是否是Collection类型
104+
*
105+
* @param psiType
106+
* @return
107+
*/
108+
public static boolean isCollectionType(PsiType psiType) {
109+
if (!(psiType instanceof PsiClassType)) {
110+
return false;
111+
}
112+
PsiClassType psiClassReferenceType = ((PsiClassType) psiType);
113+
PsiClass resolvePsiClass = psiClassReferenceType.resolve();
114+
if (resolvePsiClass == null) {
115+
return false;
116+
}
117+
if (OBJECT_TYPE.equals(resolvePsiClass.getQualifiedName())) {
118+
return false;
119+
}
120+
if (COLLECTION_TYPE.equals(resolvePsiClass.getQualifiedName())) {
121+
return true;
122+
}
123+
// 如果父类是Collection类型
124+
for (PsiType parentPsiType : ((PsiClassType) psiType).rawType().getSuperTypes()) {
125+
if (isCollectionType(parentPsiType)) {
126+
return true;
127+
}
128+
}
129+
return false;
130+
}
131+
132+
/**
133+
* 是否是Java官方类库
134+
*
135+
* @param psiType
136+
* @return
137+
*/
138+
public static boolean isJavaOfficialType(PsiType psiType) {
139+
return psiType.getCanonicalText().startsWith("java");
140+
}
141+
142+
}

0 commit comments

Comments
 (0)