get All Fields from an object via reflection As String - Java Reflection

Java examples for Reflection:Field Get

Description

get All Fields from an object via reflection As String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object obj = "java2s.com";
        System.out.println(getAllFieldsAsString(obj));
    }/*from  www .  j  a v  a2s  .com*/

    public static String getAllFieldsAsString(final Object obj) {
        final StringBuilder sb = new StringBuilder();
        writeAllFieldsToBuffer(obj, sb);

        return sb.toString();
    }

    public static void writeAllFieldsToBuffer(final Object obj,
            final StringBuilder out) {
        final Class<?> thisClass = obj.getClass();

        for (final java.lang.reflect.Field f : thisClass.getFields()) {
            try {
                out.append(f.getName() + " = ");
                final Class<?> fType = f.getType();

                if (fType.isPrimitive()) {
                    final String n = fType.getName();
                    if (n.equals("boolean")) {
                        out.append(f.getBoolean(obj) + "\n");
                    } else if (n.equals("byte")) {
                        out.append(f.getByte(obj) + "\n");
                    } else if (n.equals("char")) {
                        out.append(f.getChar(obj) + "\n");
                    } else if (n.equals("double")) {
                        out.append(f.getDouble(obj) + "\n");
                    } else if (n.equals("float")) {
                        out.append(f.getFloat(obj) + "\n");
                    } else if (n.equals("int")) {
                        out.append(f.getInt(obj) + "\n");
                    } else if (n.equals("long")) {
                        out.append(f.getLong(obj) + "\n");
                    } else if (n.equals("short")) {
                        out.append(f.getShort(obj) + "\n");
                    } else {
                        throw new IllegalStateException(
                                "unknown primitive type: "
                                        + fType.getName());
                    }
                } else {
                    final Object val = f.get(obj);
                    out.append((val == null ? "null" : val.toString())
                            + "\n");
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            }
        }
    }
}

Related Tutorials