Here you can find the source of invokeMethod(Method method, Object target, Object... args)
Parameter | Description |
---|---|
method | the method to invoke |
target | the target object to invoke the method on |
args | the invocation arguments (may be null ) |
public static Object invokeMethod(Method method, Object target, Object... args)
//package com.java2s; /**/* w ww.j a va 2 s . c om*/ * Copyright 2008-2016 Juho Jeong * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * Invoke the specified {@link Method} against the supplied target object with the * supplied arguments. The target object can be {@code null} when invoking a * static {@link Method}. * * @param method the method to invoke * @param target the target object to invoke the method on * @param args the invocation arguments (may be {@code null}) * @return the invocation result, if any */ public static Object invokeMethod(Method method, Object target, Object... args) { try { return method.invoke(target, args); } catch (InvocationTargetException | IllegalAccessException e) { throw new IllegalStateException("Could not access method: " + e.getMessage()); } } }