From 76d71cc931c62a2eed57fa62a16de604830c4f39 Mon Sep 17 00:00:00 2001 From: sotty Date: Thu, 11 Sep 2014 16:23:52 -0300 Subject: [PATCH] Add utility isTypeElementDefined method Signed-off-by: George Gastaldi --- .../jboss/forge/roaster/model/Annotation.java | 2 ++ .../roaster/model/impl/AnnotationImpl.java | 14 ++++++++++++++ .../roaster/model/common/AnnotationTest.java | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/api/src/main/java/org/jboss/forge/roaster/model/Annotation.java b/api/src/main/java/org/jboss/forge/roaster/model/Annotation.java index 21dfc222..beb4b0f6 100644 --- a/api/src/main/java/org/jboss/forge/roaster/model/Annotation.java +++ b/api/src/main/java/org/jboss/forge/roaster/model/Annotation.java @@ -65,4 +65,6 @@ public interface Annotation> extends Internal, Origin Class[] getClassArrayValue(); Class[] getClassArrayValue(String name); + + boolean isTypeElementDefined(String name); } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AnnotationImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AnnotationImpl.java index c0c939a8..918ea81f 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AnnotationImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AnnotationImpl.java @@ -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"; @@ -878,4 +879,17 @@ private Class resolveTypeLiteral(TypeLiteral typeLiteral) return null; } } + + public boolean isTypeElementDefined( String name ) + { + List values = getValues(); + for (ValuePair pair : values) + { + if ( pair.getName().equals( name ) ) + { + return true; + } + } + return false; + } } diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/common/AnnotationTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/common/AnnotationTest.java index 0a1768c9..1fa526bb 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/common/AnnotationTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/common/AnnotationTest.java @@ -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> annotations = target.getAnnotations(); + assertEquals( size + 1, annotations.size() ); + + AnnotationSource 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" ) ); + } + }