Java Reflection Super Class getSuperClasses(Class clazz, Set result)

Here you can find the source of getSuperClasses(Class clazz, Set result)

Description

get Super Classes

License

Open Source License

Declaration

private static void getSuperClasses(Class clazz, Set<Class> result) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.util.*;

public class Main {
    private static void getSuperClasses(Class clazz, Set<Class> result) {
        Class superClass = clazz.getSuperclass();
        if (superClass == null) {
            return;
        }// www.j a  va 2 s . co  m

        result.add(superClass);
        getSuper(superClass, result);
    }

    /**
     * Populates all interface and superclasses for the given class, recursivly.
     * @param clazz to reflect upon
     * @param result set of classes to populate
     */
    public static void getSuper(Class clazz, Set<Class> result) {
        getSuperInterfaces(clazz, result);
        getSuperClasses(clazz, result);
    }

    public static Class[] getSuperInterfaces(Class clazz) {
        Set<Class> interfaces = new HashSet<Class>();
        Class[] declaredInterfaces = clazz.getInterfaces();

        for (int i = 0; i < declaredInterfaces.length; i++) {
            interfaces.add(declaredInterfaces[i]);
            getSuperInterfaces(declaredInterfaces[i], interfaces);
        }

        Set<Class> superClasses = new HashSet<Class>();
        getSuperClasses(clazz, superClasses);
        for (Class superClass : superClasses) {
            declaredInterfaces = superClass.getInterfaces();

            for (int i = 0; i < declaredInterfaces.length; i++) {
                interfaces.add(declaredInterfaces[i]);
                getSuperInterfaces(declaredInterfaces[i], interfaces);
            }
        }

        return interfaces.toArray(new Class[declaredInterfaces.length]);
    }

    public static void getSuperInterfaces(Class clazz, Set<Class> result) {
        Class interfaces[] = clazz.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            result.add(interfaces[i]);
            getSuperInterfaces(interfaces[i], result);
        }
    }
}

Related

  1. getAllSuperClasses(Class clazz)
  2. getAllSuperClasses(Class clz)
  3. getAllSuperTypeNames(Class aClass)
  4. getParents(final Class Class)
  5. getSuper(Class clazz, Set result)
  6. getSuperClasses(Class clz, boolean reverseOrder)
  7. getSuperclasses(Class from)
  8. getSuperClasses(Class c)
  9. getSuperclasses(Class clazz)