Search in sources :

Example 91 with MethodMetadataBuilder

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

the class AbstractItdTypeDetailsProvidingMetadataItem method getMethod.

/**
 * Returns a public method given the method name, return type, parameter
 * types, parameter names, and method body.
 *
 * @param modifier the method modifier
 * @param methodName the method name
 * @param returnType the return type
 * @param parameterTypes a list of parameter types
 * @param parameterNames a list of parameter names
 * @param bodyBuilder the method body
 * @return null if the method exists on the governor, otherwise a new method
 *         is returned
 */
protected MethodMetadataBuilder getMethod(final int modifier, final JavaSymbolName methodName, final JavaType returnType, final List<JavaType> parameterTypes, final List<JavaSymbolName> parameterNames, final InvocableMemberBodyBuilder bodyBuilder) {
    final MethodMetadata method = getGovernorMethod(methodName, parameterTypes);
    if (method != null) {
        return null;
    }
    addToImports(parameterTypes);
    return new MethodMetadataBuilder(getId(), modifier, methodName, returnType, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, bodyBuilder);
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata)

Example 92 with MethodMetadataBuilder

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

the class AbstractItdTypeDetailsProvidingMetadataItem method getMutatorMethod.

protected MethodMetadata getMutatorMethod(final FieldMetadata field) {
    // Check if the mutator has been cached
    MethodMetadataBuilder mutator = mutatorMethods.get(field);
    if (mutator == null) {
        mutator = getMutatorMethod(field.getFieldName(), field.getFieldType());
        mutatorMethods.put(field, mutator);
    }
    // Return governor method
    if (mutator == null) {
        return getGovernorMethod(BeanInfoUtils.getMutatorMethodName(field.getFieldName()), field.getFieldType());
    }
    return mutator.build();
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder)

Example 93 with MethodMetadataBuilder

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

the class AbstractItdTypeDetailsProvidingMetadataItem method getAccessorMethod.

protected MethodMetadata getAccessorMethod(final FieldMetadata field) {
    // Check if this method has been cached
    MethodMetadataBuilder accessor = accessorMethods.get(field);
    if (accessor == null) {
        accessor = getAccessorMethod(field, InvocableMemberBodyBuilder.getInstance().appendFormalLine("return " + field.getFieldName().getSymbolName() + ";"));
        accessorMethods.put(field, accessor);
    }
    // Return governor method if exists
    if (accessor == null) {
        return getGovernorMethod(BeanInfoUtils.getAccessorMethodName(field));
    }
    return accessor.build();
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder)

Example 94 with MethodMetadataBuilder

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

the class EqualsMetadata method generateHashCodeMethod.

/**
 * Generate hashCode method
 *
 * @param metadataId
 * @param annotationValues
 * @param locatedFields
 * @param importRegistrationResolver
 * @return
 */
protected static MethodMetadataBuilder generateHashCodeMethod(String metadataId, EqualsAnnotationValues annotationValues, List<FieldMetadata> locatedFields, ImportRegistrationResolver importRegistrationResolver) {
    // Create the method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    StringBuilder builder = null;
    if (annotationValues.isJpaEntity()) {
        builder = getJpaEntityHashCodeMethodReturnStatment();
    } else {
        builder = getDefaultHashCodeMethodReturnStatment(annotationValues, locatedFields, importRegistrationResolver);
    }
    bodyBuilder.appendFormalLine(builder.toString());
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, HASH_CODE_METHOD_NAME, INT_PRIMITIVE, bodyBuilder);
    if (annotationValues.isJpaEntity()) {
        CommentStructure commentStructure = new CommentStructure();
        commentStructure.addComment(new JavadocComment("This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able ".concat(IOUtils.LINE_SEPARATOR).concat("to identify the entity in collections after a new id is assigned to the entity, following the article in ").concat(IOUtils.LINE_SEPARATOR).concat("https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/")), CommentLocation.BEGINNING);
        methodBuilder.setCommentStructure(commentStructure);
    }
    return methodBuilder;
}
Also used : JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 95 with MethodMetadataBuilder

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

the class JavaBeanMetadata method checkIfInterfaceMethodWasImplemented.

/**
 * To check if current method was implemented on all Java classes or ITds
 * associated to this entity class.
 * If method was implemented, is not necessary to add again.
 *
 * @param methodBuilder
 * @return
 */
private boolean checkIfInterfaceMethodWasImplemented(MethodMetadataBuilder methodBuilder) {
    // ROO-3584: Obtain current declared methods
    List<MethodMetadataBuilder> declaredMethods = builder.getDeclaredMethods();
    for (MethodMetadataBuilder method : declaredMethods) {
        // If current method equals to interface method, return false
        if (method.getMethodName().equals(methodBuilder.getMethodName())) {
            return true;
        }
    }
    // ROO-3587: Obtain ALL declared methods from Java classes and ITDs.
    MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), governorTypeDetails);
    List<MethodMetadata> allMethods = memberDetails.getMethods();
    for (MethodMetadata method : allMethods) {
        // If current method equals to interface method, return false
        if (method.getMethodName().equals(methodBuilder.getMethodName())) {
            return true;
        }
    }
    return false;
}
Also used : MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

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