集册 Java实例教程 为给定的源对象调用getter方法。

为给定的源对象调用getter方法。

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

750
为给定的源对象调用getter方法。


//package com.nowjava;/** 来 自 时 代      J a v a   公   众 号 - nowjava.com**/

import java.lang.reflect.Method;


public class Main {

    /**

     * Invoke the getterMethod for the given source object.

     *

     * @param getterMethod the getter method to invoke

     * @param source the source object to invoke the getter method on

     * @param property the getter method property name (used for logging)

     * @param path the full expression path (used for logging)

     * @return the getter result

     */

    public final static Object invokeGetter(Method getterMethod,

            Object source, String property, String path) {


        try {

            // Retrieve target object from getter

            return getterMethod.invoke(source, new Object[0]);


        } catch (Exception e) {

            // Log detailed error message of why getter failed

            StringBuilder buffer = new StringBuilder();

            buffer.append("Result: error occurred while trying to get");

            buffer.append(" instance of '");

            buffer.append(getterMethod.getReturnType().getName());
            /*
            nowjava 提供
            */

            buffer.append("' using method: '");

            buffer.
展开阅读全文