@SuppressWarnings("unchecked")
private static MemberValuePair convert(
final AnnotationAttributeValue<?> value) {
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()
.getFullyQualifiedTypeName()));
}
else if (castValue.getValue().getAttributeNames().size() == 1) {
annotationExpr = new SingleMemberAnnotationExpr(
JavaParserUtils.getNameExpr(nestedAnnotation
.getAnnotationType()
.getFullyQualifiedTypeName()), convert(
nestedAnnotation.getAttribute(nestedAnnotation
.getAttributeNames().get(0)))
.getValue());
}
else {
final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
for (final JavaSymbolName attributeName : nestedAnnotation
.getAttributeNames()) {
memberValuePairs.add(convert(nestedAnnotation
.getAttribute(attributeName)));
}
annotationExpr = new NormalAnnotationExpr(
JavaParserUtils.getNameExpr(nestedAnnotation
.getAnnotationType()
.getFullyQualifiedTypeName()), memberValuePairs);
}
// 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.getNameExpr(castValue
.getFullyQualifiedTypeName());
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);
if (converted != null) {
arrayElements.add(converted.getValue());
}
}