use of org.springframework.roo.classpath.details.annotations.ClassAttributeValue in project spring-roo by spring-projects.
the class SecurityOperationsImpl method generateAuthorizeAnnotations.
@Override
public void generateAuthorizeAnnotations(JavaType klass, String methodName, String roles, String usernames) {
Validate.notNull(klass, "ERROR: klass parameter is mandatory on 'generateAuthorizeAnnotations' method");
Validate.notNull(methodName, "ERROR: method parameter is mandatory on 'generateAuthorizeAnnotations' method");
// 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<?>> rooSecurityAuthorizationsToAdd = 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 @RooSecurityAuthorization
NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityAuthorizationsAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames).build());
rooSecurityAuthorizationsToAdd.add(newFilter);
}
}
// Get actual values of @RooSecurityAuthorizations
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
// Check annotation @RooSecurityAuthorizations to delete defined annotations
// that will be redefined
AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
AnnotationMetadataBuilder annotationAuthorizationsMetadataBuilder;
if (annotationAuthorizations != null) {
// Getting authorizations from annotation
AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
List<?> values = (List<?>) attributeAuthorizations.getValue();
if (values != null && !values.isEmpty()) {
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
if (checkRooSecurityAuthorizationMaintainAnnotation(rooSecurityAuthorizationsToAdd, authorizationAnnotation)) {
// Maintain annotation if 'method' or 'parameters' are different
rooSecurityAuthorizationsToAdd.add(authorizationAnnotation);
}
}
}
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(annotationAuthorizations);
// remove annotation
cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
} else {
// Doesn't exist @RooSecurityAuthorizations, create it
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
}
// Add authorizations attribute
ArrayAttributeValue<AnnotationAttributeValue<?>> newAuthorizations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("authorizations"), rooSecurityAuthorizationsToAdd);
annotationAuthorizationsMetadataBuilder.addAttribute(newAuthorizations);
// Include new @RooSecurityAuthorizations annotation
cidBuilder.addAnnotation(annotationAuthorizationsMetadataBuilder);
// 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.ClassAttributeValue 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.ClassAttributeValue in project spring-roo by spring-projects.
the class JavaParserAnnotationMetadataBuilder method convert.
@SuppressWarnings("unchecked")
private static MemberValuePair convert(final AnnotationAttributeValue<?> value, CompilationUnitServices compilationUnitServices) {
if (value instanceof NestedAnnotationAttributeValue) {
final NestedAnnotationAttributeValue castValue = (NestedAnnotationAttributeValue) value;
AnnotationExpr annotationExpr;
final AnnotationMetadata nestedAnnotation = castValue.getValue();
if (castValue.getValue().getAttributeNames().size() == 0) {
annotationExpr = new MarkerAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()));
} else if (castValue.getValue().getAttributeNames().size() == 1 && (castValue.getValue().getAttributeNames().get(0) == null || "value".equals(castValue.getValue().getAttributeNames().get(0).getSymbolName()))) {
annotationExpr = new SingleMemberAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), convert(nestedAnnotation.getAttribute(nestedAnnotation.getAttributeNames().get(0)), compilationUnitServices).getValue());
} else {
final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
for (final JavaSymbolName attributeName : nestedAnnotation.getAttributeNames()) {
memberValuePairs.add(convert(nestedAnnotation.getAttribute(attributeName), compilationUnitServices));
}
annotationExpr = new NormalAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), memberValuePairs);
}
// Add imports for nested annotation types
JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), nestedAnnotation.getAnnotationType());
// Rely on the nested instance to know its member value pairs
return new MemberValuePair(value.getName().getSymbolName(), annotationExpr);
}
if (value instanceof BooleanAttributeValue) {
final boolean castValue = ((BooleanAttributeValue) value).getValue();
final BooleanLiteralExpr convertedValue = new BooleanLiteralExpr(castValue);
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof CharAttributeValue) {
final char castValue = ((CharAttributeValue) value).getValue();
final CharLiteralExpr convertedValue = new CharLiteralExpr(new String(new char[] { castValue }));
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof LongAttributeValue) {
final Long castValue = ((LongAttributeValue) value).getValue();
final LongLiteralExpr convertedValue = new LongLiteralExpr(castValue.toString() + "L");
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof IntegerAttributeValue) {
final Integer castValue = ((IntegerAttributeValue) value).getValue();
final IntegerLiteralExpr convertedValue = new IntegerLiteralExpr(castValue.toString());
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof DoubleAttributeValue) {
final DoubleAttributeValue doubleAttributeValue = (DoubleAttributeValue) value;
final Double castValue = doubleAttributeValue.getValue();
DoubleLiteralExpr convertedValue;
if (doubleAttributeValue.isFloatingPrecisionOnly()) {
convertedValue = new DoubleLiteralExpr(castValue.toString() + "F");
} else {
convertedValue = new DoubleLiteralExpr(castValue.toString() + "D");
}
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof StringAttributeValue) {
final String castValue = ((StringAttributeValue) value).getValue();
final StringLiteralExpr convertedValue = new StringLiteralExpr(castValue.toString());
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof EnumAttributeValue) {
final EnumDetails castValue = ((EnumAttributeValue) value).getValue();
// This isn't as elegant as it could be (ie loss of type
// parameters), but it will do for now
final FieldAccessExpr convertedValue = new FieldAccessExpr(JavaParserUtils.getNameExpr(castValue.getType().getFullyQualifiedTypeName()), castValue.getField().getSymbolName());
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof ClassAttributeValue) {
final JavaType castValue = ((ClassAttributeValue) value).getValue();
// This doesn't preserve type parameters
final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), castValue);
final ClassExpr convertedValue = new ClassExpr(JavaParserUtils.getReferenceType(nameExpr));
return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
}
if (value instanceof ArrayAttributeValue) {
final ArrayAttributeValue<AnnotationAttributeValue<?>> castValue = (ArrayAttributeValue<AnnotationAttributeValue<?>>) value;
final List<Expression> arrayElements = new ArrayList<Expression>();
for (final AnnotationAttributeValue<?> v : castValue.getValue()) {
final MemberValuePair converted = convert(v, compilationUnitServices);
if (converted != null) {
arrayElements.add(converted.getValue());
}
}
return new MemberValuePair(value.getName().getSymbolName(), new ArrayInitializerExpr(arrayElements));
}
throw new UnsupportedOperationException("Unsupported attribute value '" + value.getName() + "' of type '" + value.getClass().getName() + "'");
}
use of org.springframework.roo.classpath.details.annotations.ClassAttributeValue in project spring-roo by spring-projects.
the class WsEndpointsMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting annotated class details
ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
AnnotationMetadata wsEndpointsAnnotation = cid.getAnnotation(ROO_WS_ENDPOINTS);
// Getting endpoints from annotation
AnnotationAttributeValue<?> currentEndpoints = wsEndpointsAnnotation.getAttribute("endpoints");
Validate.notNull(currentEndpoints, "ERROR: You must provide a valid endpoint list to be able to register endpoints.");
List<?> values = (List<?>) currentEndpoints.getValue();
Iterator<?> valuesIt = values.iterator();
Map<JavaType, JavaType> endpointsAndSeis = new TreeMap<JavaType, JavaType>();
Map<JavaType, JavaType> endpointsAndServices = new TreeMap<JavaType, JavaType>();
while (valuesIt.hasNext()) {
ClassAttributeValue endpointAttr = (ClassAttributeValue) valuesIt.next();
JavaType endpoint = endpointAttr.getValue();
// Getting endpoint details
ClassOrInterfaceTypeDetails endpointDetails = getTypeLocationService().getTypeDetails(endpoint);
// Use SeiImplMetadata to obtain necessary information about the endpoint
final String endpointMetadataId = SeiImplMetadata.createIdentifier(endpoint, PhysicalTypeIdentifier.getPath(endpointDetails.getDeclaredByMetadataId()));
final SeiImplMetadata endpointMetadata = (SeiImplMetadata) getMetadataService().get(endpointMetadataId);
JavaType sei = endpointMetadata.getSei();
JavaType service = endpointMetadata.getService();
// Saving the related sei
endpointsAndSeis.put(endpoint, sei);
// Saving the related service
endpointsAndServices.put(endpoint, service);
}
// Getting profile from annotation
String profile = "";
AnnotationAttributeValue<String> profileAttr = wsEndpointsAnnotation.getAttribute("profile");
if (profileAttr != null && StringUtils.isNotEmpty(profileAttr.getValue())) {
profile = profileAttr.getValue();
}
return new WsEndpointsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, endpointsAndSeis, endpointsAndServices, profile);
}
use of org.springframework.roo.classpath.details.annotations.ClassAttributeValue in project spring-roo by spring-projects.
the class LayerTypeMatcher method getTagValue.
@Override
public Object getTagValue(final MemberHoldingTypeDetails type) {
final AnnotationMetadata layerAnnotation = MemberFindingUtils.getAnnotationOfType(type.getAnnotations(), layerAnnotationType);
if (layerAnnotation == null || layerAnnotation.getAttribute(domainTypesAttribute) == null) {
return null;
}
final AnnotationAttributeValue<?> value = layerAnnotation.getAttribute(domainTypesAttribute);
final List<JavaType> domainTypes = new ArrayList<JavaType>();
if (value instanceof ClassAttributeValue) {
domainTypes.add(((ClassAttributeValue) value).getValue());
} else if (value instanceof ArrayAttributeValue<?>) {
final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value;
for (final AnnotationAttributeValue<?> val : castValue.getValue()) {
if (val instanceof ClassAttributeValue) {
domainTypes.add(((ClassAttributeValue) val).getValue());
}
}
}
return domainTypes;
}
Aggregations