Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d0dd90adb4 | ||
|
|
00076c7266 | ||
|
|
2708122e0a | ||
|
|
910c4a9175 | ||
|
|
5ebc83b408 | ||
|
|
c600c65431 |
@ -31,4 +31,9 @@ Toolkit
|
||||
更新
|
||||
----
|
||||
|
||||
欢迎关注公众号:liuzhihangs
|
||||
|
||||
|
||||
<img src="https://liuzhihang.com/oss/pic/wechat.jpg" width="20%" height="20%" />
|
||||
|
||||
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.jetbrains.intellij' version '0.4.15'
|
||||
id 'org.jetbrains.intellij' version '0.4.21'
|
||||
}
|
||||
|
||||
group 'com.liuzhihang.toolkit'
|
||||
version '1.0.0'
|
||||
version '1.0.4'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
|
||||
<ul>
|
||||
<li>1.0.0
|
||||
<li>1.0.4
|
||||
<ol>
|
||||
<li>在Java Entity 中右键复制为Json</li>
|
||||
<li>修复部分字段丢失的bug</li>
|
||||
<li>修改支持 2018.3 及以上版本</li>
|
||||
</ol>
|
||||
<ol>
|
||||
<li>Fix the bug that some fields are missing</li>
|
||||
<li>Modified support 2018.3 and above</li>
|
||||
</ol>
|
||||
</li>
|
||||
|
||||
|
||||
@ -17,5 +17,14 @@
|
||||
<ul>
|
||||
<li>将JavaBean复制为Json字符串</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Copy JavaBean as Json string</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>公众号
|
||||
<ul>
|
||||
<li>liuzhihangs</li>
|
||||
</ul>
|
||||
<img src="https://liuzhihang.com/oss/pic/wechat.jpg" width="20%" height="20%" />
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
@ -11,16 +11,19 @@ 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.InheritanceUtil;
|
||||
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 org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@ -50,7 +53,7 @@ public class CopyAsJsonAction extends AnAction {
|
||||
PROPERTIES_TYPES.put("Boolean", false);
|
||||
// 其他
|
||||
PROPERTIES_TYPES.put("String", "");
|
||||
PROPERTIES_TYPES.put("BigDecimal", null);
|
||||
PROPERTIES_TYPES.put("BigDecimal", BigDecimal.ZERO);
|
||||
PROPERTIES_TYPES.put("Date", null);
|
||||
PROPERTIES_TYPES.put("LocalDate", null);
|
||||
PROPERTIES_TYPES.put("LocalTime", null);
|
||||
@ -68,12 +71,22 @@ public class CopyAsJsonAction extends AnAction {
|
||||
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);
|
||||
if (editor == null || project == null || psiFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Gson gson = new GsonBuilder().create();
|
||||
PsiClass selectedClass = getTargetClass(editor, psiFile);
|
||||
|
||||
if (selectedClass == null) {
|
||||
Notification error = NOTIFICATION_GROUP.createNotification("Please use in java class file", NotificationType.ERROR);
|
||||
Notifications.Bus.notify(error, project);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, Object> fieldsMap = getFields(selectedClass);
|
||||
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
String json = GsonFormatUtil.gsonFormat(gson, fieldsMap);
|
||||
|
||||
// 使用自定义缩进格式 String json = new GsonBuilder().setPrettyPrinting().create().toJson(fieldsMap);
|
||||
@ -90,13 +103,25 @@ public class CopyAsJsonAction extends AnAction {
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiClass getTargetClass(@NotNull Editor editor, @NotNull PsiFile file) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
if (element != null) {
|
||||
// 当前类
|
||||
PsiClass target = PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
|
||||
public static Map getFields(PsiClass psiClass) {
|
||||
return target instanceof SyntheticElement ? null : target;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getFields(PsiClass psiClass) {
|
||||
|
||||
Map<String, Object> fieldMap = new LinkedHashMap<>();
|
||||
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();
|
||||
|
||||
if (psiClass != null && psiClass.getClassKind() == JvmClassKind.CLASS) {
|
||||
if (psiClass != null && !psiClass.isEnum() && !psiClass.isInterface() && !psiClass.isAnnotationType()) {
|
||||
for (PsiField field : psiClass.getAllFields()) {
|
||||
PsiType type = field.getType();
|
||||
String name = field.getName();
|
||||
@ -130,7 +155,7 @@ public class CopyAsJsonAction extends AnAction {
|
||||
list.add(getFields(PsiUtil.resolveClassInType(deepType)));
|
||||
}
|
||||
fieldMap.put(name, list);
|
||||
} else if (fieldTypeName.startsWith("List") || fieldTypeName.startsWith("ArrayList") || fieldTypeName.startsWith("Set") || fieldTypeName.startsWith("HashSet")) {
|
||||
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION)) {
|
||||
// List Set or HashSet
|
||||
List<Object> list = new ArrayList<>();
|
||||
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
|
||||
@ -144,10 +169,10 @@ public class CopyAsJsonAction extends AnAction {
|
||||
}
|
||||
}
|
||||
fieldMap.put(name, list);
|
||||
} else if (fieldTypeName.startsWith("HashMap") || fieldTypeName.startsWith("Map")) {
|
||||
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
|
||||
// HashMap or Map
|
||||
fieldMap.put(name, new HashMap<>(4));
|
||||
} else if (PsiUtil.resolveClassInType(type).getClassKind() != JvmClassKind.CLASS) {
|
||||
} else if (psiClass.isEnum() || psiClass.isInterface() || psiClass.isAnnotationType()) {
|
||||
// enum or interface
|
||||
fieldMap.put(name, "");
|
||||
} else {
|
||||
|
||||
@ -18,9 +18,9 @@
|
||||
|
||||
<actions>
|
||||
<action id="Toolkit.Json.CopyAsJsonAction" class="com.liuzhihang.toolkit.action.CopyAsJsonAction"
|
||||
text="Copy As Json" description="Copy As Json">
|
||||
text="Copy As Json" description="Copy Java Bean as json string">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
||||
<keyboard-shortcut keymap="$default" first-keystroke="ctrl shift J"/>
|
||||
<keyboard-shortcut keymap="$default" first-keystroke="shift meta J"/>
|
||||
</action>
|
||||
</actions>
|
||||
</idea-plugin>
|
||||
Loading…
Reference in New Issue
Block a user