Java Class Hierarchy Get getClassHierarchy(Class beanClass)

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

Description

Creates all class hierarchy from the given class to Object, exclusive, i.e.

License

Open Source License

Parameter

Parameter Description
beanClass class for building its hierarchy

Return

list of all classes from the given class up to

Declaration

public static List<Class<?>> getClassHierarchy(Class<?> beanClass) 

Method Source Code

//package com.java2s;
/**//w  w w  . j  a va2  s  . c  om
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Creates all class hierarchy from the given class to Object,
     * <b>exclusive</b>, i.e. Object won't go to the list). Interfaces won't be
     * listed in the result.
     * 
     * @param beanClass class for building its hierarchy
     * @return list of all classes from the given class up to {@link Object}
     */
    public static List<Class<?>> getClassHierarchy(Class<?> beanClass) {
        List<Class<?>> classes = new ArrayList<Class<?>>();

        Class<?> current = beanClass;
        while (current != Object.class) {
            classes.add(current);
            current = current.getSuperclass();
        }

        return classes;
    }
}

Related

  1. getClassHierarchy(Class type)
  2. getClassHierarchy(Object obj, Class ignoreClass)
  3. getDistance(Class a, Class b)
  4. getDistanceToInterface(Class to, Class from)