Here you can find the source of invokeProtectedMethod(Object o, Object[] args, String methodName, Class>[] types)
Parameter | Description |
---|---|
T | a parameter |
o | a parameter |
args | a parameter |
methodName | a parameter |
types | a parameter |
public static <T extends Object> T invokeProtectedMethod(Object o, Object[] args, String methodName, Class<?>[] types)
//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; } }