Here you can find the source of getAnnotationFromWeldBean(Object target, Class annotation)
Parameter | Description |
---|---|
A | the annotation type |
target | the target object to fetch the annotation from |
annotation | the annotation class |
null
if the annotation is not present on the given class or the given object is not a weld proxy or not present on the proxy
public static <A extends Annotation> A getAnnotationFromWeldBean(Object target, Class<A> annotation)
//package com.java2s; //License from project: LGPL import java.lang.annotation.Annotation; import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final String ERROR_OCCURED_WITH_REFLECTION_ACCESS = "Error occured with reflection access"; /**/* w ww .j a va2 s .c om*/ * Gets the annotation from weld bean. <br> * The methods tries to retrieve the annotation from the class of the given object first. If not found tries to * fetch the getTargetClass method that is method from a Weld proxy class (@link * org.jboss.interceptor.util.proxy.TargetInstanceProxy#getTargetClass()). If such method is found the annotation is * locked on the object returned from the method call. * * @param <A> * the annotation type * @param target * the target object to fetch the annotation from * @param annotation * the annotation class * @return the found annotation or <code>null</code> if the annotation is not present on the given class or the * given object is not a weld proxy or not present on the proxy */ public static <A extends Annotation> A getAnnotationFromWeldBean(Object target, Class<A> annotation) { A extension = target.getClass().getAnnotation(annotation); if (extension == null) { Method declaredMethod = null; try { declaredMethod = target.getClass().getDeclaredMethod("getTargetClass", (Class<?>[]) null); declaredMethod.setAccessible(true); Object invoke = declaredMethod.invoke(target, (Object[]) null); if (invoke instanceof Class) { Class<?> targetClass = (Class<?>) invoke; extension = targetClass.getAnnotation(annotation); } } catch (Exception e) { // probably not a weld proxy class LOGGER.warn(ERROR_OCCURED_WITH_REFLECTION_ACCESS, e); } } return extension; } }