集册 Java实例教程 帮助程序方法,通过反射来调用方法并将任何异常包装为RuntimeException实例

帮助程序方法,通过反射来调用方法并将任何异常包装为RuntimeException实例

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

414
帮助程序方法,通过反射来调用方法并将任何异常包装为RuntimeException实例
/**来 自 nowjava - 时  代  Java**/


//package com.nowjava;


import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


public class Main {

    /**

     * A helper method to invoke a method via reflection and wrap any exceptions

     * as {@link RuntimeException} instances

     *

     * @param method     the method to invoke

     * @param instance   the object instance (or null for static methods)

     * @param parameters the parameters to the method

     * @return the result of the method invocation

     */

    public static Object invokeMethod(Method method, Object instance,

            Object... parameters) {

        if (method == null) {

            return null;

        }

        try {

            return method.invoke(instance, parameters);/** n o w    j a v a  . c o m 提 供 **/

        } catch (IllegalAccessException e) {

            throw 
展开阅读全文