Java Reflection Method Static Invoke invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)

Here you can find the source of invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)

Description

invoke Reflect Static Method

License

Apache License

Declaration

public static Object invokeReflectStaticMethod(String methodName,
            Class cl, Object[] parameter, Class[] args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static boolean isThrowable = true;

    public static Object invokeReflectStaticMethod(String methodName,
            Class cl, Object[] parameter, Class[] args) {
        try {//from   w  ww . j  ava  2 s. c  o m
            Method method = cl.getDeclaredMethod(methodName, args);
            method.setAccessible(true);
            return method.invoke(null, parameter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isThrowable) {
            throw new RuntimeException("invokeReflectStaticMethod error "
                    + methodName + "   class   " + cl + "  args  " + args);
        }
        return null;
    }

    public static Method getDeclaredMethod(Object object,
            String methodName, Class[] parameterTypes, Class declaringClass) {
        Method method = null;
        if (null == declaringClass || !declaringClass.isInterface()) {

            for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz
                    .getSuperclass()) {
                try {
                    method = clazz.getDeclaredMethod(methodName,
                            parameterTypes);
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    if (null == declaringClass
                            || clazz.equals(declaringClass)) {
                        return method;
                    }
                } catch (Exception e) {
                }
            }
        } else {
            try {
                method = declaringClass.getDeclaredMethod(methodName,
                        parameterTypes);
                return method;
            } catch (Exception e) {

            }
        }
        if (isThrowable) {
            throw new RuntimeException("getDeclaredMethod error "
                    + methodName + "   parameterTypes   " + parameterTypes
                    + " targetObject " + object.toString());
        }
        return null;
    }
}

Related

  1. invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)
  2. invokeStatic(Class clazz, String methodName, Class returnClass, Object... args)
  3. invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final 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)