protected < A extends AggregateRoot < ? > > A createFromFactory ( Class < A > aggregateClass , Object ... parameters ) { checkNotNull ( aggregateClass ) ; checkNotNull ( parameters ) ; Factory < A > factory = domainRegistry . getFactory ( aggregateClass ) ; // Find the method in the factory which match the signature determined with the previously // extracted parameters Method factoryMethod ; boolean useDefaultFactory = false ; try { factoryMethod = MethodMatcher . findMatchingMethod ( factory . getClass ( ) , aggregateClass , parameters ) ; if ( factoryMethod == null ) { useDefaultFactory = true ; } } catch ( Exception e ) { throw BusinessException . wrap ( e , BusinessErrorCode . UNABLE_TO_FIND_FACTORY_METHOD ) . put ( "aggregateClass" , aggregateClass . getName ( ) ) . put ( "parameters" , Arrays . toString ( parameters ) ) ; } // Invoke the factory to create the aggregate root try { if ( useDefaultFactory ) { return factory . create ( parameters ) ; } else { if ( parameters . length == 0 ) { return ReflectUtils . invoke ( factoryMethod , factory ) ; } else { return ReflectUtils . invoke ( factoryMethod , factory , parameters ) ; } } } catch ( Exception e ) { throw BusinessException . wrap ( e , BusinessErrorCode . UNABLE_TO_INVOKE_FACTORY_METHOD ) . put ( "aggregateClass" , aggregateClass . getName ( ) ) . put ( "factoryClass" , factory . getClass ( ) . getName ( ) ) . put ( "factoryMethod" , Optional . ofNullable ( factoryMethod ) . map ( Method :: getName ) . orElse ( "create" ) ) . put ( "parameters" , Arrays . toString ( parameters ) ) ; } }
Implements the logic to create an aggregate .