[weld-commits] Weld SVN: r6218 - in extensions/trunk/src: test/java/org/jboss/weld/extensions/test/util and 1 other directory.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Sat May 8 15:18:50 EDT 2010
Author: pete.muir at jboss.org
Date: 2010-05-08 15:18:50 -0400 (Sat, 08 May 2010)
New Revision: 6218
Added:
extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animal.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animals.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/AnnotationInspectorTest.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Cat.java
Log:
WELDX-95
Added: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java (rev 0)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java 2010-05-08 19:18:50 UTC (rev 6218)
@@ -0,0 +1,67 @@
+package org.jboss.weld.extensions.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+
+import javax.enterprise.inject.Stereotype;
+import javax.enterprise.inject.spi.BeanManager;
+
+public class AnnotationInspector
+{
+
+ private AnnotationInspector() {}
+
+ /**
+ * Discover if a AnnotatedElement <b>element</b> has been annotated with <b>annotationType</b>. This
+ * also discovers annotations defined through a @{@link Stereotype} and the CDI SPI.
+ *
+ * @param element The element to inspect.
+ * @param annotationType
+ * @param metaAnnotation Whether the annotation may be used as a meta-annotation or not
+ *
+ * @return true if annotation is present either on the method itself. Returns false if the annotation
+ * is not present
+ * @throws IllegalArgumentException if element or annotationType is null
+ */
+ public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType, boolean metaAnnotation, BeanManager beanManager)
+ {
+ return getAnnotation(element, annotationType, metaAnnotation, beanManager) != null;
+ }
+
+ /**
+ * Inspect AnnoatedElement <b>element</b> for a specific <b>type</b> of annotation. This
+ * also discovers annotations defined through a @ {@link Stereotype} and the CDI SPI.
+ *
+ * @param element The element to inspect
+ * @param annotationType The annotation type to check for
+ * @param metaAnnotation Whether the annotation may be used as a meta-annotation or not
+ *
+ * @return The annotation instance found on this method or null if no matching annotation was found.
+ * @throws IllegalArgumentException if element or annotationType is null
+ */
+ public static <A extends Annotation> A getAnnotation(AnnotatedElement element, final Class<A> annotationType, boolean metaAnnotation, BeanManager beanManager)
+ {
+ if (metaAnnotation)
+ {
+ for (Annotation annotation : element.getAnnotations())
+ {
+ if (beanManager.isStereotype(annotation.annotationType()))
+ {
+ for (Annotation stereotypedAnnotation : beanManager.getStereotypeDefinition(annotation.annotationType()))
+ {
+ if (stereotypedAnnotation.annotationType().equals(annotationType))
+ {
+ return annotationType.cast(stereotypedAnnotation);
+ }
+ }
+ }
+ }
+ return null;
+ }
+ else
+ {
+ return element.getAnnotation(annotationType);
+ }
+ }
+
+}
Property changes on: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animal.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animal.java (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animal.java 2010-05-08 19:18:50 UTC (rev 6218)
@@ -0,0 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.util;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface Animal
+{
+
+ String species();
+
+}
Property changes on: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animal.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animals.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animals.java (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animals.java 2010-05-08 19:18:50 UTC (rev 6218)
@@ -0,0 +1,18 @@
+package org.jboss.weld.extensions.test.util;
+
+public class Animals
+{
+
+ @Animal(species="Dog")
+ public void dog()
+ {
+
+ }
+
+ @Cat
+ public void cat()
+ {
+
+ }
+
+}
Property changes on: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Animals.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/AnnotationInspectorTest.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/AnnotationInspectorTest.java (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/AnnotationInspectorTest.java 2010-05-08 19:18:50 UTC (rev 6218)
@@ -0,0 +1,26 @@
+package org.jboss.weld.extensions.test.util;
+
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.weld.extensions.util.AnnotationInspector;
+import org.jboss.weld.test.AbstractWeldTest;
+import org.testng.annotations.Test;
+
+ at Artifact
+public class AnnotationInspectorTest extends AbstractWeldTest
+{
+
+ @Test
+ public void testAnnotationOnElement() throws Exception
+ {
+ assert AnnotationInspector.isAnnotationPresent(Animals.class.getMethod("dog"), Animal.class, false, getCurrentManager());
+ assert AnnotationInspector.getAnnotation(Animals.class.getMethod("dog"), Animal.class, false, getCurrentManager()).species().equals("Dog");
+ }
+
+ @Test
+ public void testAnnotationOnStereotype() throws Exception
+ {
+ assert AnnotationInspector.isAnnotationPresent(Animals.class.getMethod("cat"), Animal.class, true, getCurrentManager());
+ assert AnnotationInspector.getAnnotation(Animals.class.getMethod("cat"), Animal.class, true, getCurrentManager()).species().equals("Cat");
+ }
+
+}
Property changes on: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/AnnotationInspectorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Cat.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Cat.java (rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Cat.java 2010-05-08 19:18:50 UTC (rev 6218)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.util;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.enterprise.inject.Stereotype;
+
+ at Retention(RetentionPolicy.RUNTIME)
+ at Stereotype
+ at Target(ElementType.METHOD)
+ at Animal(species="Cat")
+public @interface Cat
+{
+
+}
Property changes on: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/util/Cat.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
More information about the weld-commits
mailing list