1
votes

I want to retrieve an annotation (a custom written one) from a method. Usually I can ask the classloader by accessing

class.getMethod("methodName").getAnnotation("annotationName")

But if the bean is managed by a CDI container (I am using OpenWebBeans) the class is enhanced at runtime. Then I have to use the superclass to ask for annotations. Currently I try to detect if the class is managed by looking for "$$" in the classname. But that seems to be a very dirty solution to me.

Is there any good way to retrieve anntations from a CDI managed bean?

In detail my code is something like that: I created an annotation "Coolnessfactor" to mark a method to be very cool :-)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Coolnessfactor {
    CoolnessValue value();
}

Via the enumeration CoolnessValue I want to specify how cool the method implementation is.

public enum CoolnessValue {
    POOR, VERY_COOL, UNBELIEVABLE;
}

Then I mark different methods in my business classe with this annotation, fe:

@Override
@Coolnessfactor(CoolnessValue.POOR)
public void getSingleObjectWithDetails(final Integer techId) {
    return this.dao.findCompleteDataByPrimaryKey(techId);
}

Now I want to analyse the value of the annotation which marks the different method. I have to do it in a CDI-Decorator, therefore I cannot do it with an interceptor binding.

At the moment my approach is to use the reflection API to retrieve the annotation value:

public static <A extends Annotation> Map<String, A> getAnnotatedMethodsOfClass(final Class<?> clazz,
        final Class<A> annotationClazz) {
    final Map<String, A> annotationMap = new HashMap<String, A>();
    Method[] declaredMethods;
    if (clazz.getName().contains("$$")) {
        declaredMethods = clazz.getSuperclass().getDeclaredMethods();
    } else {
        declaredMethods = clazz.getDeclaredMethods();
    }
    for (final Method m : declaredMethods) {
        if (m.isAnnotationPresent(annotationClazz)) {
            annotationMap.put(m.getName(), m.getAnnotation(annotationClazz));
        }
    }
    return annotationMap;
}

But this looks very awful to me. Especcially the detection of a class which is enhanced by the CDI implementation is very bad.

1
Could you please give more details on what you are trying to achieve? - cassiomolin
Could the BeanManager be useful for you? - cassiomolin
What are you trying to achieve with the annotation on the CDI bean? Maybe (wild guess) a CDI interceptor would be more suitable? - Nikos Paraskevopoulos
Very sorry to be not detailed enough. I added some more code details in the question. - Steven Rudolf
I do not know if the BeanManager helps me in that case. I tried to scan thru the API but found nothing really helpful. - Steven Rudolf

1 Answers

0
votes

Maybe try it with BeanManager - you will want to use it to get hold of a Bean<?> instance of your bean. The approaches differ here, based on what kind of bean it is. Shuffle through the API and find your way.

Once you get Bean<?> you should be able to use getBeanClass() and with that you gain access to methods and their annotations.