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

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

Description

Populates all interface and superclasses for the given class, recursivly.

License

Open Source License

Parameter

Parameter Description
clazz to reflect upon
result set of classes to populate

Declaration

public static void getSuper(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 {
    /**//from  w ww.jav a  2 s  . co m
     * 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);
        }
    }

    private static void getSuperClasses(Class clazz, Set<Class> result) {
        Class superClass = clazz.getSuperclass();
        if (superClass == null) {
            return;
        }

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

Related

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