集册 Java实例教程 获取设置器方法名称

获取设置器方法名称

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

531
获取设置器方法名称


import java.lang.reflect.Method;

import java.util.ArrayList;/*from 时 代 J a v a*/

import java.util.List;


public class Main{

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

        Class c = String.class;

        System.out.println(getSetterNames(c));

    }

    public static List<String> getSetterNames(Class<?> c) {

        List<Method> ls = BeanAccess.getAllMethods(c);

        List<String> ns = new ArrayList<String>();

        for (Method m : ls) {

            String n = m.getName();
            /* 
             来自 
            *NowJava.com - 时代Java*/

            int l = n.length();

            if (l > 3 && n.startsWith("set")) {

                char[] tmp = new char[l - 3];

                char t = n.charAt(3);

                if (t >= 65 && t <= 90) {

                    t += 32;

                }

                tmp[0] = t;

                for (int i = 1; i < l - 3; i++) {

                    tmp[i] = n.charAt(i + 3);

                }

                ns.add(new String(tmp));

            }

        }

        return ns;


    }

    public static List<String> getSetterNames(List<Method> methods) {

        //      List<Method> ls = BeanAccess.getAllMethods(c);

        List<String> ns = new ArrayList<String>();

        for (Method m : methods) {

            String n = m.getName();

            int l = n.length();

            if (l > 3 && n.startsWith("set")) {

                char[] tmp = new char[l - 3];

                char t = n.ch
展开阅读全文