完善调式方法

This commit is contained in:
makejava 2018-08-14 21:20:15 +08:00
parent 2b2b3ca97e
commit 1a74bb84ab
2 changed files with 53 additions and 7 deletions

View File

@ -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<java.lang.String>"));
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<com.sjhy.plugin.entity.TableInfo>"));

View File

@ -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<Field> getAllFieldByClass(Class<?> cls) {
List<Field> 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<Field> 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<String> 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();
}
}