Java Reflection Super Class getAllSuperTypeNames(Class aClass)

Here you can find the source of getAllSuperTypeNames(Class aClass)

Description

Gets all super-type names (super classes and super interfaces) of aClass and return them in an array.

License

Open Source License

Parameter

Parameter Description
aClass the class to be examined.

Return

an array of super-type names. This array will be zero-length if no super-types exist.

Declaration

public static String[] getAllSuperTypeNames(Class aClass) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2000, 2006 Visual Systems Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License version 2
 * which accompanies this distribution in a file named "COPYING".
 * /*from  www .j a  v a2s . c om*/
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *      
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *      
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *******************************************************************************/

import java.util.HashSet;
import java.util.Iterator;

import java.util.Set;

public class Main {
    /**
     * Gets all super-type names (super classes and super interfaces) of aClass and
     * return them in an array. 
     *
     * @param aClass the class to be examined.
     * 
     * @return an array of super-type names. This array will be zero-length if 
     *  no super-types exist.
     */
    public static String[] getAllSuperTypeNames(Class aClass) {
        Set<Class> types = new HashSet<Class>();
        getAllSuperTypes(aClass, types);
        String[] typeNames = new String[types.size()];
        Iterator<Class> iter = types.iterator();
        for (int i = 0; i < typeNames.length; i++) {
            typeNames[i] = iter.next().getName();
        }

        return typeNames;
    }

    /**
     * Gets all super-types (super classes and super interfaces) of aClass and
     * return them in an array. 
     *
     * @param aClass the class to be examined.
     * 
     * @return an array of super-types. This array will be zero-length if 
     *  no super-types exist.
     */
    public static Class[] getAllSuperTypes(Class aClass) {
        Set<Class> types = new HashSet<Class>();
        getAllSuperTypes(aClass, types);
        return types.toArray(new Class[types.size()]);
    }

    /**
     * Gets all super-types (super classes and super interfaces) of aClass and
     * adds them to the allSuperTypes set. 
     *
     * @param aClass the class to be examined.
     * @param allSuperTypes the set that will receive the super-types.
     */
    public static void getAllSuperTypes(Class aClass, Set<Class> allSuperTypes) {
        Class superType = aClass.getSuperclass();
        if (superType != null) {
            allSuperTypes.add(superType);
            getAllSuperTypes(superType, allSuperTypes);
        }

        for (Class intf : aClass.getInterfaces()) {
            allSuperTypes.add(intf);
            getAllSuperTypes(intf, allSuperTypes);
        }
    }
}

Related

  1. getAllSuperclasses(Class cls)
  2. getAllSuperclasses(Class cls)
  3. getAllSuperclasses(Class clazz)
  4. getAllSuperClasses(Class clazz)
  5. getAllSuperClasses(Class clz)
  6. getParents(final Class Class)
  7. getSuper(Class clazz, Set result)
  8. getSuperClasses(Class clazz, Set result)
  9. getSuperClasses(Class clz, boolean reverseOrder)