Search in sources :

Example 51 with FieldMetadata

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

the class JpaEntityFactoryMetadata method getEmbeddedIdMutatorMethodName.

public JavaSymbolName getEmbeddedIdMutatorMethodName() {
    final List<JavaSymbolName> fieldNames = new ArrayList<JavaSymbolName>();
    for (final FieldMetadata field : fieldInitializers.keySet()) {
        fieldNames.add(field.getFieldName());
    }
    int index = -1;
    JavaSymbolName embeddedIdField;
    while (true) {
        // Compute the required field name
        index++;
        embeddedIdField = new JavaSymbolName("embeddedIdClass" + StringUtils.repeat("_", index));
        if (!fieldNames.contains(embeddedIdField)) {
            // Found a usable name
            break;
        }
    }
    return BeanInfoUtils.getMutatorMethodName(embeddedIdField);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList)

Example 52 with FieldMetadata

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

the class JpaEntityFactoryMetadata method getEmbeddedClassMutatorMethod.

private MethodMetadataBuilder getEmbeddedClassMutatorMethod(final EmbeddedHolder embeddedHolder) {
    final JavaSymbolName methodName = getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName());
    final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
    // Locate user-defined method
    if (governorHasMethod(methodName, parameterTypes)) {
        // Method found in governor so do not create method in ITD
        return null;
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Create constructor for embedded class
    final JavaType embeddedFieldType = embeddedHolder.getEmbeddedField().getFieldType();
    builder.getImportRegistrationResolver().addImport(embeddedFieldType);
    bodyBuilder.appendFormalLine(embeddedFieldType.getSimpleTypeName() + " embeddedClass = new " + embeddedFieldType.getSimpleTypeName() + "();");
    for (final FieldMetadata field : embeddedHolder.getFields()) {
        bodyBuilder.appendFormalLine(getEmbeddedFieldMutatorMethodName(embeddedHolder.getEmbeddedField().getFieldName(), field.getFieldName()).getSymbolName() + "(embeddedClass, " + INDEX_VAR + ");");
    }
    bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedHolder.getEmbeddedMutatorMethodName() + "(embeddedClass);");
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 53 with FieldMetadata

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

the class JpaEntityFactoryMetadata method getEmbeddedIdMutatorMethod.

private MethodMetadataBuilder getEmbeddedIdMutatorMethod(final Set<ClassOrInterfaceTypeDetails> dataOnDemandClasses) {
    if (!hasEmbeddedIdentifier()) {
        return null;
    }
    final JavaSymbolName embeddedIdMutator = embeddedIdHolder.getEmbeddedIdMutator();
    final JavaSymbolName methodName = getEmbeddedIdMutatorMethodName();
    final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
    // Locate user-defined method
    if (governorHasMethod(methodName, parameterTypes)) {
        // Method found in governor so do not create method in ITD
        return null;
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Create constructor for embedded id class
    final JavaType embeddedIdFieldType = embeddedIdHolder.getEmbeddedIdField().getFieldType();
    builder.getImportRegistrationResolver().addImport(embeddedIdFieldType);
    final StringBuilder sb = new StringBuilder();
    final List<FieldMetadata> identifierFields = embeddedIdHolder.getIdFields();
    for (int i = 0, n = identifierFields.size(); i < n; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        final FieldMetadata field = identifierFields.get(i);
        final String fieldName = field.getFieldName().getSymbolName();
        final JavaType fieldType = field.getFieldType();
        builder.getImportRegistrationResolver().addImport(fieldType);
        final String initializer = getFieldInitializer(field, null, dataOnDemandClasses);
        bodyBuilder.append(getFieldValidationBody(field, initializer, null, true));
        sb.append(fieldName);
    }
    bodyBuilder.appendFormalLine("");
    bodyBuilder.appendFormalLine(embeddedIdFieldType.getSimpleTypeName() + " embeddedIdClass = new " + embeddedIdFieldType.getSimpleTypeName() + "(" + sb.toString() + ");");
    bodyBuilder.appendFormalLine(OBJ_VAR + "." + embeddedIdMutator + "(embeddedIdClass);");
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 54 with FieldMetadata

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

the class JpaEntityFactoryMetadata method getCreateMethod.

private MethodMetadata getCreateMethod() {
    // Define methodName
    final JavaSymbolName methodName = CREATE_FACTORY_METHOD_NAME;
    List<JavaType> parameterTypes = new ArrayList<JavaType>();
    parameterTypes.add(JavaType.INT_PRIMITIVE);
    // Check if method exists
    MethodMetadata existingMethod = getGovernorMethod(methodName, parameterTypes);
    if (existingMethod != null) {
        return existingMethod;
    }
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(INDEX_SYMBOL);
    // Add body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Entity obj = new Entity();
    bodyBuilder.appendFormalLine("%1$s %2$s = new %1$s();", getNameOfJavaType(this.entity), OBJ_SYMBOL);
    // Set values for transient object
    for (final Map.Entry<FieldMetadata, JpaEntityFactoryMetadata> entry : locatedFields.entrySet()) {
        bodyBuilder.appendFormalLine("%s(%s, %s);", BeanInfoUtils.getMutatorMethodName(entry.getKey()), OBJ_SYMBOL, INDEX_SYMBOL);
    }
    // return obj;
    bodyBuilder.appendFormalLine("return %s;", OBJ_SYMBOL);
    // Create method
    MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
    CommentStructure commentStructure = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(String.format("%s position of the %s", INDEX_VAR, this.entity.getSimpleTypeName()));
    JavadocComment comment = new JavadocComment(String.format("Creates a new {@link %s} with the given %s.", this.entity.getSimpleTypeName(), INDEX_VAR), paramsInfo, String.format("a new transient %s", this.entity.getSimpleTypeName()), null);
    commentStructure.addComment(comment, CommentLocation.BEGINNING);
    method.setCommentStructure(commentStructure);
    return method.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with FieldMetadata

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

the class JpaEntityFactoryMetadata method getFieldMutatorMethods.

private List<MethodMetadataBuilder> getFieldMutatorMethods() {
    final List<MethodMetadataBuilder> fieldMutatorMethods = new ArrayList<MethodMetadataBuilder>();
    final List<JavaSymbolName> parameterNames = Arrays.asList(OBJ_SYMBOL, INDEX_SYMBOL);
    final JavaType[] parameterTypes = { entity, JavaType.INT_PRIMITIVE };
    for (final Map.Entry<FieldMetadata, String> entry : fieldInitializers.entrySet()) {
        final FieldMetadata field = entry.getKey();
        final JavaSymbolName mutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        // Locate user-defined method
        if (governorHasMethod(mutatorName, parameterTypes)) {
            // Method found in governor so do not create method in ITD
            continue;
        }
        // Method not on governor so need to create it
        final String initializer = entry.getValue();
        if (!StringUtils.isBlank(initializer)) {
            final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
            bodyBuilder.append(getFieldValidationBody(field, initializer, mutatorName, false));
            fieldMutatorMethods.add(new MethodMetadataBuilder(getId(), Modifier.PUBLIC, mutatorName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder));
        }
    }
    return fieldMutatorMethods;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)141 JavaType (org.springframework.roo.model.JavaType)76 ArrayList (java.util.ArrayList)69 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)40 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)40 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)39 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)38 RooJavaType (org.springframework.roo.model.RooJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)33 JpaJavaType (org.springframework.roo.model.JpaJavaType)32 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)30 SpringJavaType (org.springframework.roo.model.SpringJavaType)30 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)29 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)21 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)21 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)18 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)17 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15