Search in sources :

Example 26 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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 27 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JavaBeanMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JavaBeanAnnotationValues annotationValues = new JavaBeanAnnotationValues(governorPhysicalTypeMetadata);
    if (!annotationValues.isAnnotationFound()) {
        return null;
    }
    ClassOrInterfaceTypeDetails currentClassDetails = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    final Map<FieldMetadata, JavaSymbolName> declaredFields = new LinkedHashMap<FieldMetadata, JavaSymbolName>();
    final List<FieldMetadata> declaredFieldsList = new ArrayList<FieldMetadata>();
    for (final FieldMetadata field : currentClassDetails.getDeclaredFields()) {
        declaredFields.put(field, getIdentifierAccessorMethodName(field, metadataIdentificationString));
        declaredFieldsList.add(field);
    }
    // In order to handle switching between GAE and JPA produced MIDs need
    // to be remembered so they can be regenerated on JPA <-> GAE switch
    producedMids.add(metadataIdentificationString);
    // Getting implements
    List<JavaType> interfaces = currentClassDetails.getImplementsTypes();
    List<? extends MethodMetadata> interfaceMethods = null;
    if (!interfaces.isEmpty()) {
        for (JavaType currentInterface : interfaces) {
            ClassOrInterfaceTypeDetails currentInterfaceDetails = getTypeLocationService().getTypeDetails(currentInterface);
            if (currentInterfaceDetails != null) {
                interfaceMethods = currentInterfaceDetails.getDeclaredMethods();
            }
        }
    }
    final JavaType target = governorPhysicalTypeMetadata.getType();
    // Prepare Equals
    final EqualsAnnotationValues equalsAnnotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
    List<FieldMetadata> equalsFields = null;
    FieldMetadata identifier = null;
    if (equalsAnnotationValues.isAnnotationFound()) {
        EqualsMetadataProvider equalsProvider = getEqualsMetadataProvider();
        equalsFields = equalsProvider.locateFields(target, equalsAnnotationValues.getExcludeFields(), declaredFieldsList, metadataIdentificationString);
        identifier = equalsProvider.getIdentifier(governorPhysicalTypeMetadata);
    }
    // Prepare ToString
    final ToStringAnnotationValues toStringAnnotationValues = new ToStringAnnotationValues(governorPhysicalTypeMetadata);
    List<FieldMetadata> toStringFields = null;
    if (toStringAnnotationValues.isAnnotationFound()) {
        ToStringMetadataProvider toStringProvider = getToStringMetadataProvider();
        toStringFields = toStringProvider.getToStringFields(governorPhysicalTypeMetadata, declaredFieldsList);
    }
    return new JavaBeanMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, declaredFields, interfaceMethods, getMemberDetailsScanner(), equalsAnnotationValues, equalsFields, identifier, toStringAnnotationValues, toStringFields);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 28 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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 29 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JpaOperationsImpl method getVersionField.

/**
 * This method generates the version field using the provided values
 *
 * @param entity
 * @param versionField
 * @param versionType
 * @param versionColumn
 * @return
 */
private FieldMetadata getVersionField(final JavaType entity, String versionField, final JavaType versionType, final String versionColumn) {
    if (StringUtils.isEmpty(versionField)) {
        versionField = "version";
    }
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    annotations.add(new AnnotationMetadataBuilder(VERSION));
    if (StringUtils.isNotEmpty(versionColumn)) {
        final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
        columnBuilder.addStringAttribute("name", versionColumn);
        annotations.add(columnBuilder);
    }
    FieldDetails versionFieldDetails = new FieldDetails(getTypeLocationService().getPhysicalTypeIdentifier(entity), versionType, new JavaSymbolName(versionField));
    versionFieldDetails.setModifiers(Modifier.PRIVATE);
    versionFieldDetails.addAnnotations(annotations);
    return new FieldMetadataBuilder(versionFieldDetails).build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) FieldDetails(org.springframework.roo.classpath.details.FieldDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 30 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25