Java Utililty Methods Class Hierarchy Get

List of utility methods to do Class Hierarchy Get

Description

The list of methods to do Class Hierarchy Get are organized into topic(s).

Method

List>getClassHierarchy(Class beanClass)
Creates all class hierarchy from the given class to Object, exclusive, i.e.
List<Class<?>> classes = new ArrayList<Class<?>>();
Class<?> current = beanClass;
while (current != Object.class) {
    classes.add(current);
    current = current.getSuperclass();
return classes;
Class[]getClassHierarchy(Class type)
Returns an array of class objects representing the entire class hierarchy with the most-super class as the first element followed by all subclasses in the order they are declared.
ArrayDeque<Class<?>> classes = new ArrayDeque<Class<?>>();
Class<?> classType = type;
while (classType != null && !classType.equals(Object.class)) {
    classes.addFirst(classType);
    classType = classType.getSuperclass();
return classes.toArray(new Class[0]);
List>getClassHierarchy(Object obj, Class ignoreClass)
Gets the class hierarchy
List<Class<?>> ret = new ArrayList<Class<?>>();
if (null != obj) {
    Class<?> clazz = obj.getClass();
    while (null != clazz && (ignoreClass != clazz)) {
        ret.add(0, clazz);
        clazz = clazz.getSuperclass();
return ret;
intgetDistance(Class a, Class b)
get Distance
if (a.isInterface()) {
    return getDistanceToInterface(a, b);
Class curr = b;
int distance = 0;
while (curr != a) {
    distance++;
    curr = curr.getSuperclass();
...
intgetDistanceToInterface(Class to, Class from)
get Distance To Interface
Set<Class<?>> possibleCandidates = new LinkedHashSet<Class<?>>();
Class<?>[] interfaces = from.getInterfaces();
for (Class<?> interfase : interfaces) {
    if (to.equals(interfase)) {
        return 1;
    if (to.isAssignableFrom(interfase)) {
        possibleCandidates.add(interfase);
...
Class[]getTypeHierarchy(Class cls)
get Type Hierarchy
ArrayList<Class<?>> result = getTypeHierarchy(cls, new ArrayList<>());
return result.toArray(new Class<?>[result.size()]);