Search in sources :

Example 6 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class EmbeddableFieldCreatorProvider method createEmbeddedField.

@Override
public void createEmbeddedField(JavaType typeName, JavaType fieldType, JavaSymbolName fieldName, boolean permitReservedWords, List<AnnotationMetadataBuilder> extraAnnotations) {
    // Check if the requested entity is a JPA @Entity
    final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(typeName);
    Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", typeName);
    final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
    final PhysicalTypeMetadata targetTypeMetadata = (PhysicalTypeMetadata) metadataService.get(physicalTypeIdentifier);
    Validate.notNull(targetTypeMetadata, "The specified target '--class' does not exist or can not be found. Please create this type first.");
    final PhysicalTypeDetails targetPtd = targetTypeMetadata.getMemberHoldingTypeDetails();
    Validate.isInstanceOf(MemberHoldingTypeDetails.class, targetPtd);
    final ClassOrInterfaceTypeDetails targetTypeCid = (ClassOrInterfaceTypeDetails) targetPtd;
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), targetTypeCid);
    Validate.isTrue(memberDetails.getAnnotation(ENTITY) != null || memberDetails.getAnnotation(PERSISTENT) != null, "The field embedded command is only applicable to JPA @Entity or Spring Data @Persistent target types.");
    final EmbeddedField fieldDetails = new EmbeddedField(physicalTypeIdentifier, fieldType, fieldName);
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    insertField(fieldDetails, permitReservedWords, false);
}
Also used : EmbeddedField(org.springframework.roo.classpath.operations.jsr303.EmbeddedField) PhysicalTypeDetails(org.springframework.roo.classpath.PhysicalTypeDetails) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 7 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class EmbeddableFieldCreatorProvider method isValid.

@Override
public boolean isValid(JavaType javaType) {
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(javaType);
    MemberDetails details = memberDetailsScanner.getMemberDetails(this.getClass().getName(), cid);
    if (cid.getAnnotation(JpaJavaType.EMBEDDABLE) != null || details.getAnnotation(JpaJavaType.EMBEDDABLE) != null) {
        return true;
    }
    return false;
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 8 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class JpaFieldCreatorProvider method checkFieldExists.

/**
 * Checks if entity has already a field with the same name and throws an exception
 * in that case.
 *
 * @param fieldName
 * @param isforce
 * @param javaTypeDetails
 * @param parameterName
 */
private void checkFieldExists(final JavaSymbolName fieldName, final boolean isForce, final ClassOrInterfaceTypeDetails javaTypeDetails, final String parameterName) {
    MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), javaTypeDetails);
    List<FieldMetadata> fields = memberDetails.getFields();
    for (FieldMetadata field : fields) {
        if (field.getFieldName().equals(fieldName) && !isForce) {
            throw new IllegalArgumentException(String.format("Field '%s' already exists and cannot be created. Try to use a " + "different field name on --%s parameter or use --force parameter to overwrite it.", fieldName, parameterName));
        }
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 9 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.

the class JpaDataOnDemandCreator method newDataOnDemandClass.

/**
 * Creates a new data-on-demand provider for an entity. Silently returns
 * if the DoD class already exists.
 *
 * @param entity to produce a DoD provider for
 * @param name the name of the new DoD class
 */
private JavaType newDataOnDemandClass(JavaType entity, JavaType name) {
    Validate.notNull(entity, "Entity to produce a data on demand provider for is required");
    Validate.notNull(name, "Name of the new data on demand provider is required");
    final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, name.getModule());
    Validate.notNull(path, "Location of the new data on demand provider is required");
    // Add javax validation dependency
    projectOperations.addDependency(name.getModule(), VALIDATION_API_DEPENDENCY);
    // Verify the requested entity actually exists as a class and is not
    // abstract
    final ClassOrInterfaceTypeDetails cid = getEntityDetails(entity);
    Validate.isTrue(cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS, "Type %s is not a class", entity.getFullyQualifiedTypeName());
    Validate.isTrue(!Modifier.isAbstract(cid.getModifier()), "Type %s is abstract", entity.getFullyQualifiedTypeName());
    // Check if the requested entity is a JPA @Entity
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(JpaDataOnDemandCreator.class.getName(), cid);
    final AnnotationMetadata entityAnnotation = memberDetails.getAnnotation(ENTITY);
    Validate.isTrue(entityAnnotation != null, "Type %s must be a JPA entity type", entity.getFullyQualifiedTypeName());
    // Everything is OK to proceed
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, path);
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId).getName();
    }
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    final List<AnnotationAttributeValue<?>> dodConfig = new ArrayList<AnnotationAttributeValue<?>>();
    dodConfig.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
    annotations.add(new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_DATA_ON_DEMAND, dodConfig));
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(annotations);
    // Write changes on disk
    final ClassOrInterfaceTypeDetails dodClassCid = cidBuilder.build();
    typeManagementService.createOrUpdateTypeOnDisk(dodClassCid);
    return cid.getName();
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 10 with MemberDetails

use of org.springframework.roo.classpath.scanner.MemberDetails 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)

Aggregations

MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)56 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)36 JavaType (org.springframework.roo.model.JavaType)35 RooJavaType (org.springframework.roo.model.RooJavaType)31 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)29 ArrayList (java.util.ArrayList)25 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)20 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)14 SpringJavaType (org.springframework.roo.model.SpringJavaType)12 List (java.util.List)10 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)9 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)8 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)7 FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)7 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)6 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)6