集册 Java实例教程 获取类访问器方法

获取类访问器方法

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

459
获取类访问器方法
/** from n o w  j a v a  . c o m**/

import java.lang.reflect.Method;


public class Main{

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

        Class clazz = String.class;

        String propertyName = "nowjava.com";

        System.out.println(getAccessor(clazz,propertyName));

    }

    public static final String[] ACCESSOR_PREFIXES = new String[] { "is",

            "get", "has" };

    public static Method getAccessor(Class clazz, String propertyName) {

        propertyName = StringHelper.capitalize(propertyName);

        for (String prefix : ACCESSOR_PREFIXES) {//来自 nowjava.com - 时代Java

            try {

                return clazz.getMethod(prefix + p
展开阅读全文