use of com.github.antlrjavaparser.api.expr.BinaryExpr 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() + "'");
}
Aggregations