Dumping a java object's properties

后端 未结 10 1681
-上瘾入骨i
-上瘾入骨i 2020-11-28 02:03

Is there a library that will recursively dump/print an objects properties? I\'m looking for something similar to the console.dir() function in Firebug.

I\'m aware o

10条回答
  •  攒了一身酷
    2020-11-28 02:30

    I wanted an elegant solution to this problem that:

    • Does not use any external library
    • Uses Reflection to access fields, including superclass fields
    • Uses recursion to traverse the Object-graph with only one stack frame per call
    • Uses an IdentityHashMap to handle backwards references and avoid infinite recursion
    • Handles primitives, auto-boxing, CharSequences, enums, and nulls appropriately
    • Allows you to choose whether or not to parse static fields
    • Is simple enough to modify according to formatting preferences

    I wrote the following utility class:

    import java.lang.reflect.Array;
    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.util.IdentityHashMap;
    import java.util.Map.Entry;
    import java.util.TreeMap;
    
    /**
     * Utility class to dump {@code Object}s to string using reflection and recursion.
     */
    public class StringDump {
    
        /**
         * Uses reflection and recursion to dump the contents of the given object using a custom, JSON-like notation (but not JSON). Does not format static fields.

    * @see #dump(Object, boolean, IdentityHashMap, int) * @param object the {@code Object} to dump using reflection and recursion * @return a custom-formatted string representing the internal values of the parsed object */ public static String dump(Object object) { return dump(object, false, new IdentityHashMap(), 0); } /** * Uses reflection and recursion to dump the contents of the given object using a custom, JSON-like notation (but not JSON).

    * Parses all fields of the runtime class including super class fields, which are successively prefixed with "{@code super.}" at each level.

    * {@code Number}s, {@code enum}s, and {@code null} references are formatted using the standard {@link String#valueOf()} method. * {@code CharSequences}s are wrapped with quotes.

    * The recursive call invokes only one method on each recursive call, so limit of the object-graph depth is one-to-one with the stack overflow limit.

    * Backwards references are tracked using a "visitor map" which is an instance of {@link IdentityHashMap}. * When an existing object reference is encountered the {@code "sysId"} is printed and the recursion ends.

    * * @param object the {@code Object} to dump using reflection and recursion * @param isIncludingStatics {@code true} if {@code static} fields should be dumped, {@code false} to skip them * @return a custom-formatted string representing the internal values of the parsed object */ public static String dump(Object object, boolean isIncludingStatics) { return dump(object, isIncludingStatics, new IdentityHashMap(), 0); } private static String dump(Object object, boolean isIncludingStatics, IdentityHashMap visitorMap, int tabCount) { if (object == null || object instanceof Number || object instanceof Character || object instanceof Boolean || object.getClass().isPrimitive() || object.getClass().isEnum()) { return String.valueOf(object); } StringBuilder builder = new StringBuilder(); int sysId = System.identityHashCode(object); if (object instanceof CharSequence) { builder.append("\"").append(object).append("\""); } else if (visitorMap.containsKey(object)) { builder.append("(sysId#").append(sysId).append(")"); } else { visitorMap.put(object, object); StringBuilder tabs = new StringBuilder(); for (int t = 0; t < tabCount; t++) { tabs.append("\t"); } if (object.getClass().isArray()) { builder.append("[").append(object.getClass().getName()).append(":sysId#").append(sysId); int length = Array.getLength(object); for (int i = 0; i < length; i++) { Object arrayObject = Array.get(object, i); String dump = dump(arrayObject, isIncludingStatics, visitorMap, tabCount + 1); builder.append("\n\t").append(tabs).append("\"").append(i).append("\":").append(dump); } builder.append(length == 0 ? "" : "\n").append(length == 0 ? "" : tabs).append("]"); } else { // enumerate the desired fields of the object before accessing TreeMap fieldMap = new TreeMap(); // can modify this to change or omit the sort order StringBuilder superPrefix = new StringBuilder(); for (Class clazz = object.getClass(); clazz != null && !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (isIncludingStatics || !Modifier.isStatic(field.getModifiers())) { fieldMap.put(superPrefix + field.getName(), field); } } superPrefix.append("super."); } builder.append("{").append(object.getClass().getName()).append(":sysId#").append(sysId); for (Entry entry : fieldMap.entrySet()) { String name = entry.getKey(); Field field = entry.getValue(); String dump; try { boolean wasAccessible = field.isAccessible(); field.setAccessible(true); Object fieldObject = field.get(object); field.setAccessible(wasAccessible); // the accessibility flag should be restored to its prior ClassLoader state dump = dump(fieldObject, isIncludingStatics, visitorMap, tabCount + 1); } catch (Throwable e) { dump = "!" + e.getClass().getName() + ":" + e.getMessage(); } builder.append("\n\t").append(tabs).append("\"").append(name).append("\":").append(dump); } builder.append(fieldMap.isEmpty() ? "" : "\n").append(fieldMap.isEmpty() ? "" : tabs).append("}"); } } return builder.toString(); } }

    I tested it on a number of classes and for me it's extremely efficient. For example, try using it to dump the main thread:

    public static void main(String[] args) throws Exception {
        System.out.println(dump(Thread.currentThread()));
    }
    

    Edit

    Since writing this post I had reason to create an iterative version of this algorithm. The recursive version is limited in depth by total stack frames, but you might have reason to dump an extremely large object graph. To handle my situation, I revised the algorithm to use a stack data structure in place of the runtime stack. This version is time-efficient and is limited by heap size instead of stack frame depth.

    You can download and use the iterative version here.

提交回复
热议问题