Java Reflection Super Class getSuperClasses(Class clazz)

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

Description

Get super class list of a class.

License

Apache License

Parameter

Parameter Description
clazz the class to start traversal from

Return

a list of super class where the direct super class of the provided class is the first member of the list.
For , primitives and interfaces this returns an empty list.
For arrays this returns a list containing only .
For enums this returns a list containing and

Declaration

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

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| Licensed under the Apache License, Version 2.0 (the "License");          |
| you may not use this file except in compliance with the License.         |
| You may obtain a copy of the License at                                  |
|                                                                          |
|    http://www.apache.org/licenses/LICENSE-2.0                            |
|                                                                          |
| Unless required by applicable law or agreed to in writing, software      |
| distributed under the License is distributed on an "AS IS" BASIS,        |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and      |
| limitations under the License.                                           |
+-------------------------------------------------------------------------*/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//w  w  w  .  j  a v  a 2s .com
     * Get super class list of a class.
     * 
     * @param clazz
     *            the class to start traversal from
     * @return a list of super class where the direct super class of the
     *         provided class is the first member of the list. <br>
     *         For {@link Object}, primitives and interfaces this returns an
     *         empty list. <br>
     *         For arrays this returns a list containing only {@link Object}. <br>
     *         For enums this returns a list containing {@link Enum} and
     *         {@link Object}
     */
    public static List<Class<?>> getSuperClasses(Class<?> clazz) {
        ArrayList<Class<?>> superClasses = new ArrayList<Class<?>>();
        findSuperClasses(clazz, superClasses);
        return superClasses;
    }

    /**
     * Recursively add super classes to a list.
     * 
     * @param clazz
     *            class to start from
     * @param superClasses
     *            list to store super classes.
     */
    private static void findSuperClasses(Class<?> clazz, List<Class<?>> superClasses) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass == null) {
            return;
        }
        superClasses.add(superClass);
        findSuperClasses(superClass, superClasses);
    }
}

Related

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