[weld-issues] [JBoss JIRA] Closed: (WELDX-95) Need common method of scanning for annotations (static or dynamic)
Pete Muir (JIRA)
jira-events at lists.jboss.org
Sat May 8 15:20:05 EDT 2010
[ https://jira.jboss.org/jira/browse/WELDX-95?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Pete Muir closed WELDX-95.
--------------------------
Resolution: Done
Assignee: Pete Muir
Initial support added. We can't really take this much further without extending further the ability to identify the annotatedType from which an instance comes (WELDX-96)
> Need common method of scanning for annotations (static or dynamic)
> ------------------------------------------------------------------
>
> Key: WELDX-95
> URL: https://jira.jboss.org/jira/browse/WELDX-95
> Project: Weld Extensions
> Issue Type: Feature Request
> Reporter: Lincoln Baxter III
> Assignee: Pete Muir
> Fix For: 1.0.0.Alpha2
>
>
> There needs to be a common way of scanning classes and methods for annotation information: A Utility class for common @{@link Annotation} scanning operations. Enables scanning classes and methods for specific annotation types, and retrieving those annotation instances.
> Here is the solution currently being used in Faces Module -- it still needs a call to the BeanManager.getStereotypeDefinition(...) in order to be fully portable.
> ----------------------
> package org.jboss.weld.extensions.util;
> import java.lang.annotation.Annotation;
> import java.lang.reflect.Method;
> import javax.enterprise.inject.Stereotype;
> /**
> * Utility class for common @{@link Annotation} scanning operations. Enables
> * scanning classes and methods for specific annotation types, and retrieving
> * those annotation instances.
> * <p>
> *
> * @author <a href="mailto:lincolnbaxter at gmail.com>Lincoln Baxter, III</a>
> *
> */
> public class Annotations
> {
> /**
> * Discover if a Method <b>m</b> has been annotated with <b>type</b>. This
> * also discovers annotations defined through a @{@link Stereotype}.
> *
> * @param m The method to inspect.
> * @param type The targeted annotation class
> *
> * @return True if annotation is present either on the method itself, or on
> * the declaring class of the method. Returns false if the annotation
> * is not present.
> */
> public static boolean has(final Method m, final Class<? extends Annotation> type)
> {
> boolean result = false;
> if (m.isAnnotationPresent(type))
> {
> result = true;
> }
> else
> {
> for (Annotation a : m.getAnnotations())
> {
> if (a.annotationType().isAnnotationPresent(type))
> {
> result = true;
> }
> }
> }
> if (result == false)
> {
> result = has(m.getDeclaringClass(), type);
> }
> return result;
> }
> /**
> * Discover if a Class <b>c</b> has been annotated with <b>type</b>. This
> * also discovers annotations defined through a @{@link Stereotype}.
> *
> * @param c The class to inspect.
> * @param type The targeted annotation class
> *
> * @return True if annotation is present either on class, false if the
> * annotation is not present.
> */
> public static boolean has(final Class<?> c, final Class<? extends Annotation> type)
> {
> boolean result = false;
> if (c.isAnnotationPresent(type))
> {
> result = true;
> }
> else
> {
> for (Annotation a : c.getAnnotations())
> {
> if (a.annotationType().isAnnotationPresent(type))
> {
> result = true;
> }
> }
> }
> return result;
> }
> /**
> * Inspect method <b>m</b> for a specific <b>type</b> of annotation. This
> * also discovers annotations defined through a @ {@link Stereotype}.
> *
> * @param m The method to inspect.
> * @param type The targeted annotation class
> *
> * @return The annotation instance found on this method or enclosing class,
> * or null if no matching annotation was found.
> */
> public static <A extends Annotation> A get(final Method m, final Class<A> type)
> {
> A result = m.getAnnotation(type);
> if (result == null)
> {
> for (Annotation a : m.getAnnotations())
> {
> if (a.annotationType().isAnnotationPresent(type))
> {
> result = a.annotationType().getAnnotation(type);
> }
> }
> }
> if (result == null)
> {
> result = get(m.getDeclaringClass(), type);
> }
> return result;
> }
> /**
> * Inspect class <b>c</b> for a specific <b>type</b> of annotation. This also
> * discovers annotations defined through a @ {@link Stereotype}.
> *
> * @param c The class to inspect.
> * @param type The targeted annotation class
> *
> * @return The annotation instance found on this class, or null if no
> * matching annotation was found.
> */
> public static <A extends Annotation> A get(final Class<?> c, final Class<A> type)
> {
> A result = c.getAnnotation(type);
> if (result == null)
> {
> for (Annotation a : c.getAnnotations())
> {
> if (a.annotationType().isAnnotationPresent(type))
> {
> result = a.annotationType().getAnnotation(type);
> }
> }
> }
> return result;
> }
> }
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the weld-issues
mailing list