public static ConstructorNode addDelegateConstructor ( ClassNode classNode , MethodNode constructorMethod , Map < String , ClassNode > genericsPlaceholders ) { BlockStatement constructorBody = new BlockStatement ( ) ; Parameter [ ] constructorParams = getRemainingParameterTypes ( constructorMethod . getParameters ( ) ) ; ArgumentListExpression arguments = createArgumentListFromParameters ( constructorParams , true , genericsPlaceholders ) ; MethodCallExpression constructCallExpression = new MethodCallExpression ( new ClassExpression ( constructorMethod . getDeclaringClass ( ) ) , "initialize" , arguments ) ; constructCallExpression . setMethodTarget ( constructorMethod ) ; ExpressionStatement constructorInitExpression = new ExpressionStatement ( constructCallExpression ) ; if ( constructorParams . length > 0 ) { constructorBody . addStatement ( new ExpressionStatement ( new ConstructorCallExpression ( ClassNode . THIS , GrailsArtefactClassInjector . ZERO_ARGS ) ) ) ; } constructorBody . addStatement ( constructorInitExpression ) ; if ( constructorParams . length == 0 ) { // handle default constructor ConstructorNode constructorNode = getDefaultConstructor ( classNode ) ; if ( constructorNode != null ) { List < AnnotationNode > annotations = constructorNode . getAnnotations ( new ClassNode ( GrailsDelegatingConstructor . class ) ) ; if ( annotations . size ( ) == 0 ) { Statement existingBodyCode = constructorNode . getCode ( ) ; if ( existingBodyCode instanceof BlockStatement ) { ( ( BlockStatement ) existingBodyCode ) . addStatement ( constructorInitExpression ) ; } else { constructorNode . setCode ( constructorBody ) ; } } } else { constructorNode = new ConstructorNode ( Modifier . PUBLIC , constructorBody ) ; classNode . addConstructor ( constructorNode ) ; } constructorNode . addAnnotation ( new AnnotationNode ( new ClassNode ( GrailsDelegatingConstructor . class ) ) ) ; return constructorNode ; } else { // create new constructor, restoring default constructor if there is none ConstructorNode cn = findConstructor ( classNode , constructorParams ) ; if ( cn == null ) { cn = new ConstructorNode ( Modifier . PUBLIC , copyParameters ( constructorParams , genericsPlaceholders ) , null , constructorBody ) ; classNode . addConstructor ( cn ) ; } else { List < AnnotationNode > annotations = cn . getAnnotations ( new ClassNode ( GrailsDelegatingConstructor . class ) ) ; if ( annotations . size ( ) == 0 ) { Statement code = cn . getCode ( ) ; constructorBody . addStatement ( code ) ; cn . setCode ( constructorBody ) ; } } ConstructorNode defaultConstructor = getDefaultConstructor ( classNode ) ; if ( defaultConstructor == null ) { // add empty classNode . addConstructor ( new ConstructorNode ( Modifier . PUBLIC , new BlockStatement ( ) ) ) ; } cn . addAnnotation ( new AnnotationNode ( new ClassNode ( GrailsDelegatingConstructor . class ) ) ) ; return cn ; } }
Adds or modifies an existing constructor to delegate to the given static constructor method for initialization logic .