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

Lincoln Baxter III (JIRA) jira-events at lists.jboss.org
Tue Apr 13 13:24:25 EDT 2010


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

Lincoln Baxter III commented on WELDX-95:
-----------------------------------------

When defining annotations, we need the ability to annotation attributes:

String cid = Annotations.get(ctx.getMethod(), Begin.class).id(); // @Begin(id="conversationId") is a user-specified conversation-id

Is there a better way can we do this?

If not, we also need to be able to support dynamic metadata (added by XML or an extension), and a lot can go wrong if developers don't do this consistently. Calculating Stereotype inheritance is complicated and really should not be done by developers re-inventing the wheel -- it should be a utility provided by the API/SPI in CDI. 

> 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