com.mockrunner.util.common.FieldUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-core Show documentation
Show all versions of mockrunner-core Show documentation
Core classes common to all Mockrunner modules
package com.mockrunner.util.common;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
public class FieldUtil
{
/**
* Returns all non-static fields declared by the specified class and its
* superclasses. The returned array contains the methods of all classes
* in the inheritance hierarchy, starting with the methods of the
* most general superclass, which is java.lang.Object
.
* @param theClass the class whose methods are examined
* @return the array of field arrays
*/
public static Field[][] getFieldsSortedByInheritanceHierarchy(Class> theClass)
{
List hierarchyList = new ArrayList<>();
Class>[] hierarchyClasses = ClassUtil.getInheritanceHierarchy(theClass);
for (Class> hierarchyClass : hierarchyClasses) {
addFieldsForClass(hierarchyList, hierarchyClass);
}
return hierarchyList.toArray(new Field[hierarchyList.size()][]);
}
private static void addFieldsForClass(List hierarchyList, Class> clazz)
{
List methodList = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())) {
methodList.add(field);
}
}
hierarchyList.add(methodList.toArray(new Field[methodList.size()]));
}
}