集册 Java实例教程 返回与从目标检索componentClass实例的getter对应的Method对象。

返回与从目标检索componentClass实例的getter对应的Method对象。

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

470
返回与从目标检索componentClass实例的getter对应的Method对象。


//package com.nowjava;

import java.lang.reflect.Method;/* 来 自 时   代    Java - nowjava.com*/


public class Main {



    /**

     * Returns a Method object corresponding to a getter that retrieves an instance of componentClass from target.

     *

     * @param target         class that the getter should exist on

     * @param componentClass component to get

     * @return Method object, or null of one does not exist

     */

    public static Method getterMethod(Class<?> target,

            Class<?> componentClass) {

        try {

            return target.getMethod(getterName(componentClass));

        } catch (NoSuchMethodException e) {
        /*
        NowJava.com - 时  代  Java
        */

            //if (log.isTraceEnabled()) log.trace("Unable to find method " + getterName(componentClass) + " in class " + target);

            return null;

        } catch (NullPointerException e) {

            return null;

        }

    }


    /**

     * Returns a getter for a given class

     *

     * @param componentClass class to find getter for

     * @return name of getter method

     */

    public static String getterName(Class<?> componentClass) {

        
展开阅读全文