集册 Java实例教程 获取所有的二传手,包括超级班级

获取所有的二传手,包括超级班级

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

447
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取所有的二传手,包括超级班级


//package com.nowjava;

/**
来 自 时   代    Java - nowjava.com
**/

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;


public class Main {

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

        Class clazz = String.class;

        System.out.println(getAllSettersIncludeSuperClass(clazz));

    }


    private static final String SETMETHOD_PREFIX = "set";

    /*
    n o w j a v a . c o m 提 供
    */

    public static List<Method> getAllSettersIncludeSuperClass(

            final Class<?> clazz) {

        List<Method> ls = new ArrayList<Method>();

        for (Class<?> superClass = clazz; superClass != null

                && superClass != Object.class; superClass = superClass

                .getSuperclass()) {

            Method[] methods = superClass.getDeclaredMethods();

            for (int i = 0; i < methods.length; i++) {

                Method m = methods[i];

                if (m.getName().startsWith(SETMETHOD_PREFI
展开阅读全文