Java Reflection Method Static Invoke invokeStaticMethod(final Method method, final Object... args)

Here you can find the source of invokeStaticMethod(final Method method, final Object... args)

Description

Invoke the specified static Method with the supplied arguments.

License

Apache License

Parameter

Parameter Description
method The method to invoke
args The invocation arguments (may be <code>null</code>)

Exception

Parameter Description
IllegalAccessException when unable to access the specified method because access modifiers prevent it

Return

The invocation result, if any

Declaration

public static Object invokeStaticMethod(final Method method, final Object... args)
        throws InvocationTargetException, IllegalAccessException 

Method Source Code

//package com.java2s;
/*/*from   w w  w. j a  v a 2s  . c om*/
 * Copyright (C) 2011 the original author or authors.
 *
 * 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;
import java.lang.reflect.Modifier;

public class Main {
    /**
     * Invoke the specified static {@link Method} with the supplied arguments.
     *
     * @param method The method to invoke
     * @param args The invocation arguments (may be <code>null</code>)
     * @return The invocation result, if any
     * @throws IllegalAccessException when unable to access the specified method because access modifiers prevent it
     * @throws java.lang.reflect.InvocationTargetException when a reflection invocation fails
     */
    public static Object invokeStaticMethod(final Method method, final Object... args)
            throws InvocationTargetException, IllegalAccessException {
        if (method == null) {
            throw new IllegalArgumentException("Method must not be null.");
        }
        if (!Modifier.isStatic(method.getModifiers())) {
            throw new IllegalArgumentException("Method must be static.");
        }
        method.setAccessible(true);
        return method.invoke(null, args);
    }
}

Related

  1. invokeStaticMethod(Class objectType, String name, Class paramTypes[], Object args[])
  2. invokeStaticMethod(Class pClass, String pMethod, Object pParam)
  3. invokeStaticMethod(final Class cls, final String methodName)
  4. invokeStaticMethod(final Class cls, final String methodName, final boolean throwException)
  5. invokeStaticMethod(final Class clazz, final String methodName, final Class[] parameterTypes, final Object[] arglist)
  6. invokeStaticMethod(String className, String method, Class[] paramTypes, Object[] paramValues)
  7. invokeStaticMethod(String className, String methodName)
  8. invokeStaticMethod(String className, String methodName, Object[] args)
  9. invokeStaticMethodNoArgs(Class clazz, String methodName)