protected T construct ( ) { if ( _targetConstructor . isPresent ( ) ) { return _targetConstructor . get ( ) . apply ( this ) ; } try { final Constructor < ? extends T > constructor = _targetClass . get ( ) . getDeclaredConstructor ( this . getClass ( ) ) ; AccessController . doPrivileged ( ( PrivilegedAction < Object > ) ( ) -> { constructor . setAccessible ( true ) ; return null ; } ) ; return constructor . newInstance ( this ) ; } catch ( final NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException e ) { throw new UnsupportedOperationException ( String . format ( UNABLE_TO_CONSTRUCT_TARGET_CLASS , _targetClass ) , e ) ; } catch ( final InvocationTargetException e ) { // If the constructor of the class threw an exception, unwrap it and // rethrow it. If the constructor throws anything other than a // RuntimeException we wrap it. final Throwable cause = e . getCause ( ) ; if ( cause instanceof RuntimeException ) { throw ( RuntimeException ) cause ; } throw new UnsupportedOperationException ( String . format ( UNABLE_TO_CONSTRUCT_TARGET_CLASS , _targetClass ) , cause ) ; } }
Protected method to construct the target class reflectively from the specified type by passing its constructor an instance of this builder .