Java Reflection Super Class getAllSuperClass(Class clazz, boolean includesObjectClass)

Here you can find the source of getAllSuperClass(Class clazz, boolean includesObjectClass)

Description

Returns all super class of the given class.

License

Apache License

Parameter

Parameter Description
clazz the given object class.
includesObjectClass if specifies <b>true</b>, the returned List will be inclued Object class, otherwise, specifies <b>false</b>

Return

an of Classes if they're existed, if not, returns an empty .

Declaration

public static List<Class<?>> getAllSuperClass(Class<?> clazz, boolean includesObjectClass) 

Method Source Code

//package com.java2s;
/*/* w  w w  .  j  av  a 2  s .  c  o m*/
 * Copyright 2007-2009 the original author or authors.
 *
 * 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. 
 * 
 * Project: JGentleFramework
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns all super class of the given class.
     * 
     * @param clazz
     *            the given object class.
     * @param includesObjectClass
     *            if specifies <b>true</b>, the returned {@link List} will be
     *            inclued {@link Object} class, otherwise, specifies
     *            <b>false</b>
     * @return an {@link List} of {@link Class Classes} if they're existed, if
     *         not, returns an empty {@link List}.
     */
    public static List<Class<?>> getAllSuperClass(Class<?> clazz, boolean includesObjectClass) {

        List<Class<?>> result = new ArrayList<Class<?>>();
        Class<?> temp = clazz.getSuperclass();
        while (temp != null) {
            if (includesObjectClass == false) {
                if (temp == Object.class) {
                    break;
                }
            }
            result.add(temp);
            temp = temp.getSuperclass();
        }
        return result;
    }
}

Related

  1. findSuperTypes(Class cls, Class endBefore)
  2. getAllSuperclasses(Class cls)
  3. getAllSuperclasses(Class cls)
  4. getAllSuperclasses(Class clazz)
  5. getAllSuperClasses(Class clazz)