Search in sources :

Example 96 with FieldMetadata

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

the class EqualsMetadata method getDefaultHashCodeMethodReturnStatment.

/**
 * Returns the default `hasCode` method return statement
 *
 * @return a {@link StringBuilder}
 */
private static StringBuilder getDefaultHashCodeMethodReturnStatment(EqualsAnnotationValues annotationValues, List<FieldMetadata> locatedFields, ImportRegistrationResolver importRegistrationResolver) {
    final StringBuilder builder = new StringBuilder(String.format("return new %s()", HASH_CODE_BUILDER.getNameIncludingTypeParameters(false, importRegistrationResolver)));
    if (annotationValues.isAppendSuper()) {
        builder.append(".appendSuper(super.hashCode())");
    }
    for (final FieldMetadata field : locatedFields) {
        builder.append(".append(" + field.getFieldName() + ")");
    }
    builder.append(".toHashCode();");
    return builder;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder)

Example 97 with FieldMetadata

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

the class EntityProjectionMetadataProviderImpl method buildFieldMetadataFromAnnotation.

/**
 * Builds FieldMetadata to provide {@link EntityProjectionMetadata} with the necessary
 * resources to create constructor.
 *
 * @param fields the String[] from 'fields' annotation parameter.
 * @param cid the governor ClassOrInterfaceTypeDetails, that is, the Projection physical type.
 * @return the List<FieldMetadata> with the fields to build the constructor.
 */
private List<FieldMetadata> buildFieldMetadataFromAnnotation(String[] fields, ClassOrInterfaceTypeDetails cid) {
    List<FieldMetadata> allFields = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), cid).getFields();
    List<FieldMetadata> fieldsToAdd = new ArrayList<FieldMetadata>();
    // Iterate over all specified fields
    for (int i = 0; i < fields.length; i++) {
        String fieldName = "";
        boolean existsInGovernor = false;
        // Build field name following Java convention
        String[] splittedByDot = StringUtils.split(fields[i], ".");
        for (int t = 0; t < splittedByDot.length; t++) {
            if (t == 0) {
                fieldName = fieldName.concat(splittedByDot[t]);
            } else {
                fieldName = fieldName.concat(StringUtils.capitalize(splittedByDot[t]));
            }
        }
        // Check existence in governor
        for (FieldMetadata field : allFields) {
            if (field.getFieldName().getSymbolName().equals(fieldName)) {
                existsInGovernor = true;
                fieldsToAdd.add(field);
            }
        }
        if (!existsInGovernor) {
            throw new IllegalStateException(String.format("Field %s couldn't be located in %s. Please, be sure that it is well written in 'fields' param of @RooEntityProjection.", fieldName, cid.getType().getFullyQualifiedTypeName()));
        }
    }
    return fieldsToAdd;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList)

Example 98 with FieldMetadata

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

the class EntityProjectionMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EntityProjectionAnnotationValues annotationValues = new EntityProjectionAnnotationValues(governorPhysicalTypeMetadata);
    // Get CID from governor
    ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    // Get all projection fields from annotation
    String[] fieldsString = annotationValues.getFields();
    List<FieldMetadata> fields = buildFieldMetadataFromAnnotation(fieldsString, cid);
    // Add dependency between modules
    for (FieldMetadata field : fields) {
        getTypeLocationService().addModuleDependency(cid.getName().getModule(), field.getFieldType());
    }
    return new EntityProjectionMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, fields);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 99 with FieldMetadata

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

the class EqualsMetadataProviderImpl method getIdentifier.

public FieldMetadata getIdentifier(final PhysicalTypeMetadata governorPhysicalTypeMetadata) {
    List<FieldMetadata> identifierFields = getPersistenceMemberLocator().getIdentifierFields(governorPhysicalTypeMetadata.getType());
    FieldMetadata identifierField = null;
    if (!identifierFields.isEmpty()) {
        identifierField = identifierFields.get(0);
    }
    return identifierField;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata)

Example 100 with FieldMetadata

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

the class FinderOperationsImpl method findDtoFieldRecursivelyAndAddToMappings.

private boolean findDtoFieldRecursivelyAndAddToMappings(JavaType entity, Map<String, String> fieldNamesMap, Map<String, FieldMetadata> fieldMetadataMap, boolean found, FieldMetadata field, JavaSymbolName finderFieldName, JavaType finderFieldType, List<FieldMetadata> allDtoFields, String dtoSimpleName) {
    JavaType currentEntity;
    if (getTypeLocationService().getTypeDetails(field.getFieldType()) != null && getTypeLocationService().getTypeDetails(field.getFieldType()).getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
        // Change current entity
        currentEntity = field.getFieldType();
        // Modify pathName with one more level
        String pathName = StringUtils.uncapitalize(entity.getSimpleTypeName()).concat(".").concat(field.getFieldName().getSymbolName());
        List<FieldMetadata> relatedEntityFields = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(currentEntity)).getFields();
        for (FieldMetadata relatedField : relatedEntityFields) {
            if (relatedField.getFieldName().equals(finderFieldName) && relatedField.getFieldType().equals(finderFieldType)) {
                // Add FieldMetadata from DTO to fieldMetadataMap
                boolean fieldFoundInDto = false;
                for (FieldMetadata dtoField : allDtoFields) {
                    if (dtoField.getFieldName().equals(finderFieldName) && dtoField.getFieldType().equals(finderFieldType)) {
                        fieldMetadataMap.put(finderFieldName.getSymbolName(), dtoField);
                        fieldFoundInDto = true;
                    }
                }
                Validate.isTrue(fieldFoundInDto, "Couldn't find a field with same name and type that %s on DTO %s", finderFieldName.getSymbolName(), dtoSimpleName);
                fieldNamesMap.put(finderFieldName.getSymbolName(), pathName.concat(".").concat(relatedField.getFieldName().getSymbolName()));
                found = true;
                break;
            } else {
                findDtoFieldRecursivelyAndAddToMappings(currentEntity, fieldNamesMap, fieldMetadataMap, found, relatedField, finderFieldName, finderFieldType, allDtoFields, dtoSimpleName);
            }
        }
    }
    return found;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata)

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