Java Reflection Method Static Invoke invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)

Here you can find the source of invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)

Description

Invokes a static method on a class.

License

Apache License

Parameter

Parameter Description
clazz the class to invoke the method on
methodName the method name
argTypes the method argument types
args the method arguments

Exception

Parameter Description
Exception if anything goes wrong

Return

the method result

Declaration

public static Object invokeStatic(final Class<?> clazz, final String methodName, final Class<?>[] argTypes,
        final Object[] args) throws Exception 

Method Source Code


//package com.java2s;
/*//from w ww. j  ava2  s.  c  o  m
 * Copyright 2014 Apigee Corporation
 *
 * 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.Method;

public class Main {
    /**
     * Invokes a static method on a class.
     *
     * @param clazz the class to invoke the method on
     * @param methodName the method name
     * @param argTypes the method argument types
     * @param args the method arguments
     *
     * @return the method result
     *
     * @throws Exception if anything goes wrong
     */
    public static Object invokeStatic(final Class<?> clazz, final String methodName, final Class<?>[] argTypes,
            final Object[] args) throws Exception {
        final Method method = clazz.getMethod(methodName, argTypes);

        return method.invoke(null, args);
    }
}

Related

  1. invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)
  2. invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)
  3. invokeStatic(Class clazz, String methodName, Class returnClass, Object... args)
  4. invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)
  5. invokeStatic(final String className, final String methodName, final I argument)
  6. invokeStatic(String cname, String methodName, ClassLoader cl)
  7. invokeStaticClass(Class staticClass, String methodName, Object[] args, Class... parameterTypes)