Java Reflection Method Invoke invokeMethod(Method method, Object target, Object... args)

Here you can find the source of invokeMethod(Method method, Object target, Object... args)

Description

Invoke the specified Method against the supplied target object with the supplied arguments.

License

Apache License

Parameter

Parameter Description
method the method to invoke
target the target object to invoke the method on
args the invocation arguments (may be null )

Return

the invocation result, if any

Declaration

public static Object invokeMethod(Method method, Object target, Object... args) 

Method Source Code

//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());
        }
    }
}

Related

  1. invokeMethod(Method method, Object object, Object... arguments)
  2. invokeMethod(Method method, Object target)
  3. invokeMethod(Method method, Object target)
  4. invokeMethod(Method method, Object target)
  5. invokeMethod(Method method, Object target, Class expectedType)
  6. invokeMethod(Method method, Object target, Object... args)
  7. invokeMethod(Method method, Object target, Object... arguments)
  8. invokeMethod(Method method, Object target, Object... params)
  9. invokeMethod(Method method, Object target, Object[] args)