集册 Java实例教程 创建从给定类到对象(不包括对象)的所有类层次结构

创建从给定类到对象(不包括对象)的所有类层次结构

欢马劈雪     最近更新时间:2020-01-02 10:19:05

405
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建从给定类到对象(不包括对象)的所有类层次结构
//nowjava - 时代Java 提供

/**

 * 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

 */

//package com.nowjava;


import java.util.ArrayList;


import java.util.List;


public class Main {

    public static void main(String[] argv) throws Exception {

        Class beanClass = String.class;

        System.out.println(getClassHierarchy(beanClass));

    }


    /**

     * 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<
展开阅读全文