@ SuppressWarnings ( "unchecked" ) @ Override public < T > T newInstance ( ManagedClassSPI managedClass , Object ... args ) { Constructor < ? > constructor = managedClass . getConstructor ( ) ; if ( constructor == null ) { throw new BugError ( "Local instance factory cannot instantiate |%s|. Missing constructor." , managedClass ) ; } Object instance = null ; try { instance = constructor . newInstance ( args ) ; } catch ( IllegalArgumentException e ) { log . error ( "Wrong number of arguments or bad types for |%s|: [%s]." , constructor , Strings . join ( Classes . getParameterTypes ( args ) ) ) ; throw e ; } catch ( InstantiationException e ) { // managed class implementation is already validated, i.e. is not abstract and // test for existing constructor is performed... so no obvious reasons for instantiation exception throw new BugError ( e ) ; } catch ( IllegalAccessException e ) { // constructor has accessibility true and class is tested for public access modifier // so there is no reason for illegal access exception throw new BugError ( e ) ; } catch ( InvocationTargetException e ) { log . error ( "Managed instance constructor |%s| fail due to: %s." , constructor , e . getCause ( ) ) ; throw new InvocationException ( e ) ; } if ( managedClass . getInstanceType ( ) . equals ( InstanceType . PROXY ) ) { // there are two proxy handlers: one transactional and one not // the difference is that transactional proxy handler gets a reference to an external transactional resource ManagedProxyHandler handler = null ; if ( managedClass . isTransactional ( ) ) { TransactionalResource transactionalResource = managedClass . getContainer ( ) . getInstance ( TransactionalResource . class ) ; handler = new ManagedProxyHandler ( transactionalResource , managedClass , instance ) ; } else { handler = new ManagedProxyHandler ( managedClass , instance ) ; } final ClassLoader classLoader = managedClass . getImplementationClass ( ) . getClassLoader ( ) ; final Class < ? > [ ] interfaceClasses = managedClass . getInterfaceClasses ( ) ; return ( T ) Proxy . newProxyInstance ( classLoader , interfaceClasses , handler ) ; } return ( T ) instance ; }
Uses managed class constructor to create new instance with provided arguments . Arguments should be in order types and number required by constructor signature .