Search in sources :

Example 1 with ConstructorMetadataBuilder

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

the class DomainModelModuleMetadata method getConstructor.

private ConstructorMetadata getConstructor() {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Generating constructor
    ConstructorMetadataBuilder constructor = new ConstructorMetadataBuilder(getId());
    constructor.setModifier(Modifier.PUBLIC);
    bodyBuilder.appendFormalLine("// Mixin registration");
    bodyBuilder.newLine();
    // Generating body
    for (Entry<JavaType, JavaType> item : mixins.entrySet()) {
        bodyBuilder.appendFormalLine("setMixInAnnotation(%s.class, %s.class);", getNameOfJavaType(item.getKey()), getNameOfJavaType(item.getValue()));
    }
    // Adding body
    constructor.setBodyBuilder(bodyBuilder);
    return constructor.build();
}
Also used : JavaType(org.springframework.roo.model.JavaType) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 2 with ConstructorMetadataBuilder

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

the class ExceptionsMetadata method getConstructor.

private ConstructorMetadata getConstructor() {
    // Getting MessageSource field name
    final String messageSource = String.valueOf(this.messageSourceField.getFieldName());
    // Generating constructor
    ConstructorMetadataBuilder constructor = new ConstructorMetadataBuilder(getId());
    constructor.setModifier(Modifier.PUBLIC);
    constructor.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    // Adding parameters
    constructor.addParameter(messageSource, SpringJavaType.MESSAGE_SOURCE);
    // Generating body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine(String.format("this.%s = %s;", messageSource, messageSource));
    // Adding body
    constructor.setBodyBuilder(bodyBuilder);
    return constructor.build();
}
Also used : ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 3 with ConstructorMetadataBuilder

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

the class JpaDataOnDemandConfigurationMetadata method getConstructor.

/**
 * Builds and returns a class constructor which injects
 * {@link EntityManager} field.
 *
 * @return a ConstructorMetadataBuilder to add to ITD.
 */
private ConstructorMetadataBuilder getConstructor() {
    // Create instance and add parameters
    ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(this.getId());
    constructorBuilder.addParameter(ENTITY_MANAGER_FIELD_NAME, JpaJavaType.ENTITY_MANAGER);
    // Add body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("%1$s(%2$s);", getMutatorMethod(getEntityManagerField().build()).getMethodName(), ENTITY_MANAGER_FIELD_NAME);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    constructorBuilder.setModifier(Modifier.PUBLIC);
    // Add @Autowired
    constructorBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    return constructorBuilder;
}
Also used : ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 4 with ConstructorMetadataBuilder

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

the class IdentifierMetadata method getParameterizedConstructor.

/**
 * Locates the parameterised constructor consisting of the id fields for
 * this class.
 *
 * @param fields
 * @return the constructor, never null.
 */
private ConstructorMetadataBuilder getParameterizedConstructor(final List<FieldMetadataBuilder> fields) {
    // Search for an existing constructor
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    for (final FieldMetadataBuilder field : fields) {
        parameterTypes.add(field.getFieldType());
    }
    final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
    if (result != null) {
        // Found an existing parameterised constructor on this class
        publicNoArgConstructor = true;
        return null;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("super();");
    for (final FieldMetadataBuilder field : fields) {
        final String fieldName = field.getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
        parameterNames.add(field.getFieldName());
    }
    // Create the constructor
    final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
    constructorBuilder.setParameterNames(parameterNames);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    return constructorBuilder;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with ConstructorMetadataBuilder

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

the class JpaDataOnDemandMetadata method getConstructorWithEntityManagerAndSize.

/**
 * Builds constructor with EntityManager and size arguments.
 *
 * @return {@link ConstructorMetadataBuilder} for adding constructor to ITD
 */
private ConstructorMetadataBuilder getConstructorWithEntityManagerAndSize() {
    ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    constructorBuilder.addParameter(ENTITY_MANAGER_VAR, JpaJavaType.ENTITY_MANAGER);
    constructorBuilder.addParameter(SIZE_VAR, JavaType.INT_PRIMITIVE);
    bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getEntityManagerField().build()).getMethodName(), ENTITY_MANAGER_VAR));
    bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getSizeField()).getMethodName(), SIZE_VAR));
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    CommentStructure comment = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(ENTITY_MANAGER_VAR + " to persist entities");
    paramsInfo.add(SIZE_VAR + " the number of entities to create and persist initially.");
    JavadocComment javadocComment = new JavadocComment(String.format("Creates a new {@link %s}.", this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), paramsInfo, null, null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    constructorBuilder.setCommentStructure(comment);
    return constructorBuilder;
}
Also used : JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

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