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

Pete Muir (JIRA) jira-events at lists.jboss.org
Tue Apr 13 07:20:06 EDT 2010


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

Pete Muir commented on WELDX-95:
--------------------------------

Lincoln/Dan can you comment on why you need this. It's pretty much what we are trying to avoid doing in Seam 3 (have modules concerned about low level concerns) and suggests there are problems in the CDI API/SPI that need addressing quickly.

A naive solution like above won't cut it, as it will block continually, so by adding stuff like this to the modules, we are reinventing the wheel (as this is already done in Weld). We also loose the ability to take advantage of the containers underlying ability to cache this sort of info (as it knows when/where it comes from).



> 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