Search in sources :

Example 61 with AnnotationMetadata

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

the class SecurityOperationsImpl method generateFilterAnnotations.

public void generateFilterAnnotations(JavaType klass, String methodName, String roles, String usernames, String when) {
    // Get methods to annotate.
    // With the last parameter to false, we avoid that push in action occurs.
    List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
    List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (Object pushedElement : pushedElements) {
        if (pushedElement instanceof DefaultMethodMetadata) {
            DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
            // Get parameters
            List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
            List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
            Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
            while (iterParamTypes.hasNext()) {
                ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
                lstParamTypes.add(parameterAttributeValue);
            }
            // Generate new annotations @RooSecurityFilter
            NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityFilterAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames, when).build());
            rooSecurityFiltersToAdd.add(newFilter);
        }
    }
    // Get actual values of @RooSecurityFilters
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
    // Check annotation @RooSecurityFilters to delete defined annotations
    // that will be redefined
    AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    AnnotationMetadataBuilder annotationFiltersMetadataBuilder;
    if (annotationFilters != null) {
        // Getting filters from annotation
        AnnotationAttributeValue<?> attributeFilters = annotationFilters.getAttribute("filters");
        List<?> values = (List<?>) attributeFilters.getValue();
        if (values != null && !values.isEmpty()) {
            Iterator<?> valuesIt = values.iterator();
            while (valuesIt.hasNext()) {
                NestedAnnotationAttributeValue filterAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
                if (checkRooSecurityFilterMaintainAnnotation(rooSecurityFiltersToAdd, filterAnnotation)) {
                    // Maintain annotation if 'method', 'parameters' or 'when' are different
                    rooSecurityFiltersToAdd.add(filterAnnotation);
                }
            }
        }
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(annotationFilters);
        // remove annotation
        cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    } else {
        // Doesn't exist @RooSecurityFilters, create it
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTERS);
    }
    // Add filters attribute
    ArrayAttributeValue<AnnotationAttributeValue<?>> newFilters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("filters"), rooSecurityFiltersToAdd);
    annotationFiltersMetadataBuilder.addAttribute(newFilters);
    // Include new @RooSecurityFilters annotation
    cidBuilder.addAnnotation(annotationFiltersMetadataBuilder);
    // Write on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Add Spring Security dependency
    getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DefaultMethodMetadata(org.springframework.roo.classpath.details.DefaultMethodMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 62 with AnnotationMetadata

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

the class ExceptionsMetadataProviderImpl method getNestedAttributeValue.

/**
 * Gets value of a nested annotation attribute value
 *
 * @param newstedAnnotationAttr
 * @param attributeName
 * @return
 */
private <T> T getNestedAttributeValue(NestedAnnotationAttributeValue nestedAnnotationAttr, String attributeName) {
    AnnotationMetadata annotationValue = nestedAnnotationAttr.getValue();
    if (annotationValue == null) {
        return null;
    }
    AnnotationAttributeValue<?> attribute = annotationValue.getAttribute(attributeName);
    if (attribute == null) {
        return null;
    }
    return (T) attribute.getValue();
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 63 with AnnotationMetadata

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

the class ExceptionsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(String metadataIdentificationString, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, String itdFilename) {
    AnnotationMetadata handlersAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(ROO_EXCEPTION_HANDLERS);
    // Prepare list to register @RooExceptionHandler annotations data
    List<ExceptionHandlerAnnotationValues> exceptionHandlerValues = new ArrayList<ExceptionHandlerAnnotationValues>();
    // Get nested annotations
    AnnotationAttributeValue<Object> handlers = handlersAnnotation.getAttribute("value");
    List<?> values = (List<?>) handlers.getValue();
    Iterator<?> valuesIt = values.iterator();
    // Iterate over nested annotations
    while (valuesIt.hasNext()) {
        NestedAnnotationAttributeValue handlerAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
        if (handlerAnnotation.getValue() != null) {
            // Get attribute values
            JavaType exception = getNestedAttributeValue(handlerAnnotation, "exception");
            ClassOrInterfaceTypeDetails exceptionDetails = getTypeLocationService().getTypeDetails(exception);
            Validate.notNull(exception, "'exception' attribute in @RooExceptionHandler must not be null");
            String errorView = getNestedAttributeValue(handlerAnnotation, "errorView");
            // Register attribute values
            exceptionHandlerValues.add(new ExceptionHandlerAnnotationValues(exceptionDetails, errorView));
        }
    }
    // Check if type is a controller
    AnnotationMetadata rooControllerAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_CONTROLLER);
    boolean isController = rooControllerAnnotation != null;
    List<FieldMetadata> fieldsMetadata = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), governorPhysicalTypeMetadata.getMemberHoldingTypeDetails()).getFields();
    return new ExceptionsMetadata(metadataIdentificationString, exceptionHandlerValues, aspectName, governorPhysicalTypeMetadata, fieldsMetadata, isController);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 64 with AnnotationMetadata

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

the class ExceptionsOperationsImpl method isRooController.

/**
 * Returns true if a class is annotated with {@link RooController}
 *
 * @param klass
 * @return
 */
private boolean isRooController(JavaType klass) {
    ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(klass);
    Validate.notNull(typeDetails, String.format("Can't found class: %s", klass.getFullyQualifiedTypeName()));
    AnnotationMetadata annotation = typeDetails.getAnnotation(RooJavaType.ROO_CONTROLLER);
    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 65 with AnnotationMetadata

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

the class ExceptionsOperationsImpl method createControllerAdviceIfRequired.

/**
 * Creates a new class annotated with @ControllerAdvice if not exists. Returns true if success
 * or class already exists.
 *
 * Returns false if class already exists but it's not annotated with @ControllerAdvice.
 *
 * @param controllerAdviceClass
 */
private boolean createControllerAdviceIfRequired(JavaType controllerAdviceClass) {
    // Checks if new service interface already exists.
    final String controllerAdviceClassIdentifier = getPathResolver().getCanonicalPath(controllerAdviceClass.getModule(), Path.SRC_MAIN_JAVA, controllerAdviceClass);
    if (!getFileManager().exists(controllerAdviceClassIdentifier)) {
        // Creating class builder
        final String mid = PhysicalTypeIdentifier.createIdentifier(controllerAdviceClass, getPathResolver().getPath(controllerAdviceClassIdentifier));
        final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, controllerAdviceClass, PhysicalTypeCategory.CLASS);
        typeBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.CONTROLLER_ADVICE).build());
        // Write new class disk
        getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
    } else {
        // Check if class is annotated with @ControllerAdvice
        ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(controllerAdviceClass);
        AnnotationMetadata annotation = typeDetails.getAnnotation(SpringJavaType.CONTROLLER_ADVICE);
        if (annotation == null) {
            LOGGER.warning("Class must be annotated with @ControllerAdvice");
            return false;
        }
    }
    return true;
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10