mirror of
https://github.com/alibaba/arthas.git
synced 2026-10-21 03:20:39 +00:00
support print fields in parent class. #435
This commit is contained in:
committed by
hengyunabc
parent
9f0212422d
commit
5850869843
@@ -99,4 +99,14 @@ public class GlobalOptions {
|
||||
)
|
||||
public static volatile String jobTimeout = "1d";
|
||||
|
||||
/**
|
||||
* 是否打印parent类里的field
|
||||
* @see com.taobao.arthas.core.view.ObjectView
|
||||
*/
|
||||
@Option(level = 1,
|
||||
name = "print-parent-fields",
|
||||
summary = "Option to print all fileds in parent class",
|
||||
description = "This option enables print files in parent class, default value true."
|
||||
)
|
||||
public static volatile boolean printParentFields = true;
|
||||
}
|
||||
|
||||
@@ -582,7 +582,22 @@ public class ObjectView implements View {
|
||||
appendStringBuilder(buf, format("@%s[%s]", className, obj));
|
||||
} else {
|
||||
appendStringBuilder(buf, format("@%s[", className));
|
||||
final Field[] fields = obj.getClass().getDeclaredFields();
|
||||
List<Field> fields = new ArrayList<Field>();
|
||||
Class objClass = obj.getClass();
|
||||
if (GlobalOptions.printParentFields) {
|
||||
// 当父类为null的时候说明到达了最上层的父类(Object类).
|
||||
while (objClass != null) {
|
||||
for (Field field : objClass.getDeclaredFields()) {
|
||||
fields.add(field);
|
||||
}
|
||||
objClass = objClass.getSuperclass();
|
||||
}
|
||||
} else {
|
||||
for (Field field : objClass.getDeclaredFields()) {
|
||||
fields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
if (null != fields) {
|
||||
for (Field field : fields) {
|
||||
|
||||
@@ -620,17 +635,6 @@ public class ObjectView implements View {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否根节点
|
||||
*
|
||||
* @param deep 深度
|
||||
* @return true:根节点 / false:非根节点
|
||||
*/
|
||||
private static boolean isRoot(int deep) {
|
||||
return deep == 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否展开当前深度的节点
|
||||
*
|
||||
|
||||
@@ -266,4 +266,41 @@ public class ObjectViewTest {
|
||||
return new NestedClass(code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示基类属性值
|
||||
*/
|
||||
@Test
|
||||
public void testObjectViewBaseFieldValue() {
|
||||
SonBean sonBean = new SonBean();
|
||||
sonBean.setI(10);
|
||||
sonBean.setJ("test");
|
||||
|
||||
ObjectView objectView = new ObjectView(sonBean, 3, 100);
|
||||
Assert.assertTrue(objectView.draw().contains("i=@Integer[10]"));
|
||||
}
|
||||
|
||||
private class BaseBean {
|
||||
private int i;
|
||||
|
||||
public int getI() {
|
||||
return i;
|
||||
}
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
}
|
||||
|
||||
private class SonBean extends BaseBean {
|
||||
private String j;
|
||||
|
||||
public String getJ() {
|
||||
return j;
|
||||
}
|
||||
|
||||
public void setJ(String j) {
|
||||
this.j = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user