[weld-issues] [JBoss JIRA] Commented: (WELDX-95) Need common method of scanning for annotations (static or dynamic)

Adam Warski (JIRA) jira-events at lists.jboss.org
Wed Apr 14 03:07:25 EDT 2010


    [ https://jira.jboss.org/jira/browse/WELDX-95?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12525557#action_12525557 ] 

Adam Warski commented on WELDX-95:
----------------------------------

A general use-case is to retrieve an element value from an @InterceptorBinding annotation, where the element is @Nonbinding, but carries some information for the interceptor, e.g. a security condition. Taking into account that the annotation can be dynamically added by a portable extension.

> 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
>          Components: Weld Extensions
>            Reporter: Lincoln Baxter III
>
> 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