Dumping a java object's properties

后端 未结 10 1687
-上瘾入骨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:25

    this will print out all fields (including arrays of objects) of an object.

    Fixed version of Ben Williams post from this thread

    Note: this method uses recursion so If you have a very deep object graph you may get a stack-overflow (no pun intended ;) IF so you need to use the VM parameter -Xss10m. If your using eclipse put it in run>runconfiguration>augments (tab) VM augment box and press apply

    import java.lang.reflect.Array;
    import java.lang.reflect.Field;
    
    public static String dump(Object o) {
        StringBuffer buffer = new StringBuffer();
        Class oClass = o.getClass();
         if (oClass.isArray()) {
             buffer.append("Array: ");
            buffer.append("[");
            for (int i = 0; i < Array.getLength(o); i++) {
                Object value = Array.get(o, i);
                if (value.getClass().isPrimitive() ||
                        value.getClass() == java.lang.Long.class ||
                        value.getClass() == java.lang.Integer.class ||
                        value.getClass() == java.lang.Boolean.class ||
                        value.getClass() == java.lang.String.class ||
                        value.getClass() == java.lang.Double.class ||
                        value.getClass() == java.lang.Short.class ||
                        value.getClass() == java.lang.Byte.class
                        ) {
                    buffer.append(value);
                    if(i != (Array.getLength(o)-1)) buffer.append(",");
                } else {
                    buffer.append(dump(value));
                 }
            }
            buffer.append("]\n");
        } else {
             buffer.append("Class: " + oClass.getName());
             buffer.append("{\n");
            while (oClass != null) {
                Field[] fields = oClass.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    fields[i].setAccessible(true);
                    buffer.append(fields[i].getName());
                    buffer.append("=");
                    try {
                        Object value = fields[i].get(o);
                        if (value != null) {
                            if (value.getClass().isPrimitive() ||
                                    value.getClass() == java.lang.Long.class ||
                                    value.getClass() == java.lang.String.class ||
                                    value.getClass() == java.lang.Integer.class ||
                                    value.getClass() == java.lang.Boolean.class ||
                                        value.getClass() == java.lang.Double.class ||
                                    value.getClass() == java.lang.Short.class ||
                                    value.getClass() == java.lang.Byte.class
                                    ) {
                                buffer.append(value);
                            } else {
                                buffer.append(dump(value));
                            }
                        }
                    } catch (IllegalAccessException e) {
                        buffer.append(e.getMessage());
                    }
                    buffer.append("\n");
                }
                oClass = oClass.getSuperclass();
            }
            buffer.append("}\n");
        }
        return buffer.toString();
    }
    

提交回复
热议问题