Search in sources :

Example 1 with ClassOrInterfaceTypeDetails

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

the class EntityDeserializerMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final EntityDeserializerAnnotationValues values = new EntityDeserializerAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType deserializerType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), deserializerType, RooJavaType.ROO_DESERIALIZER.getSimpleTypeName());
    // Register JpaEntityMetadata dependency
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(entityId, metadataIdentificationString);
    // Register JavaBeanMetadata dependency
    final String javaBeanId = JavaBeanMetadata.createIdentifier(entityDetails);
    final JavaBeanMetadata javaBeanMetadata = getMetadataService().get(javaBeanId);
    if (javaBeanMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    registerDependency(javaBeanId, metadataIdentificationString);
    // Register ServiceMetadata dependency
    ClassOrInterfaceTypeDetails serviceDetails = getServiceLocator().getService(entity);
    String serviceMetadataId = ServiceMetadata.createIdentifier(serviceDetails);
    ServiceMetadata serviceMetadata = getMetadataService().get(serviceMetadataId);
    if (serviceMetadata == null) {
        // not ready to this metadata yet
        return null;
    }
    return new EntityDeserializerMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, serviceMetadata);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaBeanMetadata(org.springframework.roo.addon.javabean.addon.JavaBeanMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Example 2 with ClassOrInterfaceTypeDetails

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

the class JSONMixinMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JSONMixinAnnotationValues values = new JSONMixinAnnotationValues(governorPhysicalTypeMetadata);
    final JavaType mixinType = governorPhysicalTypeMetadata.getType();
    final JavaType entity = values.getEntity();
    final ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    Validate.notNull(entityDetails, "Can't get details of '%s' defined on '%s.@%s.entity'", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    Validate.notNull(entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Class '%s' defined on '%s.@%s.entity' has no @%s annotation. Only JPA entities can set as mixin", entity.getFullyQualifiedTypeName(), mixinType, RooJavaType.ROO_JSON_MIXIN.getSimpleTypeName());
    final String entityId = JpaEntityMetadata.createIdentifier(entityDetails);
    final JpaEntityMetadata entityMetadata = getMetadataService().get(entityId);
    if (entityMetadata == null) {
        // not ready for this metadata yet
        return null;
    }
    // register metadata dependency
    registerDependency(entityId, metadataIdentificationString);
    // Register dependency with DomainModelModule
    Set<ClassOrInterfaceTypeDetails> domainModelModuleDetails = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_DOMAIN_MODEL_MODULE);
    if (!domainModelModuleDetails.isEmpty()) {
        String domainModelModuleMetadataId = DomainModelModuleMetadata.createIdentifier(domainModelModuleDetails.iterator().next());
        registerDependency(metadataIdentificationString, domainModelModuleMetadataId);
    }
    Map<FieldMetadata, JavaType> jsonDeserializerByEntity = new TreeMap<FieldMetadata, JavaType>(FieldMetadata.COMPARATOR_BY_NAME);
    for (FieldMetadata field : entityMetadata.getRelationsAsChild().values()) {
        if (isAnyToOneRelation(field)) {
            JavaType parentEntity = field.getFieldType().withoutParameters();
            JavaType entityDeserializer = getEntityDeserializerFor(parentEntity);
            Validate.notNull(entityDeserializer, "Can't locate class with @%s.entity=%s required for %s entity Json Mixin (%s)", RooJavaType.ROO_DESERIALIZER, parentEntity, entity.getFullyQualifiedTypeName(), mixinType.getFullyQualifiedTypeName());
            jsonDeserializerByEntity.put(field, entityDeserializer);
        }
    }
    return new JSONMixinMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, values, entityMetadata, jsonDeserializerByEntity);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) TreeMap(java.util.TreeMap)

Example 3 with ClassOrInterfaceTypeDetails

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

the class ExceptionsOperationsImpl method addHandlersAnnotations.

/**
 * Generates {@link RooExceptionHandlers} and {@link RooExceptionHandler} annotations
 * and adds or updates it on specified class.
 *
 * @param exception
 * @param targetClass
 * @param errorView
 */
