Java Reflection Method Static Invoke invokeStaticMethod(String className, String methodName)

Here you can find the source of invokeStaticMethod(String className, String methodName)

Description

Invokes static method methodName in class by given className without parameters.

License

Apache License

Parameter

Parameter Description
className Class name, e.g. java.lang.Object
methodName Name of static method to invoke

Exception

Parameter Description
Throwable If there is some unexpected problem during execution

Return

Result of invoked method

Declaration

public static Object invokeStaticMethod(String className, String methodName) throws Throwable 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;

public class Main {
    /**//from  ww  w  .jav  a  2  s  .  c om
     * Invokes static method {@code methodName} in class by given {@code className} without parameters.
     * @param className Class name, e.g. {@code java.lang.Object}
     * @param methodName Name of static method to invoke
     * @return Result of invoked method 
     * @throws Throwable If there is some unexpected problem during execution
     */
    public static Object invokeStaticMethod(String className, String methodName) throws Throwable {
        return invokeStaticMethod(className, methodName, new Class<?>[] {}, new Object[] {});
    }

    /**
     * Invokes static method {@code methodName} in class by given {@code className}, {@code paramTypes} and {@code paramValues}.
     * @param className Class name, e.g. {@code java.lang.Object}
     * @param methodName Name of static method to invoke
     * @param paramTypes Array containing parameter types
     * @param paramValues Array containing parameter values
     * @return Result of invoked method 
     * @throws Throwable If there is some unexpected problem during execution
     */
    public static Object invokeStaticMethod(String className, String methodName, Class<?>[] paramTypes,
            Object[] paramValues) throws Throwable {
        Class<?> clazz = Class.forName(className);
        Method method = clazz.getMethod(methodName, paramTypes);
        return method.invoke(null, paramValues);
    }
}

Related

  1. invokeStaticMethod(final Class cls, final String methodName)
  2. invokeStaticMethod(final Class cls, final String methodName, final boolean throwException)
  3. invokeStaticMethod(final Class clazz, final String methodName, final Class[] parameterTypes, final Object[] arglist)
  4. invokeStaticMethod(final Method method, final Object... args)
  5. invokeStaticMethod(String className, String method, Class[] paramTypes, Object[] paramValues)
  6. invokeStaticMethod(String className, String methodName, Object[] args)
  7. invokeStaticMethodNoArgs(Class clazz, String methodName)