private static < T > T createInstance ( Constructor < T > constructor , Object ... arguments ) throws Exception { if ( constructor == null ) { throw new IllegalArgumentException ( "Constructor cannot be null" ) ; } constructor . setAccessible ( true ) ; T createdObject = null ; try { if ( constructor . isVarArgs ( ) ) { Class < ? > [ ] parameterTypes = constructor . 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 ; createdObject = constructor . newInstance ( completeArgumentList ) ; } else { createdObject = constructor . newInstance ( arguments ) ; } } catch ( InvocationTargetException e ) { Throwable cause = e . getCause ( ) ; if ( cause instanceof Exception ) { throw ( Exception ) cause ; } else if ( cause instanceof Error ) { throw ( Error ) cause ; } } return createdObject ; }
Creates the instance .