public static Object callMethod ( final Method method , final Object instance , final Object ... parameters ) throws CoreException { Object res = null ; try { // store current visibility final boolean accessible = method . isAccessible ( ) ; // let it accessible anyway method . setAccessible ( true ) ; // Call this method with right parameters res = method . invoke ( instance , parameters ) ; // Reset default visibility method . setAccessible ( accessible ) ; } catch ( IllegalAccessException | InvocationTargetException | IllegalArgumentException e ) { final String methodName = instance . getClass ( ) . getSimpleName ( ) + "." + method . getName ( ) ; throw new CoreException ( "Impossible to call method " + methodName , e ) ; } return res ; }
Call the given method for the instance object even if its visibility is private or protected .