Search in sources :

Example 16 with ConstructorMetadataBuilder

use of org.springframework.roo.classpath.details.ConstructorMetadataBuilder in project spring-roo by spring-projects.

the class ThymeleafMetadata method getConstructor.

private ConstructorMetadata getConstructor(String itemLinkBuilderLine, String collectionLinkBuilderLine, boolean autowired) {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Generating constructor
    ConstructorMetadataBuilder newConstructor = new ConstructorMetadataBuilder(getId());
    newConstructor.setModifier(Modifier.PUBLIC);
    if (autowired) {
        newConstructor.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    }
    // Getting serviceFieldName
    String serviceFieldName = controllerMetadata.getServiceField().getFieldName().getSymbolName();
    newConstructor.addParameter(serviceFieldName, controllerMetadata.getService());
    bodyBuilder.appendFormalLine(String.format("%s(%s);", getMutatorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceFieldName));
    if (this.type == ControllerType.DETAIL || this.type == ControllerType.DETAIL_ITEM) {
        for (FieldMetadata serviceField : controllerMetadata.getDetailsServiceFields().values()) {
            // Getting parentServiceFieldName
            String childServiceFieldName = serviceField.getFieldName().getSymbolName();
            // Adding parameters
            newConstructor.addParameter(childServiceFieldName, serviceField.getFieldType());
            // Generating body
            bodyBuilder.appendFormalLine(String.format("%s(%s);", getMutatorMethod(serviceField).getMethodName(), childServiceFieldName));
        }
    }
    // field
    if (this.type == ControllerType.COLLECTION || this.type == ControllerType.DETAIL || this.type == ControllerType.SEARCH) {
        String conversionServiceFieldName = this.conversionServiceField.getFieldName().getSymbolName();
        newConstructor.addParameter(conversionServiceFieldName, this.conversionServiceField.getFieldType());
        bodyBuilder.appendFormalLine("%s(%s);", getMutatorMethod(this.getConversionServiceField()).getMethodName(), conversionServiceFieldName);
    }
    String messageSourceName = this.messageSourceField.getFieldName().getSymbolName();
    newConstructor.addParameter(messageSourceName, messageSourceField.getFieldType());
    bodyBuilder.appendFormalLine("%s(%s);", getMutatorMethod(this.messageSourceField).getMethodName(), messageSourceName);
    // Add ControllerMethodLinkBuilderFactory argument
    if (StringUtils.isNotBlank(itemLinkBuilderLine) || StringUtils.isNotBlank(collectionLinkBuilderLine)) {
        newConstructor.addParameter(LINK_BUILDER_ARGUMENT_NAME, SpringletsJavaType.SPRINGLETS_CONTROLLER_METHOD_LINK_BUILDER_FACTORY);
        this.builder.getImportRegistrationResolver().addImport(SpringletsJavaType.SPRINGLETS_CONTROLLER_METHOD_LINK_BUILDER_FACTORY);
        if (StringUtils.isNotBlank(itemLinkBuilderLine)) {
            bodyBuilder.appendFormalLine(itemLinkBuilderLine);
        }
        if (StringUtils.isNotBlank(collectionLinkBuilderLine)) {
            bodyBuilder.appendFormalLine(collectionLinkBuilderLine);
        }
    }
    // Adding body
    newConstructor.setBodyBuilder(bodyBuilder);
    return newConstructor.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)16 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)15 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)6 ArrayList (java.util.ArrayList)5 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 JavaType (org.springframework.roo.model.JavaType)4 ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)3 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)2 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)1 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)1 JdkJavaType (org.springframework.roo.model.JdkJavaType)1