Skip to content

Commit

Permalink
ROASTER-20: Add support for annotations that contain an array of sub-…
Browse files Browse the repository at this point in the history
…annotations.
  • Loading branch information
woodcoder authored and gastaldi committed Jul 10, 2014
1 parent 1102e5e commit d107d57
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public interface Annotation<O extends JavaType<O>> extends Internal, Origin<O>

Annotation<O> getAnnotationValue(String name);

Annotation<O>[] getAnnotationArrayValue();

Annotation<O>[] getAnnotationArrayValue(String name);

Class<?> getClassValue();

Class<?> getClassValue(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,34 @@ public AnnotationSource<O> getAnnotationValue(String name)
return null;
}

@Override
public AnnotationSource<O>[] getAnnotationArrayValue()
{
return getAnnotationArrayValue(DEFAULT_VALUE);
}

@Override
public AnnotationSource<O>[] getAnnotationArrayValue(String name)
{
final Expression expr = getElementValueExpression(name);
if (expr instanceof ArrayInitializer)
{
final List<AnnotationSource<O>> results = new ArrayList<AnnotationSource<O>>();
@SuppressWarnings("unchecked")
final List<Expression> arrayElements = ((ArrayInitializer) expr).expressions();
for (Expression arrayElement : arrayElements)
{
final AnnotationSource<O> instance = new Nested(this, arrayElement);
results.add(instance);
}
@SuppressWarnings("unchecked")
final AnnotationSource<O>[] result = new AnnotationSource[results.size()];
return results.toArray(result);
}
return null;
}


@Override
public <E extends Enum<E>> E[] getEnumArrayValue(Class<E> type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void resetTests()
public void testParseEnumValueStaticImport() throws Exception
{
List<AnnotationSource<JavaClassSource>> annotations = getTarget().getAnnotations();
AnnotationSource<JavaClassSource> annotation = annotations.get(annotations.size() - 2);
AnnotationSource<JavaClassSource> annotation = annotations.get(3);
MockEnumType enumValue = annotation.getEnumValue(MockEnumType.class);
assertEquals(MockEnumType.FOO, enumValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void reset()
public void testParseAnnotation() throws Exception
{
List<AnnotationSource<O>> annotations = target.getAnnotations();
assertEquals(5, annotations.size());
assertEquals(6, annotations.size());
assertEquals("deprecation", annotations.get(1).getStringValue());
assertEquals("deprecation", annotations.get(1).getStringValue("value"));
assertEquals("value", annotations.get(1).getValues().get(0).getName());
Expand All @@ -63,8 +63,11 @@ public void testParseAnnotation() throws Exception
assertEquals("unchecked", annotations.get(2).getStringValue());
assertEquals("value", annotations.get(2).getValues().get(0).getName());
assertEquals("unchecked", annotations.get(2).getValues().get(0).getStringValue());
assertEquals("MockAnnotation", annotations.get(3).getName());
assertEquals("MockNestingAnnotation", annotations.get(4).getName());
assertEquals("MockNestedAnnotation", annotations.get(4).getAnnotationValue().getName());
assertEquals("MockContainerAnnotation", annotations.get(5).getName());
assertEquals("MockContainedAnnotation", annotations.get(5).getAnnotationArrayValue()[0].getName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
@MockContainerAnnotation({
@MockContainedAnnotation(0)
})
public class MockAnnotatedClass
{
private String field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public enum MockAnnotatedEnumConstant
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
@MockContainerAnnotation({
@MockContainedAnnotation(0)
})
SINGLE_CONSTANT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public class MockAnnotatedField
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
@MockContainerAnnotation({
@MockContainedAnnotation(0)
})
private String field;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class MockAnnotatedMethod
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
@MockContainerAnnotation({
@MockContainedAnnotation(0)
})
public MockAnnotatedMethod()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public void mockAnnotatedMethod(
@SuppressWarnings(value = "unchecked")
@MockAnnotation(FOO)
@MockNestingAnnotation(@MockNestedAnnotation)
@MockContainerAnnotation({
@MockContainedAnnotation(0)
})
String param1, int param2)
{
}
Expand Down

0 comments on commit d107d57

Please sign in to comment.