public static < R > R callConstructor ( Class < ? extends R > clazz , ClassParameter < ? > ... classParameters ) { try { final Class < ? > [ ] classes = ClassParameter . getClasses ( classParameters ) ; final Object [ ] values = ClassParameter . getValues ( classParameters ) ; Constructor < ? extends R > constructor = clazz . getDeclaredConstructor ( classes ) ; constructor . setAccessible ( true ) ; return constructor . newInstance ( values ) ; } catch ( InstantiationException e ) { throw new RuntimeException ( "error instantiating " + clazz . getName ( ) , e ) ; } catch ( InvocationTargetException e ) { if ( e . getTargetException ( ) instanceof RuntimeException ) { throw ( RuntimeException ) e . getTargetException ( ) ; } if ( e . getTargetException ( ) instanceof Error ) { throw ( Error ) e . getTargetException ( ) ; } throw new RuntimeException ( e . getTargetException ( ) ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Reflectively call the constructor of an object .