Search in sources :

Example 11 with FieldMetadata

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

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

the class JpaOperationsImpl method getFieldChildPartOfCompositionRelation.

@Override
public Pair<FieldMetadata, RelationInfo> getFieldChildPartOfCompositionRelation(ClassOrInterfaceTypeDetails entityCdi) {
    JavaType domainType = entityCdi.getType();
    List<Pair<FieldMetadata, RelationInfo>> relations = getFieldChildPartOfRelation(entityCdi);
    if (relations.isEmpty()) {
        return null;
    }
    JpaEntityMetadata parent;
    JavaType parentType;
    RelationInfo info;
    List<Pair<FieldMetadata, RelationInfo>> compositionRelation = new ArrayList<Pair<FieldMetadata, RelationInfo>>();
    for (Pair<FieldMetadata, RelationInfo> field : relations) {
        if (field.getRight().type == JpaRelationType.COMPOSITION) {
            compositionRelation.add(field);
        }
    }
    Validate.isTrue(compositionRelation.size() <= 1, "Entity %s has more than one relations of composition as child part: ", domainType, StringUtils.join(getFieldNamesOfRelationList(compositionRelation), ","));
    if (compositionRelation.isEmpty()) {
        return null;
    }
    return compositionRelation.get(0);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) ArrayList(java.util.ArrayList) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) Pair(org.apache.commons.lang3.tuple.Pair)

Example 13 with FieldMetadata

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

the class EqualsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EqualsAnnotationValues annotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
    if (!annotationValues.isAnnotationFound()) {
        return null;
    }
    final MemberDetails memberDetails = getMemberDetails(governorPhysicalTypeMetadata);
    if (memberDetails == null) {
        return null;
    }
    AnnotationMetadata javaBeanAnnotation = memberDetails.getAnnotation(ROO_JAVA_BEAN);
    if (javaBeanAnnotation != null) {
        // Return an empty metadata as @RooJavaBean do the work
        return new EqualsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, new ArrayList<FieldMetadata>(), null, true);
    }
    final JavaType javaType = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getName();
    final List<FieldMetadata> equalityFields = locateFields(javaType, annotationValues.getExcludeFields(), memberDetails.getFields(), metadataIdentificationString);
    FieldMetadata identifierField = getIdentifier(governorPhysicalTypeMetadata);
    return new EqualsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, equalityFields, identifierField, false);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 14 with FieldMetadata

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

the class EqualsMetadataProviderImpl method locateFields.

/**
 * Locates class fields needed to generate `equals` and `hashCode` methods.
 * Also fills id field info if class is an entity.
 *
 * @param javaType the {@link JavaType} of the governor class.
 * @param excludeFields the {@link String[]} with field names to exclude.
 * @param memberDetails the {@link MemberDetails} of the class.
 * @param metadataIdentificationString
 * @return a {@link List<FieldMetadata>}
 */
public List<FieldMetadata> locateFields(final JavaType javaType, final String[] excludeFields, final List<FieldMetadata> fields, final String metadataIdentificationString) {
    final SortedSet<FieldMetadata> locatedFields = new TreeSet<FieldMetadata>(new Comparator<FieldMetadata>() {

        public int compare(final FieldMetadata l, final FieldMetadata r) {
            return l.getFieldName().compareTo(r.getFieldName());
        }
    });
    final List<?> excludeFieldsList = CollectionUtils.arrayToList(excludeFields);
    final FieldMetadata versionField = getPersistenceMemberLocator().getVersionField(javaType);
    for (final FieldMetadata field : fields) {
        if (excludeFieldsList.contains(field.getFieldName().getSymbolName())) {
            continue;
        }
        if (Modifier.isStatic(field.getModifier()) || Modifier.isTransient(field.getModifier()) || field.getFieldType().isCommonCollectionType() || field.getFieldType().isArray()) {
            continue;
        }
        if (versionField != null && field.getFieldName().equals(versionField.getFieldName())) {
            continue;
        }
        locatedFields.add(field);
        getMetadataDependencyRegistry().registerDependency(field.getDeclaredByMetadataId(), metadataIdentificationString);
    }
    return new ArrayList<FieldMetadata>(locatedFields);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList)

Example 15 with FieldMetadata

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

the class ToStringMetadataProviderImpl method getToStringFields.

/* (non-Javadoc)
   * @see org.springframework.roo.addon.javabean.addon.ToStringMetadataProvider#getToStringFields(org.springframework.roo.classpath.PhysicalTypeMetadata, java.util.List)
   */
public List<FieldMetadata> getToStringFields(final PhysicalTypeMetadata governorPhysicalTypeMetadata, final List<FieldMetadata> declaredFields) {
    ClassOrInterfaceTypeDetails superclass = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getSuperclass();
    List<FieldMetadata> toStringFields = new ArrayList<FieldMetadata>();
    if (superclass != null && superclass != JavaType.OBJECT) {
        List<FieldMetadata> superclassFields = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), superclass).getFields();
        for (FieldMetadata field : declaredFields) {
            boolean alreadyInSuperclass = false;
            for (FieldMetadata superclassField : superclassFields) {
                if (superclassField.getDeclaredByMetadataId().equals(field.getDeclaredByMetadataId()) && superclassField.getFieldName().equals(field.getFieldName())) {
                    alreadyInSuperclass = true;
                    break;
                }
            }
            if (!alreadyInSuperclass) {
                toStringFields.add(field);
            }
        }
    } else {
        toStringFields.addAll(declaredFields);
    }
    return toStringFields;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

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