Skip to content

Commit

Permalink
ROASTER-124: Support multiple value pairs in setLiteralValue
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jun 12, 2017
1 parent 1e51335 commit fe838b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public AnnotationSource<O> setName(final String className)
return this;
}

@SuppressWarnings("unchecked")
@Override
public AnnotationSource<O> setLiteralValue(final String value)
{
Expand All @@ -317,15 +318,33 @@ public AnnotationSource<O> setLiteralValue(final String value)

if (isSingleValue())
{
SingleMemberAnnotation sa = (SingleMemberAnnotation) annotation;

String stub = "@" + getName() + "(" + value + ") public class Stub { }";
JavaClass<?> temp = Roaster.parse(JavaClass.class, stub);

SingleMemberAnnotation anno = (SingleMemberAnnotation) temp.getAnnotations().get(0).getInternal();
Object internal = temp.getAnnotations().get(0).getInternal();
if (internal instanceof SingleMemberAnnotation)
{
SingleMemberAnnotation sa = (SingleMemberAnnotation) annotation;
Expression expression = ((SingleMemberAnnotation) internal).getValue();
sa.setValue((Expression) ASTNode.copySubtree(ast, expression));
}
else if (internal instanceof NormalAnnotation)
{
NormalAnnotation anno = (NormalAnnotation) internal;
convertTo(AnnotationType.NORMAL);
NormalAnnotation na = (NormalAnnotation) annotation;

Expression expression = anno.getValue();
sa.setValue((Expression) ASTNode.copySubtree(ast, expression));
for (MemberValuePair mvp : (List<MemberValuePair>) anno.values())
{
na.values().add(ASTNode.copySubtree(annotation.getAST(), mvp));
}
}
else
{
throw new IllegalArgumentException(
"Type " + internal.getClass().getName() + " cannot be handled in this method");
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,15 @@ public void testGetLiteralValueReturnsFQNInClassAnnotationValue()
Assert.assertEquals(IllegalStateException.class.getName(), annotation.getStringValue("expected"));
}

@Test
public void testSetLiteralValueStructured()
{
String annotationValue = "clazz = String.class, method = \"startAnalyse\"";
AnnotationSource<O> ann = target.addAnnotation(Test.class);
ann.setLiteralValue(annotationValue);
Assert.assertEquals("startAnalyse", ann.getStringValue("method"));
ann.setLiteralValue(annotationValue);
Assert.assertEquals(String.class, ann.getClassValue("clazz"));
}

}

0 comments on commit fe838b3

Please sign in to comment.