Java Method Call invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)

Here you can find the source of invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)

Description

Invokes a protected method on the specified object.

License

Apache License

Parameter

Parameter Description
T a parameter
o a parameter
args a parameter
methodName a parameter
types a parameter

Declaration

public static <T extends Object> T invokeProtectedMethod(Object o, Object[] args, String methodName,
        Class<?>[] types) 

Method Source Code

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

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**//w ww.  j  ava2  s.  co  m
     * Invokes a protected method on the specified object.
     * 
     * @param <T>
     * @param o
     * @param methodName
     * @return
     */
    public static <T extends Object> T invokeProtectedMethod(Object o, String methodName) {
        return invokeProtectedMethod(o, null, methodName, null);
    }

    /**
     * Invokes a protected method on the specified object.
     * 
     * @param <T>
     * @param o
     * @param args
     * @param methodName
     * @param types
     * @return
     */
    public static <T extends Object> T invokeProtectedMethod(Object o, Object[] args, String methodName,
            Class<?>[] types) {
        try {
            Method m = getDeclaredMethod(o.getClass(), methodName, types);
            m.setAccessible(true);
            return (T) m.invoke(o, args);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the declared method in the specified class or any of its super
     * classes.
     * 
     * @param c
     * @param methodName
     * @param types
     * @return
     * @throws NoSuchMethodException
     */
    public static Method getDeclaredMethod(Class<?> c, String methodName, Class<?>[] types)
            throws NoSuchMethodException {
        Method m = null;
        while (m == null && c != null) {
            try {
                m = c.getDeclaredMethod(methodName, types);
            } catch (SecurityException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                // nop
            }
            c = c.getSuperclass();
        }
        if (m == null) {
            throw new NoSuchMethodException();
        }
        return m;
    }
}

Related

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