public static Object callMethodOverridingIllegalAccess ( Object o , String method , Object [ ] params , Class [ ] paramClasses ) { // Get the objects class. Class cls = o . getClass ( ) ; // Get the classes of the parameters. /*Class[] paramClasses = new Class[params.length]; for (int i = 0; i < params.length; i++) { paramClasses[i] = params[i].getClass(); }*/ try { // Try to find the matching method on the class. Method m = cls . getDeclaredMethod ( method , paramClasses ) ; // Make it accessible. m . setAccessible ( true ) ; // Invoke it with the parameters. return m . invoke ( o , params ) ; } catch ( NoSuchMethodException e ) { throw new IllegalStateException ( e ) ; } catch ( IllegalAccessException e ) { throw new IllegalStateException ( e ) ; } catch ( InvocationTargetException e ) { throw new IllegalStateException ( e ) ; } }
Calls a named method on an object with a specified set of parameters any Java access modifier are overridden .