CDI Tip #1 - Get the annotation for a proxied class

In the Ozark runtime there is a time we need to get the annotation on a class. In some cases those instances are proxies and we need to get the annotation of the class behind the proxy. The code snippet below shows you how to do that.

    public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationType) { 
        final T an = clazz.getDeclaredAnnotation(annotationType); 
        if (an != null) { 
            return an;  

        final BeanManager bm = CDI.current().getBeanManager(); 
        final AnnotatedType<?> type = bm.createAnnotatedType(clazz); 
        return type != null ? type.getAnnotation(annotationType) : null; 

Posted July 29, 2015