Compare commits
No commits in common. "master" and "v1.0.0" have entirely different histories.
@ -31,9 +31,4 @@ Toolkit
|
|||||||
更新
|
更新
|
||||||
----
|
----
|
||||||
|
|
||||||
欢迎关注公众号:liuzhihangs
|
|
||||||
|
|
||||||
|
|
||||||
<img src="https://liuzhihang.com/oss/pic/wechat.jpg" width="20%" height="20%" />
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'org.jetbrains.intellij' version '0.4.21'
|
id 'org.jetbrains.intellij' version '0.4.15'
|
||||||
}
|
}
|
||||||
|
|
||||||
group 'com.liuzhihang.toolkit'
|
group 'com.liuzhihang.toolkit'
|
||||||
version '1.0.4'
|
version '1.0.0'
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,8 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
|
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
|
||||||
<ul>
|
<ul>
|
||||||
<li>1.0.4
|
<li>1.0.0
|
||||||
<ol>
|
<ol>
|
||||||
<li>修复部分字段丢失的bug</li>
|
<li>在Java Entity 中右键复制为Json</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>
|
</ol>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
|||||||
@ -17,14 +17,5 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li>将JavaBean复制为Json字符串</li>
|
<li>将JavaBean复制为Json字符串</li>
|
||||||
</ul>
|
</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>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@ -11,19 +11,16 @@ import com.intellij.openapi.actionSystem.PlatformDataKeys;
|
|||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.util.InheritanceUtil;
|
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
import com.intellij.psi.util.PsiTypesUtil;
|
import com.intellij.psi.util.PsiTypesUtil;
|
||||||
import com.intellij.psi.util.PsiUtil;
|
import com.intellij.psi.util.PsiUtil;
|
||||||
import com.liuzhihang.toolkit.utils.GsonFormatUtil;
|
import com.liuzhihang.toolkit.utils.GsonFormatUtil;
|
||||||
import org.jetbrains.annotations.NonNls;
|
import org.jetbrains.annotations.NonNls;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.datatransfer.Clipboard;
|
import java.awt.datatransfer.Clipboard;
|
||||||
import java.awt.datatransfer.StringSelection;
|
import java.awt.datatransfer.StringSelection;
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -53,7 +50,7 @@ public class CopyAsJsonAction extends AnAction {
|
|||||||
PROPERTIES_TYPES.put("Boolean", false);
|
PROPERTIES_TYPES.put("Boolean", false);
|
||||||
// 其他
|
// 其他
|
||||||
PROPERTIES_TYPES.put("String", "");
|
PROPERTIES_TYPES.put("String", "");
|
||||||
PROPERTIES_TYPES.put("BigDecimal", BigDecimal.ZERO);
|
PROPERTIES_TYPES.put("BigDecimal", null);
|
||||||
PROPERTIES_TYPES.put("Date", null);
|
PROPERTIES_TYPES.put("Date", null);
|
||||||
PROPERTIES_TYPES.put("LocalDate", null);
|
PROPERTIES_TYPES.put("LocalDate", null);
|
||||||
PROPERTIES_TYPES.put("LocalTime", null);
|
PROPERTIES_TYPES.put("LocalTime", null);
|
||||||
@ -71,22 +68,12 @@ public class CopyAsJsonAction extends AnAction {
|
|||||||
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
|
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
|
||||||
Editor editor = e.getData(CommonDataKeys.EDITOR);
|
Editor editor = e.getData(CommonDataKeys.EDITOR);
|
||||||
|
|
||||||
if (editor == null || project == null || psiFile == null) {
|
PsiElement referenceAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
|
||||||
return;
|
PsiClass selectedClass = (PsiClass) PsiTreeUtil.getContextOfType(referenceAt, new Class[]{PsiClass.class});
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
Map<String, Object> fieldsMap = getFields(selectedClass);
|
Map fieldsMap = getFields(selectedClass);
|
||||||
|
|
||||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
Gson gson = new GsonBuilder().create();
|
||||||
String json = GsonFormatUtil.gsonFormat(gson, fieldsMap);
|
String json = GsonFormatUtil.gsonFormat(gson, fieldsMap);
|
||||||
|
|
||||||
// 使用自定义缩进格式 String json = new GsonBuilder().setPrettyPrinting().create().toJson(fieldsMap);
|
// 使用自定义缩进格式 String json = new GsonBuilder().setPrettyPrinting().create().toJson(fieldsMap);
|
||||||
@ -103,25 +90,13 @@ 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);
|
|
||||||
|
|
||||||
return target instanceof SyntheticElement ? null : target;
|
public static Map getFields(PsiClass psiClass) {
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, Object> getFields(PsiClass psiClass) {
|
|
||||||
|
|
||||||
Map<String, Object> fieldMap = new LinkedHashMap<>();
|
Map<String, Object> fieldMap = new LinkedHashMap<>();
|
||||||
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();
|
// Map<String, Object> commentFieldMap = new LinkedHashMap<>();
|
||||||
|
|
||||||
if (psiClass != null && !psiClass.isEnum() && !psiClass.isInterface() && !psiClass.isAnnotationType()) {
|
if (psiClass != null && psiClass.getClassKind() == JvmClassKind.CLASS) {
|
||||||
for (PsiField field : psiClass.getAllFields()) {
|
for (PsiField field : psiClass.getAllFields()) {
|
||||||
PsiType type = field.getType();
|
PsiType type = field.getType();
|
||||||
String name = field.getName();
|
String name = field.getName();
|
||||||
@ -155,7 +130,7 @@ public class CopyAsJsonAction extends AnAction {
|
|||||||
list.add(getFields(PsiUtil.resolveClassInType(deepType)));
|
list.add(getFields(PsiUtil.resolveClassInType(deepType)));
|
||||||
}
|
}
|
||||||
fieldMap.put(name, list);
|
fieldMap.put(name, list);
|
||||||
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_COLLECTION)) {
|
} else if (fieldTypeName.startsWith("List") || fieldTypeName.startsWith("ArrayList") || fieldTypeName.startsWith("Set") || fieldTypeName.startsWith("HashSet")) {
|
||||||
// List Set or HashSet
|
// List Set or HashSet
|
||||||
List<Object> list = new ArrayList<>();
|
List<Object> list = new ArrayList<>();
|
||||||
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
|
PsiType iterableType = PsiUtil.extractIterableTypeParameter(type, false);
|
||||||
@ -169,10 +144,10 @@ public class CopyAsJsonAction extends AnAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fieldMap.put(name, list);
|
fieldMap.put(name, list);
|
||||||
} else if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) {
|
} else if (fieldTypeName.startsWith("HashMap") || fieldTypeName.startsWith("Map")) {
|
||||||
// HashMap or Map
|
// HashMap or Map
|
||||||
fieldMap.put(name, new HashMap<>(4));
|
fieldMap.put(name, new HashMap<>(4));
|
||||||
} else if (psiClass.isEnum() || psiClass.isInterface() || psiClass.isAnnotationType()) {
|
} else if (PsiUtil.resolveClassInType(type).getClassKind() != JvmClassKind.CLASS) {
|
||||||
// enum or interface
|
// enum or interface
|
||||||
fieldMap.put(name, "");
|
fieldMap.put(name, "");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -18,9 +18,9 @@
|
|||||||
|
|
||||||
<actions>
|
<actions>
|
||||||
<action id="Toolkit.Json.CopyAsJsonAction" class="com.liuzhihang.toolkit.action.CopyAsJsonAction"
|
<action id="Toolkit.Json.CopyAsJsonAction" class="com.liuzhihang.toolkit.action.CopyAsJsonAction"
|
||||||
text="Copy As Json" description="Copy Java Bean as json string">
|
text="Copy As Json" description="Copy As Json">
|
||||||
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
|
||||||
<keyboard-shortcut keymap="$default" first-keystroke="shift meta J"/>
|
<keyboard-shortcut keymap="$default" first-keystroke="ctrl shift J"/>
|
||||||
</action>
|
</action>
|
||||||
</actions>
|
</actions>
|
||||||
</idea-plugin>
|
</idea-plugin>
|
||||||
Loading…
Reference in New Issue
Block a user