Java Method Call invokeProtectedMethod(Class c, String method, Object... args)

Here you can find the source of invokeProtectedMethod(Class c, String method, Object... args)

Description

invoke Protected Method

License

Apache License

Declaration

public static Object invokeProtectedMethod(Class c, String method, Object... args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Object invokeProtectedMethod(Class c, String method, Object... args) {
        return invokeProtectedMethod(c, null, method, args);
    }/*from   w w  w  . j  a v  a2s  .com*/

    public static Object invokeProtectedMethod(Object o, String method, Object... args) {
        return invokeProtectedMethod(o.getClass(), o, method, args);
    }

    public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args) {
        try {
            Class[] pTypes = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Integer) {
                    pTypes[i] = int.class;
                } else {
                    pTypes[i] = args[i].getClass();
                }
            }

            Method m = c.getDeclaredMethod(method, pTypes);
            m.setAccessible(true);
            return m.invoke(o, args);
        } catch (Exception ex) {
            System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
            return null;
        }
    }
}

Related

  1. invokePrivateMethod(Method method, Object object, Object... args)
  2. invokePrivateMethod(Object instance, String name, Object... args)
  3. invokePrivateMethod(Object obj, String methodName, Class[] parameterTypes, Object[] args)
  4. invokePrivateMethod(String methodName, Class clazz, Object object)
  5. invokeProperty(Object obj, String property)
  6. invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)
  7. invokeProxied(final Callable callable, final ClassLoader classLoader)
  8. invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)
  9. invokeQuietly(MethodHandle handle, Object instance)