print Fields from an Object via Reflection - Java Reflection

Java examples for Reflection:Field

Description

print Fields from an Object via Reflection

Demo Code

//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Object o = "java2s.com";
        StringBuilder sb = new StringBuilder();
        int indentation = 42;
        printFields(o, sb, indentation);
    }//from  w  w  w  .  j a  v  a2 s . c om

    private static final void printFields(Object o, StringBuilder sb,
            int indentation) {
        if (o == null) {
            sb.append("null");
            return;
        }
        if (indentation > 12) {
            sb.append(o);
            return;
        }
        if (o instanceof Object[]) {
            Object[] os = (Object[]) o;
            sb.append("(").append(os.length).append(")[\n");
            int i = 0;
            for (Object o2 : os) {
                generateIndentation(sb, indentation);
                sb.append(i++);
                sb.append(" => ");
                printFields(o2, sb, indentation + 2);
                sb.append("\n");
            }
            i = sb.length() - 1;
            if (os.length == 0)
                sb.setCharAt(i, ']');
            else {
                generateIndentation(sb, indentation - 2);
                sb.append(']');
            }
            return;
        }
        if (o instanceof Collection<?>) {
            Collection<? extends Object> os = (Collection<?>) o;
            sb.append("(").append(os.size()).append(")[\n");
            int i = 0;
            for (Object o2 : os) {
                generateIndentation(sb, indentation);
                sb.append(i++);
                sb.append(" => ");
                printFields(o2, sb, indentation + 2);
                sb.append("\n");
            }
            i = sb.length() - 1;
            if (os.size() == 0)
                sb.setCharAt(i, ']');
            else {
                generateIndentation(sb, indentation - 2);
                sb.append(']');
            }
            return;
        }
        if (o.getClass().getName().startsWith("java")
                && !o.getClass().getName().equals("java.lang.Object")) {
            sb.append(o);
            return;
        }
        sb.append("{\n");
        for (Field f : o.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if (f.isSynthetic() || (f.getModifiers() & Modifier.STATIC) > 0)
                continue;
            try {
                generateIndentation(sb, indentation);
                sb.append(f.getName()).append("=");
                printFields(f.get(o), sb, indentation + 2);
                sb.append("\n");
            } catch (Exception e) {
            }
        }
        int i = sb.length() - 2;
        if (sb.charAt(i) == '{')
            sb.setCharAt(i + 1, '}');
        else {
            generateIndentation(sb, indentation - 2);
            sb.append("}");
        }
    }

    private static final void generateIndentation(StringBuilder sb,
            int indentation) {
        while ((indentation = indentation - 2) >= 0) {
            sb.append("  ");
        }
    }
}

Related Tutorials