001 package com.mockrunner.util.common;
002
003 import java.lang.reflect.Field;
004 import java.lang.reflect.Modifier;
005 import java.util.ArrayList;
006 import java.util.List;
007
008 public class FieldUtil
009 {
010 /**
011 * Returns all non-static fields declared by the specified class and its
012 * superclasses. The returned array contains the methods of all classes
013 * in the inheritance hierarchy, starting with the methods of the
014 * most general superclass, which is <code>java.lang.Object</code>.
015 * @param theClass the class whose methods are examined
016 * @return the array of field arrays
017 */
018 public static Field[][] getFieldsSortedByInheritanceHierarchy(Class theClass)
019 {
020 List hierarchyList = new ArrayList();
021 Class[] hierarchyClasses = ClassUtil.getInheritanceHierarchy(theClass);
022 for(int ii = 0; ii < hierarchyClasses.length; ii++)
023 {
024 addFieldsForClass(hierarchyList, hierarchyClasses[ii]);
025 }
026 return (Field[][])hierarchyList.toArray(new Field[hierarchyList.size()][]);
027 }
028
029 private static void addFieldsForClass(List hierarchyList, Class clazz)
030 {
031 List methodList = new ArrayList();
032 Field[] fields = clazz.getDeclaredFields();
033 for(int ii = 0; ii < fields.length; ii++)
034 {
035 if(!Modifier.isStatic(fields[ii].getModifiers()))
036 {
037 methodList.add(fields[ii]);
038 }
039 }
040 hierarchyList.add(methodList.toArray(new Field[methodList.size()]));
041 }
042 }