Mega Code Archive

 
Categories / Java / Reflection
 

All Fields Snippet

/* Java Reflection in Action Ira R. Forman and Nate Forman ISBN 1932394184 Publisher: Manning Publications Co. */ import java.lang.reflect.Field; import java.util.LinkedList; import java.util.List; public class AllFieldsSnippet {   public static void main(String[] args) {     Object obj = new Object();     //start extract AllFieldsSnippet     Class cls = obj.getClass();     List accum = new LinkedList();     while (cls != null) {       Field[] f = cls.getDeclaredFields();       for (int i = 0; i < f.length; i++) {         accum.add(f[i]);       }       cls = cls.getSuperclass();     }     Field[] allFields = (Field[]) accum.toArray(new Field[accum.size()]);     //stop extract AllFieldsSnippet   } }