annotationExpression = new MarkerAnnotationExpr(nameToUse);
}
else if (memberValuePairs.size() == 1
&& (memberValuePairs.get(0).getName() == null || "value"
.equals(memberValuePairs.get(0).getName()))) {
final Expression toUse = JavaParserUtils
.importExpressionIfRequired(
compilationUnitServices.getEnclosingTypeName(),
compilationUnitServices.getImports(),
memberValuePairs.get(0).getValue());
annotationExpression = new SingleMemberAnnotationExpr(nameToUse,
toUse);
}
else {
// We have a number of pairs being presented
annotationExpression = new NormalAnnotationExpr(nameToUse,
new ArrayList<MemberValuePair>());
}
// Add our AnnotationExpr to the actual annotations that will eventually
// be flushed through to the compilation unit
annotations.add(annotationExpression);
// Add member-value pairs to our AnnotationExpr
if (!memberValuePairs.isEmpty()) {
// Have to check here for cases where we need to change an existing
// MarkerAnnotationExpr to a NormalAnnotationExpr or
// SingleMemberAnnotationExpr
if (annotationExpression instanceof MarkerAnnotationExpr) {
final MarkerAnnotationExpr mae = (MarkerAnnotationExpr) annotationExpression;
annotations.remove(mae);
if (memberValuePairs.size() == 1
&& (memberValuePairs.get(0).getName() == null || "value"
.equals(memberValuePairs.get(0).getName()))) {
final Expression toUse = JavaParserUtils
.importExpressionIfRequired(compilationUnitServices
.getEnclosingTypeName(),
compilationUnitServices.getImports(),
memberValuePairs.get(0).getValue());
annotationExpression = new SingleMemberAnnotationExpr(
nameToUse, toUse);
annotations.add(annotationExpression);
}
else {
// We have a number of pairs being presented
annotationExpression = new NormalAnnotationExpr(nameToUse,
new ArrayList<MemberValuePair>());
annotations.add(annotationExpression);
}
}
if (annotationExpression instanceof SingleMemberAnnotationExpr) {
// Potentially upgrade this expression to a NormalAnnotationExpr
final SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotationExpression;
if (memberValuePairs.size() == 1
&& memberValuePairs.get(0).getName() == null
|| memberValuePairs.get(0).getName().equals("value")
|| memberValuePairs.get(0).getName().equals("")) {
// They specified only a single member-value pair, and it is
// the default anyway, so we need not do anything except
// update the value
final Expression toUse = JavaParserUtils
.importExpressionIfRequired(compilationUnitServices
.getEnclosingTypeName(),
compilationUnitServices.getImports(),
memberValuePairs.get(0).getValue());
smae.setMemberValue(toUse);
return;
}
// There is > 1 expression, or they have provided some sort of
// non-default value, so it's time to upgrade the expression
// (whilst retaining any potentially existing expression values)
final Expression existingValue = smae.getMemberValue();
annotationExpression = new NormalAnnotationExpr(smae.getName(),
new ArrayList<MemberValuePair>());
((NormalAnnotationExpr) annotationExpression).getPairs().add(
new MemberValuePair("value", existingValue));
}
Validate.isInstanceOf(
NormalAnnotationExpr.class,
annotationExpression,
"Attempting to add >1 annotation member-value pair requires an existing normal annotation expression");
final List<MemberValuePair> annotationPairs = ((NormalAnnotationExpr) annotationExpression)
.getPairs();
annotationPairs.clear();
for (final MemberValuePair pair : memberValuePairs) {
final Expression toUse = JavaParserUtils
.importExpressionIfRequired(
compilationUnitServices.getEnclosingTypeName(),
compilationUnitServices.getImports(),
pair.getValue());
pair.setValue(toUse);