Skip to content

Commit 11153ac

Browse files
committed
优化动态调式功能
1 parent 2480410 commit 11153ac

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/main/java/com/sjhy/plugin/entity/DebugMethod.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public class DebugMethod {
1919
* 方法描述
2020
*/
2121
private String desc;
22+
/**
23+
* 执行方法得到的值
24+
*/
25+
private Object value;
2226
}

src/main/java/com/sjhy/plugin/tool/GlobalTool.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.IOException;
1212
import java.lang.reflect.Field;
13+
import java.lang.reflect.InvocationTargetException;
1314
import java.lang.reflect.Method;
1415
import java.util.*;
1516

@@ -137,6 +138,37 @@ public String debug(Object obj) throws JsonProcessingException {
137138
// 获取类
138139
Class<?> cls = obj.getClass();
139140
result.put("title", String.format("调试:%s", cls.getName()));
141+
// 方法列表
142+
List<DebugMethod> debugMethodList = new ArrayList<>();
143+
// 排除方法
144+
List<String> filterMethodName = Arrays.asList("hashCode", "toString", "equals", "getClass", "clone", "notify", "notifyAll", "wait", "finalize");
145+
for (Method method : cls.getMethods()) {
146+
if (filterMethodName.contains(method.getName())) {
147+
continue;
148+
}
149+
DebugMethod debugMethod = new DebugMethod();
150+
String methodName = method.getName();
151+
debugMethod.setName(methodName);
152+
debugMethod.setDesc(method.toGenericString());
153+
// 针对get,is开头的无参方法进行调用并取值。
154+
if ((methodName.startsWith("get") || methodName.startsWith("is"))) {
155+
if (method.getParameterCount() == 0) {
156+
try {
157+
Object val = method.invoke(obj);
158+
if (val != null) {
159+
debugMethod.setValue(val.toString());
160+
}
161+
} catch (IllegalAccessException | InvocationTargetException e) {
162+
ExceptionUtil.rethrow(e);
163+
}
164+
}
165+
}
166+
// 添加至列表
167+
debugMethodList.add(debugMethod);
168+
}
169+
result.put("methodList", debugMethodList);
170+
// 添加一条分割先
171+
result.put("----", "-----------------我是一条华丽的分割线-----------------");
140172
// 字段列表
141173
List<Field> fieldList = getAllFieldByClass(cls);
142174
List<DebugField> debugFieldList = new ArrayList<>();
@@ -159,19 +191,6 @@ public String debug(Object obj) throws JsonProcessingException {
159191
debugFieldList.add(debugField);
160192
});
161193
result.put("fieldList", debugFieldList);
162-
// 方法列表
163-
List<DebugMethod> debugMethodList = new ArrayList<>();
164-
// 排除方法
165-
List<String> filterMethodName = Arrays.asList("hashCode", "toString", "equals", "getClass", "clone", "notify", "notifyAll", "wait", "finalize");
166-
for (Method method : cls.getDeclaredMethods()) {
167-
if (filterMethodName.contains(method.getName())) {
168-
continue;
169-
}
170-
DebugMethod debugMethod = new DebugMethod();
171-
debugMethod.setName(method.getName());
172-
debugMethod.setDesc(method.toGenericString());
173-
}
174-
result.put("methodList", debugMethodList);
175194
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result).replace("\r\n", "\n");
176195
}
177196

@@ -202,7 +221,8 @@ public String serial() {
202221

203222
/**
204223
* 远程调用服务
205-
* @param name 服务名称
224+
*
225+
* @param name 服务名称
206226
* @param param 请求参数
207227
* @return 结果
208228
*/

0 commit comments

Comments
 (0)