Java Reflection Interface Get getAllClassesAndInterfaces(Class startClass)

Here you can find the source of getAllClassesAndInterfaces(Class startClass)

Description

Find all super classes and implemented interfaces for the given class.

License

Open Source License

Parameter

Parameter Description
startClass The class

Return

A List of Class objects

Declaration


public static List getAllClassesAndInterfaces(Class startClass) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//  ww w.  j a va  2 s .  com
     * Find all super classes and implemented interfaces for the given
     * class.
     *
     * @param startClass The class
     * @return A List of Class objects
     */

    public static List getAllClassesAndInterfaces(Class startClass) {
        ArrayList classes = new ArrayList();

        addClassesAndInterfaces(startClass, classes);

        return classes;
    }

    /**
     * Add all super classes and interfaces of the given class to the given
     * List.
     *
     * @param c       The Class
     * @param classes An List of Classes
     */

    protected static void addClassesAndInterfaces(Class c, List classes) {
        if (c != null) {
            Class superClass = c.getSuperclass();
            Class[] interfaces = c.getInterfaces();

            if (superClass != null && !classes.contains(superClass)) {
                classes.add(superClass);
            }

            for (Class anInterface : interfaces) {
                if (anInterface != null && !classes.contains(anInterface)) {
                    classes.add(anInterface);
                }
            }

            addClassesAndInterfaces(superClass, classes);
            for (Class anInterface : interfaces) {
                addClassesAndInterfaces(anInterface, classes);
            }
        }
    }
}

Related

  1. addInterfaceName(String name)
  2. equalInterfaces(Object obj1, Object obj2)
  3. getAllInterfaces(Class clazz)
  4. getAllInterfaces(Class clazz)
  5. getAllInterfaces(Class clazz)
  6. getAllInterfaces(Class cls)