Java Reflection Super Class getSuperclasses(Class clazz)

Here you can find the source of getSuperclasses(Class clazz)

Description

Returns a list of all superclasses and implemented interfaces by the supplied class, recursively to the base, up to but excluding Object.class.

License

Apache License

Declaration

public static List<Class<?>> getSuperclasses(Class<?> clazz) 

Method Source Code

//package com.java2s;
/**/* ww  w. j a  v  a  2s  .  c o  m*/
 * $Id: ClassLoaderUtils.java 2 2008-10-01 10:04:26Z azeckoski $
 * $URL: http://reflectutils.googlecode.com/svn/trunk/src/main/java/org/azeckoski/reflectutils/ClassLoaderUtils.java $
 * ClassLoaderUtils.java - genericdao - May 8, 2008 5:39:34 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski (azeckoski@gmail.com) (aaronz@vt.edu) (aaron@caret.cam.ac.uk)
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns a list of all superclasses and implemented interfaces by the supplied class,
     * recursively to the base, up to but excluding Object.class. These will be listed in order from
     * the supplied class, all concrete superclasses in ascending order, and then finally all
     * interfaces in recursive ascending order.<br/>
     * This will include duplicates if any superclasses implement the same classes 
     * 
     * Taken from PonderUtilCore around version 1.2.2
     *  - Antranig Basman (antranig@caret.cam.ac.uk)
     */
    public static List<Class<?>> getSuperclasses(Class<?> clazz) {
        List<Class<?>> accumulate = new ArrayList<Class<?>>();
        while (clazz != Object.class) {
            accumulate.add(clazz);
            clazz = clazz.getSuperclass();
        }
        int supers = accumulate.size();
        for (int i = 0; i < supers; ++i) {
            appendSuperclasses(accumulate.get(i), accumulate);
        }
        return accumulate;
    }

    /**
     * Taken from PonderUtilCore around version 1.2.2
     *  - Antranig Basman (antranig@caret.cam.ac.uk)
     */
    private static void appendSuperclasses(Class<?> clazz, List<Class<?>> accrete) {
        Class<?>[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; ++i) {
            accrete.add(interfaces[i]);
        }
        for (int i = 0; i < interfaces.length; ++i) {
            appendSuperclasses(interfaces[i], accrete);
        }
    }
}

Related

  1. getSuper(Class clazz, Set result)
  2. getSuperClasses(Class clazz, Set result)
  3. getSuperClasses(Class clz, boolean reverseOrder)
  4. getSuperclasses(Class from)
  5. getSuperClasses(Class c)
  6. getSuperClasses(Class clazz)
  7. getSuperclasses(Class clazz)
  8. getSuperClasses(Class cls)
  9. getSuperClasses(Class entryClass)