Java examples for Reflection:Field
write All Fields from an object via reflection To Buffer
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { Object obj = "java2s.com"; StringBuilder out = new StringBuilder(); writeAllFieldsToBuffer(obj, out); }/*from w ww . ja va 2 s .c o m*/ 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) { } } } }