@ SuppressWarnings ( "unchecked" ) public static < T > T performMethodInvocation ( Object tested , Method methodToInvoke , Object ... arguments ) throws Exception { final boolean accessible = methodToInvoke . isAccessible ( ) ; if ( ! accessible ) { methodToInvoke . setAccessible ( true ) ; } try { if ( isPotentialVarArgsMethod ( methodToInvoke , arguments ) ) { Class < ? > [ ] parameterTypes = methodToInvoke . getParameterTypes ( ) ; final int varArgsIndex = parameterTypes . length - 1 ; Class < ? > varArgsType = parameterTypes [ varArgsIndex ] . getComponentType ( ) ; Object varArgsArrayInstance = createAndPopulateVarArgsArray ( varArgsType , varArgsIndex , arguments ) ; Object [ ] completeArgumentList = new Object [ parameterTypes . length ] ; System . arraycopy ( arguments , 0 , completeArgumentList , 0 , varArgsIndex ) ; completeArgumentList [ completeArgumentList . length - 1 ] = varArgsArrayInstance ; return ( T ) methodToInvoke . invoke ( tested , completeArgumentList ) ; } else { return ( T ) methodToInvoke . invoke ( tested , arguments == null ? new Object [ ] { arguments } : arguments ) ; } } catch ( InvocationTargetException e ) { Throwable cause = e . getCause ( ) ; if ( cause instanceof Exception ) { throw ( Exception ) cause ; } else if ( cause instanceof Error ) { throw ( Error ) cause ; } else { throw new MethodInvocationException ( cause ) ; } } finally { if ( ! accessible ) { methodToInvoke . setAccessible ( false ) ; } } }
Perform method invocation .