Java Class Hierarchy Get getClassHierarchy(Class type)

Here you can find the source of getClassHierarchy(Class type)

Description

Returns an array of class objects representing the entire class hierarchy with the most-super class as the first element followed by all subclasses in the order they are declared.

License

Apache License

Declaration

public static Class<?>[] getClassHierarchy(Class<?> type) 

Method Source Code

//package com.java2s;
/*/* w ww.ja v a2  s  .c om*/
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayDeque;

public class Main {
    /**
     * Returns an array of class objects representing the entire class hierarchy
     * with the most-super class as the first element followed by all subclasses
     * in the order they are declared. This method does not include the generic
     * Object type in its list. If this class represents the Object type, this
     * method will return a zero-size array.
     */
    public static Class<?>[] getClassHierarchy(Class<?> type) {
        ArrayDeque<Class<?>> classes = new ArrayDeque<Class<?>>();
        // class to start our search from, we'll loop thru the entire class hierarchy
        Class<?> classType = type;
        // keep searching up until we reach an Object class type
        while (classType != null && !classType.equals(Object.class)) {
            // keep adding onto front
            classes.addFirst(classType);
            classType = classType.getSuperclass();
        }
        return classes.toArray(new Class[0]);
    }
}

Related

  1. getClassHierarchy(Class beanClass)
  2. getClassHierarchy(Object obj, Class ignoreClass)
  3. getDistance(Class a, Class b)
  4. getDistanceToInterface(Class to, Class from)
  5. getTypeHierarchy(Class cls)