public static < T > T newInstance ( Class < T > theClass , Class < ? > [ ] parameterTypes , Object [ ] initargs ) { // Perform some sanity checks on the arguments. if ( parameterTypes . length != initargs . length ) { throw new IllegalArgumentException ( "Constructor parameter types don't match constructor arguments" ) ; } for ( int i = 0 ; i < parameterTypes . length ; i ++ ) { Class < ? > clazz = parameterTypes [ i ] ; if ( ! ( clazz . isInstance ( initargs [ i ] ) ) ) { throw new IllegalArgumentException ( "Object : " + initargs [ i ] + " is not an instance of " + clazz ) ; } } try { Constructor < T > meth = theClass . getDeclaredConstructor ( parameterTypes ) ; meth . setAccessible ( true ) ; return meth . newInstance ( initargs ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Create an object for the given class .