private static Optional < Module > instantiateWithPrivateConstructor ( Class < ? > clazz , Class < ? > [ ] parameters , Object ... paramVals ) throws InvocationTargetException , IllegalAccessException , InstantiationException { final Constructor < ? > constructor ; try { constructor = clazz . getDeclaredConstructor ( parameters ) ; } catch ( NoSuchMethodException e ) { return Optional . absent ( ) ; } final boolean oldAccessible = constructor . isAccessible ( ) ; constructor . setAccessible ( true ) ; try { return Optional . of ( ( Module ) constructor . newInstance ( paramVals ) ) ; } catch ( InstantiationException | IllegalAccessException | InvocationTargetException e ) { throw e ; } finally { constructor . setAccessible ( oldAccessible ) ; } }
Instantiates a module which may have a private constructor . You can t call private constructors in Java unless you first make the constructor un - private . After doing so however we should clean up after ourselves and restore the previous state .