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);
}
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();
}
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);
}
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;
}
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;
}
Aggregations