private void addHandlersAnnotations(JavaType exception, JavaType targetClass, String errorView) {
    Validate.notNull(targetClass, "Target class is required to add @RooExceptionHandlers annotation");
    // Create @RooExceptionHandler Annotation
    final AnnotationMetadataBuilder exceptionHandlerAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLER);
    final List<AnnotationAttributeValue<?>> exceptionHandlerAnnotationAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    exceptionHandlerAnnotationAttributes.add(new ClassAttributeValue(new JavaSymbolName(EXCEPTION), exception));
    if (errorView != null) {
        exceptionHandlerAnnotationAttributes.add(new StringAttributeValue(new JavaSymbolName(ERROR_VIEW), errorView));
    }
    exceptionHandlerAnnotationBuilder.setAttributes(exceptionHandlerAnnotationAttributes);
    // Check if container annotation already exists
    ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(targetClass);
    ClassOrInterfaceTypeDetailsBuilder typeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(typeDetails);
    AnnotationMetadata exceptionHandlersAnnotation = typeDetails.getAnnotation(RooJavaType.ROO_EXCEPTION_HANDLERS);
    AnnotationMetadataBuilder exceptionHandlersAnnotationBuilder = null;
    if (exceptionHandlersAnnotation != null) {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(exceptionHandlersAnnotation);
    } else {
        exceptionHandlersAnnotationBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_EXCEPTION_HANDLERS);
    }
    Validate.notNull(exceptionHandlersAnnotationBuilder);
    // Add @RooExceptionHandler annotation into @RooExceptionHandlers
    final List<NestedAnnotationAttributeValue> exceptionHandlersArrayValues = new ArrayList<NestedAnnotationAttributeValue>();
    exceptionHandlersArrayValues.add(new NestedAnnotationAttributeValue(new JavaSymbolName(VALUE), exceptionHandlerAnnotationBuilder.build()));
    final List<AnnotationAttributeValue<?>> attributeValues = new ArrayList<AnnotationAttributeValue<?>>();
    attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
    if (exceptionHandlersAnnotation == null) {
        // Add new @RooExceptionHandlers annotation with given values
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        typeDetailsBuilder.addAnnotation(exceptionHandlersAnnotationBuilder.build());
    } else {
        // Get current annotation values from @RooExceptionHandlers annotation
        AnnotationAttributeValue<?> currentHandlers = exceptionHandlersAnnotation.getAttribute(VALUE);
        if (currentHandlers != null) {
            List<?> values = (List<?>) currentHandlers.getValue();
            Iterator<?> it = values.iterator();
            while (it.hasNext()) {
                NestedAnnotationAttributeValue handler = (NestedAnnotationAttributeValue) it.next();
                if (handler.getValue() != null) {
                    // Check if there is a @RooExceptionHandlers with same 'exception' value
                    if (exceptionHandlerAnnotationBuilder.build().getAttribute(EXCEPTION).getValue().equals(handler.getValue().getAttribute(EXCEPTION).getValue())) {
                        LOGGER.warning(String.format("There is already a handler for exception %s in class %s", exception.getSimpleTypeName(), targetClass.getSimpleTypeName()));
                        return;
                    }
                    exceptionHandlersArrayValues.add(handler);
                }
            }
        }
        // Add found values
        attributeValues.add(new ArrayAttributeValue<NestedAnnotationAttributeValue>(new JavaSymbolName(VALUE), exceptionHandlersArrayValues));
        exceptionHandlersAnnotationBuilder.setAttributes(attributeValues);
        // Update annotation
        typeDetailsBuilder.updateTypeAnnotation(exceptionHandlersAnnotationBuilder.build());
    }
    // Write to disk
    getTypeManagementService().createOrUpdateTypeOnDisk(typeDetailsBuilder.build());
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 4 with ClassOrInterfaceTypeDetails

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

the class ExceptionsOperationsImpl method isExceptionAnnotatedWithResponseStatus.

private boolean isExceptionAnnotatedWithResponseStatus(JavaType exception) {
    ClassOrInterfaceTypeDetails exceptionCid = typeLocationService.getTypeDetails(exception);
    Validate.notNull(exceptionCid, String.format("Can't found class: %s", exception.getFullyQualifiedTypeName()));
    AnnotationMetadata annotation = exceptionCid.getAnnotation(SpringJavaType.RESPONSE_STATUS);
    if (annotation == null) {
        return false;
    }
    return true;
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 5 with ClassOrInterfaceTypeDetails

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

the class TestCommands method getAllEntities.

@CliOptionAutocompleteIndicator(command = "test integration", param = "class", help = "Option `--class` must " + "be a non-abstract valid type. Please, use auto-complete feature to select it.")
public List<String> getAllEntities(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("class");
    // Create results to return
    List<String> results = new ArrayList<String>();
    // Look for all valid types for all available test creators
    for (TestCreatorProvider creator : getAllTestCreators()) {
        if (creator.isIntegrationTestCreationAvailable()) {
            for (JavaType annotationType : creator.getValidTypes()) {
                // Look for types with this annotation type
                Set<ClassOrInterfaceTypeDetails> types = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(annotationType);
                for (ClassOrInterfaceTypeDetails typeCid : types) {
                    String name = replaceTopLevelPackageString(typeCid.getType(), currentText);
                    if (!results.contains(name)) {
                        results.add(name);
                    }
                }
            }
        }
    }
    return results;
}
Also used : JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) TestCreatorProvider(org.springframework.roo.addon.test.providers.TestCreatorProvider) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17