From 1a74bb84abddfa17ff983d044fc45be8b7c2875f Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Tue, 14 Aug 2018 21:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=B0=83=E5=BC=8F=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EasyCodeGlobalVariableProvider.java | 2 +- .../java/com/sjhy/plugin/tool/GlobalTool.java | 58 +++++++++++++++++-- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sjhy/plugin/provider/EasyCodeGlobalVariableProvider.java b/src/main/java/com/sjhy/plugin/provider/EasyCodeGlobalVariableProvider.java index 37621df..d68ae5f 100644 --- a/src/main/java/com/sjhy/plugin/provider/EasyCodeGlobalVariableProvider.java +++ b/src/main/java/com/sjhy/plugin/provider/EasyCodeGlobalVariableProvider.java @@ -34,7 +34,7 @@ public class EasyCodeGlobalVariableProvider extends VtlGlobalVariableProvider { result.add(new VtlLightVariable("modulePath", file, CommonClassNames.JAVA_LANG_STRING)); result.add(new VtlLightVariable("importList", file, "java.util.Set")); result.add(new VtlLightVariable("callback", file, "com.sjhy.plugin.entity.Callback")); - result.add(new VtlLightVariable("tool", file, "com.sjhy.plugin.tool.NameUtils")); + result.add(new VtlLightVariable("tool", file, "com.sjhy.plugin.tool.GlobalTool")); result.add(new VtlLightVariable("time", file, "com.sjhy.plugin.tool.TimeUtils")); result.add(new VtlLightVariable("tableInfo", file, "com.sjhy.plugin.entity.TableInfo")); result.add(new VtlLightVariable("tableInfoList", file, "java.util.List")); diff --git a/src/main/java/com/sjhy/plugin/tool/GlobalTool.java b/src/main/java/com/sjhy/plugin/tool/GlobalTool.java index e0bcf36..b597760 100644 --- a/src/main/java/com/sjhy/plugin/tool/GlobalTool.java +++ b/src/main/java/com/sjhy/plugin/tool/GlobalTool.java @@ -1,11 +1,12 @@ package com.sjhy.plugin.tool; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.intellij.util.ExceptionUtil; import com.intellij.util.ReflectionUtil; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.*; /** @@ -40,6 +41,7 @@ public class GlobalTool extends NameUtils { } return globalTool; } + /** * jackson格式化工具 */ @@ -98,6 +100,21 @@ public class GlobalTool extends NameUtils { return ReflectionUtil.getField(cls, obj, Object.class, fieldName); } + /** + * 获取某个类的所有字段 + * + * @param cls 类 + * @return 所有字段 + */ + private List getAllFieldByClass(Class cls) { + List result = new ArrayList<>(); + do { + result.addAll(Arrays.asList(cls.getDeclaredFields())); + cls = cls.getSuperclass(); + } while (!cls.equals(Object.class)); + return result; + } + /** * 调式对象 * @@ -105,11 +122,40 @@ public class GlobalTool extends NameUtils { * @return 调式JSON结果 */ public String debug(Object obj) { - try { - return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); - } catch (JsonProcessingException e) { - ExceptionUtil.rethrow(e); + if (obj == null) { + return "null"; } - return null; + StringBuilder builder = new StringBuilder("\n/*调试信息:\nField Item List:\n"); + Class cls = obj.getClass(); + List fieldList = getAllFieldByClass(cls); + fieldList.forEach(field -> { + field.setAccessible(true); + builder.append("field="); + builder.append(field.getName()); + builder.append(",\t\ttype="); + builder.append(field.getType()); + builder.append(",\t\tvalue="); + try { + builder.append(field.get(obj)); + } catch (IllegalAccessException e) { + ExceptionUtil.rethrow(e); + } + builder.append("\n"); + }); + // 方法列表 + builder.append("\nMethod List:\n"); + // 排除方法 + List fillterMethodName = Arrays.asList("hashCode", "toString", "equals"); + for (Method method : cls.getDeclaredMethods()) { + if (fillterMethodName.contains(method.getName())) { + continue; + } + builder.append(method.getName()); + builder.append("="); + builder.append(method.toGenericString()); + builder.append("\n"); + } + builder.append("*/\n"); + return builder.toString(); } }