use of org.springframework.roo.classpath.details.annotations.BooleanAttributeValue 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.BooleanAttributeValue in project spring-roo by spring-projects.
the class AutoPopulationUtils method populate.
/**
* Introspects the target {@link Object} for its declared fields, locating
* all {@link AutoPopulate} annotated fields. For each field, an attempt
* will be made to locate the value from the passed
* {@link AnnotationMetadata}. The annotation value will be converted into
* the required field type, or silently skipped if this is not possible (eg
* the user edited source code and made a formatting error). As such it is
* important that the caller.
*
* @param target to put values into (mandatory, cannot be null)
* @param annotation to obtain values from (can be null, for convenience of
* the caller)
*/
public static void populate(final Object target, final AnnotationMetadata annotation) {
Validate.notNull(target, "Target required");
if (annotation == null) {
return;
}
List<Field> fields = cachedIntrospections.get(target.getClass());
if (fields == null) {
// Go and cache them
fields = new ArrayList<Field>();
for (final Field field : target.getClass().getDeclaredFields()) {
// Determine if this field even contains the necessary
// annotation
final AutoPopulate ap = field.getAnnotation(AutoPopulate.class);
if (ap == null) {
continue;
}
// Determine attribute name we should be looking for in the
// annotation
String attribute = ap.value();
if ("".equals(ap.value())) {
attribute = field.getName();
}
// Ensure field is accessible
if (!field.isAccessible()) {
field.setAccessible(true);
}
final JavaSymbolName attributeName = new JavaSymbolName(attribute);
// Store the info
fields.add(field);
attributeNameForEachField.put(field, attributeName);
}
cachedIntrospections.put(target.getClass(), fields);
}
for (final Field field : fields) {
// Lookup whether this attribute name was provided
final JavaSymbolName attributeName = attributeNameForEachField.get(field);
if (attributeName == null) {
throw new IllegalStateException("Expected cached attribute name lookup");
}
if (annotation.getAttributeNames().contains(attributeName)) {
// Get the value
final AnnotationAttributeValue<?> value = annotation.getAttribute(attributeName);
// Assign the value to the target object
try {
final Class<?> fieldType = field.getType();
if (value instanceof BooleanAttributeValue && (fieldType.equals(Boolean.class) || fieldType.equals(Boolean.TYPE))) {
field.set(target, value.getValue());
} else if (value instanceof CharAttributeValue && (fieldType.equals(Character.class) || fieldType.equals(Character.TYPE))) {
field.set(target, value.getValue());
} else if (value instanceof ClassAttributeValue && fieldType.equals(JavaType.class)) {
field.set(target, value.getValue());
} else if (value instanceof DoubleAttributeValue && (fieldType.equals(Double.class) || fieldType.equals(Double.TYPE))) {
field.set(target, value.getValue());
} else if (value instanceof EnumAttributeValue && Enum.class.isAssignableFrom(fieldType)) {
field.set(target, ((EnumAttributeValue) value).getAsEnum(target));
} else if (value instanceof IntegerAttributeValue && (fieldType.equals(Integer.class) || fieldType.equals(Integer.TYPE))) {
field.set(target, value.getValue());
} else if (value instanceof LongAttributeValue && (fieldType.equals(Long.class) || fieldType.equals(Long.TYPE))) {
field.set(target, value.getValue());
} else if (value instanceof StringAttributeValue && fieldType.equals(String.class)) {
field.set(target, value.getValue());
} else if (value instanceof StringAttributeValue && fieldType.getComponentType() != null && fieldType.getComponentType().equals(String.class)) {
// ROO-618
final Object newValue = Array.newInstance(String.class, 1);
Array.set(newValue, 0, value.getValue());
field.set(target, newValue);
} else if (value instanceof ArrayAttributeValue<?> && fieldType.isArray()) {
// The field is a string array, the attribute is an
// array, so let's hope it's a string array
final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value;
final List<String> result = new ArrayList<String>();
final List<JavaType> result1 = new ArrayList<JavaType>();
for (final AnnotationAttributeValue<?> val : castValue.getValue()) {
// For now we'll only support arrays of strings
if (fieldType.getComponentType().equals(String.class) && val instanceof StringAttributeValue) {
final StringAttributeValue stringValue = (StringAttributeValue) val;
result.add(stringValue.getValue());
} else if (fieldType.getComponentType().equals(JavaType.class) && val instanceof ClassAttributeValue) {
final ClassAttributeValue classValue = (ClassAttributeValue) val;
result1.add(classValue.getValue());
}
}
if (result.size() > 0) {
// We had at least one string array, so we change
// the field
field.set(target, result.toArray(new String[] {}));
}
if (result1.size() > 0) {
// We had at least one string array, so we change
// the field
field.set(target, result1.toArray(new JavaType[] {}));
}
}
// If not in the above list, it's unsupported so we silently
// skip
} catch (final Throwable ignoreFailures) {
}
}
}
}
use of org.springframework.roo.classpath.details.annotations.BooleanAttributeValue in project spring-roo by spring-projects.
the class FieldDetails method decorateAnnotationsList.
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
Validate.notNull(annotations);
if (notNull) {
annotations.add(new AnnotationMetadataBuilder(NOT_NULL));
}
if (nullRequired) {
annotations.add(new AnnotationMetadataBuilder(NULL));
}
AnnotationMetadataBuilder columnBuilder = null;
if (column != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("name"), column));
columnBuilder = new AnnotationMetadataBuilder(COLUMN, attrs);
}
if (unique) {
if (columnBuilder != null) {
columnBuilder.addBooleanAttribute("unique", true);
} else {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new BooleanAttributeValue(new JavaSymbolName("unique"), true));
columnBuilder = new AnnotationMetadataBuilder(COLUMN, attrs);
}
}
if (value != null) {
final List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>();
attrs.add(new StringAttributeValue(new JavaSymbolName("value"), value));
annotations.add(new AnnotationMetadataBuilder(VALUE, attrs));
}
if (fieldName.getSymbolName().equals("created")) {
if (columnBuilder == null) {
columnBuilder = new AnnotationMetadataBuilder(COLUMN);
}
columnBuilder.addBooleanAttribute("updatable", false);
}
if (columnBuilder != null) {
annotations.add(columnBuilder);
}
}
use of org.springframework.roo.classpath.details.annotations.BooleanAttributeValue in project spring-roo by spring-projects.
the class AnnotationMetadataUtils method computeAttributeValue.
private static String computeAttributeValue(final AnnotationAttributeValue<?> value, final ImportRegistrationResolver resolver) {
String attributeValue = null;
if (value instanceof BooleanAttributeValue) {
attributeValue = ((BooleanAttributeValue) value).getValue().toString();
} else if (value instanceof CharAttributeValue) {
attributeValue = "'" + ((CharAttributeValue) value).getValue().toString() + "'";
} else if (value instanceof ClassAttributeValue) {
final JavaType clazz = ((ClassAttributeValue) value).getValue();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
attributeValue = clazz.getFullyQualifiedTypeName() + ".class";
} else {
attributeValue = clazz.getSimpleTypeName() + ".class";
}
} else if (value instanceof DoubleAttributeValue) {
final DoubleAttributeValue dbl = (DoubleAttributeValue) value;
if (dbl.isFloatingPrecisionOnly()) {
attributeValue = dbl.getValue().toString() + "F";
} else {
attributeValue = dbl.getValue().toString() + "D";
}
} else if (value instanceof EnumAttributeValue) {
final EnumDetails enumDetails = ((EnumAttributeValue) value).getValue();
final JavaType clazz = enumDetails.getType();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(clazz)) {
attributeValue = clazz.getFullyQualifiedTypeName() + "." + enumDetails.getField().getSymbolName();
} else {
attributeValue = clazz.getSimpleTypeName() + "." + enumDetails.getField().getSymbolName();
}
} else if (value instanceof IntegerAttributeValue) {
attributeValue = ((IntegerAttributeValue) value).getValue().toString();
} else if (value instanceof LongAttributeValue) {
attributeValue = ((LongAttributeValue) value).getValue().toString() + "L";
} else if (value instanceof StringAttributeValue) {
attributeValue = "\"" + ((StringAttributeValue) value).getValue() + "\"";
} else if (value instanceof NestedAnnotationAttributeValue) {
final AnnotationMetadata annotationMetadata = ((NestedAnnotationAttributeValue) value).getValue();
final StringBuilder data = new StringBuilder("@");
final JavaType annotationType = annotationMetadata.getAnnotationType();
if (resolver == null || resolver.isFullyQualifiedFormRequiredAfterAutoImport(annotationType)) {
data.append(annotationType.getFullyQualifiedTypeName());
} else {
data.append(annotationType.getSimpleTypeName());
}
if (!annotationMetadata.getAttributeNames().isEmpty()) {
data.append("(");
int i = 0;
for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) {
i++;
if (i > 1) {
data.append(", ");
}
data.append(attributeName.getSymbolName()).append(" = ");
data.append(computeAttributeValue(annotationMetadata.getAttribute(attributeName), resolver));
}
data.append(")");
}
attributeValue = data.toString();
} else if (value instanceof ArrayAttributeValue<?>) {
final ArrayAttributeValue<?> array = (ArrayAttributeValue<?>) value;
final StringBuilder data = new StringBuilder("{ ");
int i = 0;
for (final AnnotationAttributeValue<?> val : array.getValue()) {
i++;
if (i > 1) {
data.append(", ");
}
data.append(computeAttributeValue(val, resolver));
}
data.append(" }");
attributeValue = data.toString();
}
return attributeValue;
}
use of org.springframework.roo.classpath.details.annotations.BooleanAttributeValue in project spring-roo by spring-projects.
the class ReferenceField method decorateAnnotationsList.
@Override
public void decorateAnnotationsList(final List<AnnotationMetadataBuilder> annotations) {
super.decorateAnnotationsList(annotations);
final List<AnnotationAttributeValue<?>> attributes = new ArrayList<AnnotationAttributeValue<?>>();
// Add cascade if option exists
if (cascadeType != null) {
List<EnumAttributeValue> cascadeValues = new ArrayList<EnumAttributeValue>();
for (Cascade type : cascadeType) {
cascadeValues.add(new EnumAttributeValue(new JavaSymbolName("cascade"), new EnumDetails(CASCADE_TYPE, new JavaSymbolName(type.name()))));
}
attributes.add(new ArrayAttributeValue<EnumAttributeValue>(new JavaSymbolName("cascade"), cascadeValues));
}
// Add orphanRemoval if option exists
if (getOrphanRemoval() != null) {
attributes.add(new BooleanAttributeValue(new JavaSymbolName("orphanRemoval"), getOrphanRemoval().booleanValue()));
}
if (fetch != null) {
JavaSymbolName value = new JavaSymbolName("EAGER");
if (fetch == Fetch.LAZY) {
value = new JavaSymbolName("LAZY");
}
attributes.add(new EnumAttributeValue(new JavaSymbolName("fetch"), new EnumDetails(FETCH_TYPE, value)));
}
if (mappedBy != null) {
attributes.add(new StringAttributeValue(new JavaSymbolName("mappedBy"), mappedBy.getSymbolName()));
}
switch(cardinality) {
case ONE_TO_MANY:
annotations.add(new AnnotationMetadataBuilder(ONE_TO_MANY, attributes));
break;
case MANY_TO_MANY:
annotations.add(new AnnotationMetadataBuilder(MANY_TO_MANY, attributes));
break;
case ONE_TO_ONE:
annotations.add(new AnnotationMetadataBuilder(ONE_TO_ONE, attributes));
break;
default:
annotations.add(new AnnotationMetadataBuilder(MANY_TO_ONE, attributes));
break;
}
// Add additional annotations (if any)
if (additionaAnnotations != null) {
annotations.addAll(additionaAnnotations);
}
}
Aggregations