Java Reflection Annotation getAnnotationFromWeldBean(Object target, Class annotation)

Here you can find the source of getAnnotationFromWeldBean(Object target, Class annotation)

Description

Gets the annotation from weld bean.

License

LGPL

Parameter

Parameter Description
A the annotation type
target the target object to fetch the annotation from
annotation the annotation class

Return

the found annotation or 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

Declaration

public static <A extends Annotation> A getAnnotationFromWeldBean(Object target, Class<A> annotation) 

Method Source Code


//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;
    }
}

Related

  1. getAnnotationFromEntityFields(Class entity, Class annotation)
  2. getAnnotationFromEntityOrInterface( Class annotationType, Class entity)
  3. getAnnotationFromField(Object object, Class annotationClass)
  4. getAnnotationFromJoinpointMethod(ProceedingJoinPoint joinpoint, Class annotationClass)
  5. getAnnotationFromStackTrace(Class annotationClass)
  6. getAnnotationFullname(Annotation annotation)
  7. getAnnotationInClass( final Class annotationClass, final Class clazz)
  8. getAnnotationInHeirarchy(Class annotationClass, Class aClass)
  9. getAnnotationInherited(Class type, Class annotationClass)