Skip to content

Commit

Permalink
Add utility isTypeElementDefined method
Browse files Browse the repository at this point in the history
Signed-off-by: George Gastaldi <gegastaldi@gmail.com>
  • Loading branch information
sotty authored and gastaldi committed Sep 11, 2014
1 parent 70f87e1 commit 76d71cc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ public interface Annotation<O extends JavaType<O>> extends Internal, Origin<O>
Class<?>[] getClassArrayValue();

Class<?>[] getClassArrayValue(String name);

boolean isTypeElementDefined(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected void replace(org.eclipse.jdt.core.dom.Annotation oldNode, org.eclipse.
((MemberValuePair) oldNode.getParent()).setValue(newNode);
}
}

}

private static final String DEFAULT_VALUE = "value";
Expand Down Expand Up @@ -878,4 +879,17 @@ private Class<?> resolveTypeLiteral(TypeLiteral typeLiteral)
return null;
}
}

public boolean isTypeElementDefined( String name )
{
List<ValuePair> values = getValues();
for (ValuePair pair : values)
{
if ( pair.getName().equals( name ) )
{
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,23 @@ public void testNamedSingleStringArrayElement()
Assert.assertArrayEquals(values, ann.getStringArrayValue("aName"));
}

@Test
public void testCheckValueAreDefined() throws Exception
{
int size = target.getAnnotations().size();

target.addAnnotation(Test.class).setLiteralValue("expected", "RuntimeException.class")
.setLiteralValue("foo", "bar");

List<AnnotationSource<O>> annotations = target.getAnnotations();
assertEquals( size + 1, annotations.size() );

AnnotationSource<O> annotation = annotations.get(annotations.size() - 1);
assertEquals(Test.class.getSimpleName(), annotation.getName());
assertTrue( annotation.isTypeElementDefined( "foo" ) );
assertTrue( annotation.isTypeElementDefined( "expected" ) );
assertFalse( annotation.isTypeElementDefined( "missing" ) );
assertFalse( annotation.isTypeElementDefined( "fooo" ) );
}

}

0 comments on commit 76d71cc

Please sign in to comment.