use of org.springframework.roo.classpath.details.annotations.StringAttributeValue 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());
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class SecurityOperationsImpl method getRooSecurityAuthorizationsAnnotation.
/**
* This method provides {@link RooSecurityAuthorization} annotation with all the necessary
* attributes
*
* @param method Method to add the annotation
* @param lstParamTypes Parameter types of the method to add the annotation
* @param roles Roles to apply by the filter
* @param usernames Usernames apply by the filter
* @return the annotation created
*/
private AnnotationMetadataBuilder getRooSecurityAuthorizationsAnnotation(final String method, final List<AnnotationAttributeValue<?>> lstParamTypes, final String roles, final String usernames) {
final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
attributes.add(new StringAttributeValue(new JavaSymbolName("method"), method));
ArrayAttributeValue<AnnotationAttributeValue<?>> newParameters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("parameters"), lstParamTypes);
attributes.add(newParameters);
if (roles != null) {
attributes.add(new StringAttributeValue(new JavaSymbolName("roles"), roles));
}
if (usernames != null) {
attributes.add(new StringAttributeValue(new JavaSymbolName("usernames"), usernames));
}
return new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATION, attributes);
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class EqualsOperationsImpl method addEqualsAndHashCodeMethods.
public void addEqualsAndHashCodeMethods(final JavaType javaType, final boolean appendSuper, final Set<String> excludeFields) {
// Add @RooEquals annotation to class if not yet present
final ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(javaType);
if (cid == null || cid.getTypeAnnotation(ROO_EQUALS) != null) {
return;
}
final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(ROO_EQUALS);
if (appendSuper) {
annotationBuilder.addBooleanAttribute("appendSuper", appendSuper);
}
if (!CollectionUtils.isEmpty(excludeFields)) {
final List<StringAttributeValue> attributes = new ArrayList<StringAttributeValue>();
for (final String excludeField : excludeFields) {
attributes.add(new StringAttributeValue(new JavaSymbolName("value"), excludeField));
}
annotationBuilder.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), attributes));
}
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(cid);
cidBuilder.addAnnotation(annotationBuilder.build());
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class JavaParserAnnotationMetadataBuilder method convert.
private AnnotationAttributeValue<?> convert(JavaSymbolName annotationName, final Expression expression, final CompilationUnitServices compilationUnitServices) {
if (annotationName == null) {
annotationName = new JavaSymbolName("__ARRAY_ELEMENT__");
}
if (expression instanceof AnnotationExpr) {
final AnnotationExpr annotationExpr = (AnnotationExpr) expression;
final AnnotationMetadata value = getInstance(annotationExpr, compilationUnitServices).build();
return new NestedAnnotationAttributeValue(annotationName, value);
}
if (expression instanceof BooleanLiteralExpr) {
final boolean value = ((BooleanLiteralExpr) expression).getValue();
return new BooleanAttributeValue(annotationName, value);
}
if (expression instanceof CharLiteralExpr) {
final String value = ((CharLiteralExpr) expression).getValue();
Validate.isTrue(value.length() == 1, "Expected a char expression, but instead received '%s' for attribute '%s'", value, annotationName);
final char c = value.charAt(0);
return new CharAttributeValue(annotationName, c);
}
if (expression instanceof LongLiteralExpr) {
String value = ((LongLiteralExpr) expression).getValue();
Validate.isTrue(value.toUpperCase().endsWith("L"), "Expected long literal expression '%s' to end in 'l' or 'L'", value);
value = value.substring(0, value.length() - 1);
final long l = new Long(value);
return new LongAttributeValue(annotationName, l);
}
if (expression instanceof IntegerLiteralExpr) {
final String value = ((IntegerLiteralExpr) expression).getValue();
final int i = new Integer(value);
return new IntegerAttributeValue(annotationName, i);
}
if (expression instanceof DoubleLiteralExpr) {
String value = ((DoubleLiteralExpr) expression).getValue();
boolean floatingPrecisionOnly = false;
if (value.toUpperCase().endsWith("F")) {
value = value.substring(0, value.length() - 1);
floatingPrecisionOnly = true;
}
if (value.toUpperCase().endsWith("D")) {
value = value.substring(0, value.length() - 1);
}
final double d = new Double(value);
return new DoubleAttributeValue(annotationName, d, floatingPrecisionOnly);
}
if (expression instanceof BinaryExpr) {
String result = "";
BinaryExpr current = (BinaryExpr) expression;
while (current != null) {
String right = "";
if (current.getRight() instanceof StringLiteralExpr) {
right = ((StringLiteralExpr) current.getRight()).getValue();
} else if (current.getRight() instanceof NameExpr) {
right = ((NameExpr) current.getRight()).getName();
}
result = right + result;
if (current.getLeft() instanceof StringLiteralExpr) {
final String left = ((StringLiteralExpr) current.getLeft()).getValue();
result = left + result;
}
if (current.getLeft() instanceof BinaryExpr) {
current = (BinaryExpr) current.getLeft();
} else {
current = null;
}
}
return new StringAttributeValue(annotationName, result);
}
if (expression instanceof StringLiteralExpr) {
final String value = ((StringLiteralExpr) expression).getValue();
return new StringAttributeValue(annotationName, value);
}
if (expression instanceof FieldAccessExpr) {
final FieldAccessExpr field = (FieldAccessExpr) expression;
final String fieldName = field.getField();
// Determine the type
final Expression scope = field.getScope();
NameExpr nameToFind = null;
if (scope instanceof FieldAccessExpr) {
final FieldAccessExpr fScope = (FieldAccessExpr) scope;
nameToFind = JavaParserUtils.getNameExpr(fScope.toString());
} else if (scope instanceof NameExpr) {
nameToFind = (NameExpr) scope;
} else {
throw new UnsupportedOperationException("A FieldAccessExpr for '" + field.getScope() + "' should return a NameExpr or FieldAccessExpr (was " + field.getScope().getClass().getName() + ")");
}
final JavaType fieldType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(fieldName));
return new EnumAttributeValue(annotationName, enumDetails);
}
if (expression instanceof NameExpr) {
final NameExpr field = (NameExpr) expression;
final String name = field.getName();
// As we have no way of finding out the real type
final JavaType fieldType = new JavaType("unknown.Object");
final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(name));
return new EnumAttributeValue(annotationName, enumDetails);
}
if (expression instanceof ClassExpr) {
final ClassExpr clazz = (ClassExpr) expression;
final Type nameToFind = clazz.getType();
final JavaType javaType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
return new ClassAttributeValue(annotationName, javaType);
}
if (expression instanceof ArrayInitializerExpr) {
final ArrayInitializerExpr castExp = (ArrayInitializerExpr) expression;
final List<AnnotationAttributeValue<?>> arrayElements = new ArrayList<AnnotationAttributeValue<?>>();
for (final Expression e : castExp.getValues()) {
arrayElements.add(convert(null, e, compilationUnitServices));
}
return new ArrayAttributeValue<AnnotationAttributeValue<?>>(annotationName, arrayElements);
}
if (expression instanceof UnaryExpr) {
final UnaryExpr castExp = (UnaryExpr) expression;
if (castExp.getOperator() == Operator.negative) {
String value = castExp.toString();
value = value.toUpperCase().endsWith("L") ? value.substring(0, value.length() - 1) : value;
final long l = new Long(value);
return new LongAttributeValue(annotationName, l);
} else {
throw new UnsupportedOperationException("Only negative operator in UnaryExpr is supported");
}
}
throw new UnsupportedOperationException("Unable to parse annotation attribute '" + annotationName + "' due to unsupported annotation expression '" + expression.getClass().getName() + "'");
}
use of org.springframework.roo.classpath.details.annotations.StringAttributeValue in project spring-roo by spring-projects.
the class WsOperationsImpl method getWsClientAnnotation.
/**
* This method provides @RooWsClient annotation with all the necessary attributes
*
* @param endpoint
* @param targetNamespace
* @param bindingType
* @return
*/
private AnnotationMetadataBuilder getWsClientAnnotation(final String endpoint, final String targetNamespace, final EnumDetails bindingType) {
final List<AnnotationAttributeValue<?>> wsClientAttributes = new ArrayList<AnnotationAttributeValue<?>>();
wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("endpoint"), endpoint));
wsClientAttributes.add(new StringAttributeValue(new JavaSymbolName("targetNamespace"), targetNamespace));
wsClientAttributes.add(new EnumAttributeValue(new JavaSymbolName("binding"), bindingType));
return new AnnotationMetadataBuilder(RooJavaType.ROO_WS_CLIENT, wsClientAttributes);
}
Aggregations