Search in sources :

Example 76 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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 77 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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 78 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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 79 with MethodMetadataBuilder

use of org.springframework.roo.classpath.details.MethodMetadataBuilder 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)

Example 80 with MethodMetadataBuilder

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

the class RepositoryJpaCustomMetadata method getFindAllMethodByReferencedField.

/**
 * Method that generates the findAll method for provided referenced field on current interface.
 *
 * @param referencedField
 * @param identifierType
 *
 * @return
 */
private MethodMetadata getFindAllMethodByReferencedField(FieldMetadata referencedField, RelationInfo relationInfo) {
    // Define method name
    String findPattern = "findBy%s";
    if (relationInfo.cardinality == Cardinality.MANY_TO_MANY) {
        findPattern = "findBy%sContains";
    }
    // Method name
    JavaSymbolName methodName = new JavaSymbolName(String.format(findPattern, referencedField.getFieldName().getSymbolNameCapitalisedFirstLetter()));
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    final JavaType paramType = referencedField.getFieldType().getBaseType();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(paramType));
    parameterTypes.add(GLOBAL_SEARCH_PARAMETER);
    parameterTypes.add(PAGEABLE_PARAMETER);
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(referencedField.getFieldName().getSymbolName())));
    parameterNames.add(GOBAL_SEARCH_PARAMETER_NAME);
    parameterNames.add(PAGEABLE_PARAMETER_NAME);
    // Return type
    JavaType returnType = new JavaType(SpringJavaType.PAGE.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(defaultReturnType));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, methodName, returnType, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Aggregations

MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)155 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)120 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)117 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)115 ArrayList (java.util.ArrayList)110 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)87 JavaType (org.springframework.roo.model.JavaType)65 SpringJavaType (org.springframework.roo.model.SpringJavaType)48 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)43 JdkJavaType (org.springframework.roo.model.JdkJavaType)34 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)30 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)14 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)14 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)12 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)7