Search in sources :

Example 11 with MethodMetadataBuilder

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

the class JavaBeanMetadata method generateGettersAndSetters.

protected void generateGettersAndSetters(final Map<FieldMetadata, JavaSymbolName> declaredFields, List<? extends MethodMetadata> interfaceMethods) {
    // Add getters and setters
    for (final Entry<FieldMetadata, JavaSymbolName> entry : declaredFields.entrySet()) {
        final FieldMetadata field = entry.getKey();
        final MethodMetadataBuilder accessorMethod = getDeclaredGetter(field);
        final MethodMetadataBuilder mutatorMethod = getDeclaredSetter(field);
        // Check to see if GAE is interested
        if (entry.getValue() != null) {
            JavaSymbolName hiddenIdFieldName;
            if (field.getFieldType().isCommonCollectionType()) {
                hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Keys");
                builder.getImportRegistrationResolver().addImport(GAE_DATASTORE_KEY_FACTORY);
                builder.addField(getMultipleEntityIdField(hiddenIdFieldName));
            } else {
                hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Id");
                builder.addField(getSingularEntityIdField(hiddenIdFieldName));
            }
            processGaeAnnotations(field);
            InvocableMemberBodyBuilder gaeAccessorBody = getGaeAccessorBody(field, hiddenIdFieldName);
            accessorMethod.setBodyBuilder(gaeAccessorBody);
            InvocableMemberBodyBuilder gaeMutatorBody = getGaeMutatorBody(field, hiddenIdFieldName);
            mutatorMethod.setBodyBuilder(gaeMutatorBody);
        }
        // Add to mutators and accesors list and build
        if (accessorMethod != null) {
            this.accesorMethods.put(field.getFieldName(), accessorMethod.build());
            builder.addMethod(accessorMethod);
        }
        if (mutatorMethod != null) {
            this.mutatorMethods.put(field.getFieldName(), mutatorMethod.build());
            builder.addMethod(mutatorMethod);
        }
    }
    // Implements interface methods if exists
    implementsInterfaceMethods(interfaceMethods);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 12 with MethodMetadataBuilder

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

the class JavaBeanMetadata method getDeclaredSetter.

/**
 * Obtains the specific mutator method that is either contained within the
 * normal Java compilation unit or will be introduced by this add-on via an
 * ITD.
 *
 * @param field
 *            that already exists on the type either directly or via
 *            introduction (required; must be declared by this type to be
 *            located)
 * @return the method corresponding to a mutator, or null if not found
 */
private MethodMetadataBuilder getDeclaredSetter(final FieldMetadata field) {
    Validate.notNull(field, "Field required");
    // Compute the mutator method name
    final JavaSymbolName methodName = BeanInfoUtils.getMutatorMethodName(field);
    // Compute the mutator method parameters
    final JavaType parameterType = field.getFieldType();
    // See if the type itself declared the mutator
    if (governorHasMethod(methodName, parameterType)) {
        return null;
    }
    // Compute the mutator method parameter names
    final List<JavaSymbolName> parameterNames = Arrays.asList(field.getFieldName());
    // final fields as per ROO-36)
    if (annotationValues.isSettersByDefault() && !Modifier.isTransient(field.getModifier()) && !Modifier.isStatic(field.getModifier()) && !Modifier.isFinal(field.getModifier())) {
        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.appendFormalLine("this." + field.getFieldName().getSymbolName() + " = " + field.getFieldName().getSymbolName() + ";");
        return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
    }
    return null;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 13 with MethodMetadataBuilder

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

the class JavaBeanMetadata method getInterfaceMethod.

/**
 * Obtains a valid MethodMetadataBuilder with necessary configuration
 *
 * @param method
 * @return MethodMetadataBuilder
 */
private MethodMetadataBuilder getInterfaceMethod(final MethodMetadata method) {
    // Compute the method name
    final JavaSymbolName methodName = method.getMethodName();
    // See if the type itself declared the accessor
    if (governorHasMethod(methodName)) {
        return null;
    }
    // Getting return type
    JavaType returnType = method.getReturnType();
    // Generating body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // If return type is not primitive, return null
    if (returnType.isPrimitive()) {
        JavaType baseType = returnType.getBaseType();
        if (baseType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return false;");
        } else if (baseType.equals(JavaType.BYTE_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.SHORT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.INT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.LONG_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.FLOAT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.DOUBLE_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0.00;");
        } else if (baseType.equals(JavaType.CHAR_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return '\0';");
        }
    } else {
        bodyBuilder.appendFormalLine("return null;");
    }
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 14 with MethodMetadataBuilder

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

the class ToStringMetadata method generateToStringMethod.

/**
 * Generates toString method
 *
 * @param metadataId
 * @param target
 * @param annotationValues
 * @param fields
 * @return
 */
protected static MethodMetadataBuilder generateToStringMethod(String metadataId, JavaType target, ToStringAnnotationValues annotationValues, List<FieldMetadata> fields) {
    final JavaSymbolName methodName = new JavaSymbolName(annotationValues.getToStringMethod());
    // Get excludeFields attribute value
    final String[] excludeFields = annotationValues.getExcludeFields();
    // Get all fields from class
    List<FieldMetadata> affectedFields = new ArrayList<FieldMetadata>();
    for (FieldMetadata field : fields) {
        // Check if field must be excluded manually by "excludeFields" attribute
        if (Modifier.isStatic(field.getModifier()) || !isCommonJavaField(field) || isCollectionField(field) || isRelationField(field) || isExcluded(excludeFields, field)) {
            continue;
        }
        affectedFields.add(field);
    }
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Build toString method body
    // return "Entity {" + "fieldName1='" + fieldName1 + '\'' + ", fieldName2='" + fieldName2 + '\''+ "}" + super.toString();
    bodyBuilder.appendFormalLine(String.format("return \"%s {\" + ", target.getSimpleTypeName()));
    for (int i = 0; i < affectedFields.size(); i++) {
        bodyBuilder.appendIndent();
        StringBuilder fieldString = new StringBuilder();
        fieldString.append("\"");
        if (i != 0) {
            fieldString.append(", ");
        }
        FieldMetadata fieldMetadata = affectedFields.get(i);
        String fieldName = fieldMetadata.getFieldName().getSymbolName();
        String fieldValue = fieldName;
        if (isDateField(fieldMetadata)) {
            fieldValue = fieldName + " == null ? null : java.text.DateFormat.getDateTimeInstance().format(" + fieldName + ")";
        } else if (isCalendarField(fieldMetadata)) {
            fieldValue = fieldName + " == null ? null : java.text.DateFormat.getDateTimeInstance().format(" + fieldName + ".getTime())";
        }
        fieldString.append(fieldName).append("='\"").append(" + ").append(fieldValue).append(" + '\\''").append(" + ");
        if (i == affectedFields.size() - 1) {
            fieldString.append("\"}\" + ").append("super.toString();");
        }
        // Append next field line
        bodyBuilder.appendFormalLine(fieldString.toString());
    }
    if (affectedFields.isEmpty()) {
        bodyBuilder.appendFormalLine("\"}\" + super.toString();");
    }
    return new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, methodName, STRING, bodyBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) RooToString(org.springframework.roo.addon.javabean.annotations.RooToString) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 15 with MethodMetadataBuilder

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

the class EqualsMetadata method generateEqualsMethod.

/**
 * Generate equals method
 *
 * @param metadataId
 * @param target
 * @param annotationValues
 * @param identifierAccessor
 * @param locatedFields
 * @param builder
 * @return
 */
protected static MethodMetadataBuilder generateEqualsMethod(String metadataId, JavaType target, EqualsAnnotationValues annotationValues, JavaSymbolName identifierAccessor, List<FieldMetadata> locatedFields, ItdTypeDetailsBuilder builder) {
    final List<JavaSymbolName> parameterNames = Arrays.asList(new JavaSymbolName(OBJECT_NAME));
    // Create the method body depending on destination class properties
    InvocableMemberBodyBuilder bodyBuilder = null;
    if (annotationValues.isJpaEntity()) {
        bodyBuilder = getJpaEntityEqualsMethodBody(target, identifierAccessor, builder.getImportRegistrationResolver());
    } else {
        bodyBuilder = generateDefaultEqualsMethodBody(target, annotationValues.isAppendSuper(), locatedFields, builder.getImportRegistrationResolver());
    }
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, EQUALS_METHOD_NAME, BOOLEAN_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(OBJECT), parameterNames, bodyBuilder);
    if (annotationValues.isJpaEntity()) {
        CommentStructure commentStructure = new CommentStructure();
        commentStructure.addComment(new JavadocComment("This `equals` implementation is specific for JPA entities and uses ".concat(IOUtils.LINE_SEPARATOR).concat("the entity identifier for it, 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 : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

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