Search in sources :

Example 36 with JavaType

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

the class JavaBeanMetadata method getEntityCollectionMutatorBody.

private InvocableMemberBodyBuilder getEntityCollectionMutatorBody(final FieldMetadata field, final JavaSymbolName entityIdsFieldName) {
    final String entityCollectionName = field.getFieldName().getSymbolName();
    final String entityIdsName = entityIdsFieldName.getSymbolName();
    final JavaType collectionElementType = field.getFieldType().getParameters().get(0);
    final String localEnitiesName = "local" + StringUtils.capitalize(entityCollectionName);
    JavaType collectionType = field.getFieldType();
    builder.getImportRegistrationResolver().addImport(collectionType);
    final String collectionName = field.getFieldType().getNameIncludingTypeParameters().replace(field.getFieldType().getPackage().getFullyQualifiedPackageName() + ".", "");
    String instantiableCollection = collectionName;
    if (collectionType.getFullyQualifiedTypeName().equals(LIST.getFullyQualifiedTypeName())) {
        collectionType = new JavaType(ARRAY_LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
        instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
    } else if (collectionType.getFullyQualifiedTypeName().equals(SET.getFullyQualifiedTypeName())) {
        collectionType = new JavaType(HASH_SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, collectionType.getParameters());
        instantiableCollection = collectionType.getNameIncludingTypeParameters().replace(collectionType.getPackage().getFullyQualifiedPackageName() + ".", "");
    }
    builder.getImportRegistrationResolver().addImports(collectionType, LIST, ARRAY_LIST);
    final String identifierMethodName = getIdentifierMethodName(field).getSymbolName();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine(collectionName + " " + localEnitiesName + " = new " + instantiableCollection + "();");
    bodyBuilder.appendFormalLine("List<Long> longIds = new ArrayList<Long>();");
    bodyBuilder.appendFormalLine("for (Key key : " + entityIdsName + ") {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("if (!longIds.contains(key.getId())) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("longIds.add(key.getId());");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("for (" + collectionElementType.getSimpleTypeName() + " entity : " + entityCollectionName + ") {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("if (!longIds.contains(entity." + identifierMethodName + "())) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("longIds.add(entity." + identifierMethodName + "());");
    bodyBuilder.appendFormalLine(entityIdsName + ".add(KeyFactory.createKey(" + collectionElementType.getSimpleTypeName() + ".class.getName(), entity." + identifierMethodName + "()));");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine(localEnitiesName + ".add(entity);");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("this." + entityCollectionName + " = " + localEnitiesName + ";");
    return bodyBuilder;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) RooToString(org.springframework.roo.addon.javabean.annotations.RooToString) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 37 with JavaType

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

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

the class JavaBeanMetadataProviderImpl method getIdentifierAccessorMethodName.

private JavaSymbolName getIdentifierAccessorMethodName(final FieldMetadata field, final String metadataIdentificationString) {
    if (projectOperations == null) {
        projectOperations = getProjectOperations();
    }
    Validate.notNull(projectOperations, "ProjectOperations is required");
    final LogicalPath path = PhysicalTypeIdentifier.getPath(field.getDeclaredByMetadataId());
    final String moduleNme = path.getModule();
    if (projectOperations.isProjectAvailable(moduleNme) || !projectOperations.isFeatureInstalled(FeatureNames.GAE)) {
        return null;
    }
    // @javax.persistence.Transient
    for (final AnnotationMetadata annotationMetadata : field.getAnnotations()) {
        if (annotationMetadata.getAnnotationType().equals(TRANSIENT)) {
            return null;
        }
    }
    JavaType fieldType = field.getFieldType();
    // type
    if (fieldType.isCommonCollectionType()) {
        if (fieldType.getParameters().isEmpty()) {
            return null;
        }
        fieldType = fieldType.getParameters().get(0);
    }
    final MethodMetadata identifierAccessor = getPersistenceMemberLocator().getIdentifierAccessor(fieldType);
    if (identifierAccessor != null) {
        getMetadataDependencyRegistry().registerDependency(identifierAccessor.getDeclaredByMetadataId(), metadataIdentificationString);
        return identifierAccessor.getMethodName();
    }
    return null;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) LogicalPath(org.springframework.roo.project.LogicalPath) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 39 with JavaType

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

the class JavaBeanMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.

@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
    final JavaType javaType = JavaBeanMetadata.getJavaType(metadataIdentificationString);
    final LogicalPath path = JavaBeanMetadata.getPath(metadataIdentificationString);
    return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) LogicalPath(org.springframework.roo.project.LogicalPath)

Example 40 with JavaType

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

the class SerializableMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.

@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
    final JavaType javaType = SerializableMetadata.getJavaType(metadataIdentificationString);
    final LogicalPath path = SerializableMetadata.getPath(metadataIdentificationString);
    return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Also used : JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) LogicalPath(org.springframework.roo.project.LogicalPath)

Aggregations

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30