public static < T > T invokeConstructor ( Class < T > classThatContainsTheConstructorToTest , Object ... arguments ) throws Exception { if ( classThatContainsTheConstructorToTest == null ) { throw new IllegalArgumentException ( "The class should contain the constructor cannot be null." ) ; } Class < ? > [ ] argumentTypes = null ; if ( arguments == null ) { argumentTypes = new Class < ? > [ 0 ] ; } else { argumentTypes = new Class < ? > [ arguments . length ] ; for ( int i = 0 ; i < arguments . length ; i ++ ) { argumentTypes [ i ] = getType ( arguments [ i ] ) ; } } Constructor < T > constructor = null ; constructor = getBestCandidateConstructor ( classThatContainsTheConstructorToTest , argumentTypes , arguments ) ; return createInstance ( constructor , arguments ) ; }
Invoke a constructor . Useful for testing classes with a private constructor .