Java Reflection Method Static Invoke invokeStaticMethod(Class objClass, String methodName, Object args[])

Here you can find the source of invokeStaticMethod(Class objClass, String methodName, Object args[])

Description

invoke Static Method

License

Open Source License

Parameter

Parameter Description
objClass a parameter
methodName a parameter
args a parameter

Declaration

public static Object invokeStaticMethod(Class objClass, String methodName, Object args[]) 

Method Source Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    /**/* w w w  . j  a v a 2 s.  c o m*/
     * invoke Static Method
     * 
     * @param objClass
     * @param methodName
     * @param args
     * @return
     */
    public static Object invokeStaticMethod(Class objClass, String methodName, Object args[]) {
        Object result = null;
        try {
            Class argClasses[] = null;
            if (args != null && args.length > 0) {
                argClasses = new Class[args.length];
                for (int i = 0, max = args.length; i < max; i++) {
                    argClasses[i] = (args[i] == null) ? null : args[i].getClass();
                }
            }
            Method method = getMethod(objClass, methodName, argClasses);
            result = method.invoke(null, args);
        } catch (Exception e) {
            // logger.debug(e);
        }
        return result;
    }

    /**
     * get Method
     * 
     * @param objClass
     * @param methodName
     * @param argClass
     * @return Method
     * @throws Exception
     */
    public static Method getMethod(Class objClass, String methodName, Class argClass) throws Exception {
        Class argClasses[] = (argClass == null) ? null : new Class[] { argClass };
        return getMethod(objClass, methodName, argClasses);
    }

    /**
     * get Method
     * 
     * @param objClass
     * @param methodName
     * @param argClasses
     * @return
     * @throws Exception
     */
    public static Method getMethod(Class objClass, String methodName, Class argClasses[]) throws Exception {
        Method method = null;
        try {
            method = objClass.getDeclaredMethod(methodName, argClasses);
        } catch (Exception e) {
        }
        if (method == null) {
            try {
                method = objClass.getMethod(methodName, argClasses);
            } catch (Exception e) {
                // logger.debug(e);
            }
        }
        return method;
    }
}

Related

  1. invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)
  2. invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)
  3. invokeStatic(final String className, final String methodName, final I argument)
  4. invokeStatic(String cname, String methodName, ClassLoader cl)
  5. invokeStaticClass(Class staticClass, String methodName, Object[] args, Class... parameterTypes)
  6. invokeStaticMethod(Class cls, String method, Class[] params, Object... args)
  7. invokeStaticMethod(Class cls, String methodName, Object... args)
  8. invokeStaticMethod(Class cls, String name)
  9. invokeStaticMethod(Class klass, String methodName)