我制作了一个小型 Java 程序,它使用反射来检查对象的字段并递归地遍历其结构,以便构建其所有成员的树,类似于调试器在检查变量时所做的事情。
最大的问题是有时会出现循环引用。我的树变成了图表!然后我的程序进入一个无限循环并最终抛出 StackOverflow 异常(哦,具有讽刺意味!)
我的问题是......如何将任意对象“标记”为已访问以实现任何标准的图形横向算法?我不能使用 hashCode() 因为任何对象都可以用作输入,并且不同的对象可以返回相同的哈希码。有什么线索吗?
先谢谢了!
public class ClassHierarchyItem {
private boolean parent;
private String id;
private String parentId;
private String name;
private String type;
private String value;
public ClassHierarchyItem(boolean parent, String id, String parentId, String name, String type, String value){
this.parent = parent;
this.id = id;
this.parentId = parentId;
this.name = name;
this.type = type;
this.value = value;
}
public String toString() {
return (isParent() ? "+" : "") + name + " - " + type + " - " + value + " [id=" + id + ", pId=" + parentId + "]";
}
//Getters and setters follow (cutted)
}
public class ClassHierarchyNavigator {
public static void main(String[] args) {
Integer i = 123;
// Fails with StackOverflow exception (some reference inside Integer points back to base object)
System.out.println(renderHirearchy(i));
}
public static List<ClassHierarchyItem> renderHirearchy(Object o) {
List<ClassHierarchyItem> items = new ArrayList<ClassHierarchyItem>();
boolean parent = (o.getClass().getDeclaredFields().length > 0 && !o.getClass().isPrimitive() && o.getClass() != String.class)
|| o.getClass().isArray();
buildObjectTree(items, o, parent, "root", o.getClass().getName(), "r", "");
return items;
}
private static boolean isParent(Field field) {
return (field.getClass().getDeclaredFields().length > 0 && !field.getType().isPrimitive() && field.getType() != String.class)
|| field.getType().isArray();
}
private static void buildObjectTree(List<ClassHierarchyItem> items, Object object, boolean parent,
String objectName, String objectType, String objectId, String parentId) {
long subItemCount = 1;
String value = object == null ? "null" : object.toString();
ClassHierarchyItem item = new ClassHierarchyItem(parent, objectId, parentId, objectName, objectType,
value.substring(0, value.length() > 80 ? 80 : value.length()));
items.add(item);
if (!parent) {
return;
}
// if (isArray) {
// do_array_treatment
// } else {
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object child;
try {
child = field.get(object);
} catch (IllegalArgumentException e) {
continue;
} catch (IllegalAccessException e) {
continue;
}
String childId = objectId + "-" + subItemCount++;
String fieldName = field.getName();
boolean childIsParent = child != null && !"this$0".equals(fieldName) && isParent(field);
buildObjectTree(items, child, childIsParent, fieldName, field.getType().getName(), childId, objectId);
}
}
}