get All Super classes - Java Reflection

Java examples for Reflection:Super Class

Description

get All Super classes

Demo Code


import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class clazz = String.class;
        System.out.println(getAllSuperclasses(clazz));
    }/*from www  .  j a  va  2 s.  c  o  m*/
    
    public static List<Class<?>> getAllSuperclasses(Class<?> clazz) {
        if (clazz == null) {
            return null;
        }
        List<Class<?>> classes = CollectionUtil.createArrayList();
        Class<?> superclass = clazz.getSuperclass();
        while (superclass != null && superclass != Object.class) {
            classes.add(superclass);
            superclass = superclass.getSuperclass();
        }
        return classes;
    }
}

Related Tutorials