support print fields in parent class. #435

pull/966/head
lilizhaolilizhao 5 years ago committed by hengyunabc
parent 9f0212422d
commit 5850869843

@ -99,4 +99,14 @@ public class GlobalOptions {
)
public static volatile String jobTimeout = "1d";
/**
* parentfield
* @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;
}
}
}

Loading…
Cancel
Save