Java Utililty Methods Reflection Annotation

List of utility methods to do Reflection Annotation

Description

The list of methods to do Reflection Annotation are organized into topic(s).

Method

ListgetAnnotationFields(Class clazz, Class annotationClass)
Retrieves all Field of given Class and his superclasses annotated with given Annotation class.
if (clazz == null || annotationClass == null) {
    throw new NullPointerException("clazz and annotationClass parameters must be not null.");
} else if (clazz.isInterface()) {
    throw new IllegalArgumentException("clazz parameter can not be an interface, it must be a class.");
} else if (!annotationClass.isAnnotation()) {
    throw new IllegalArgumentException("annotationClass parameter must be an annotation.");
List<Field> fields = new ArrayList<Field>();
...
TgetAnnotationForMethodOrContainingClass(Method m, Class annotationType)
Look for a specified annotation on a method.
T annotation = m.getAnnotation(annotationType);
if (annotation != null) {
    return annotation;
return m.getDeclaringClass().getAnnotation(annotationType);
TgetAnnotationForProperty(Class cls, String name, Class anno)
get Annotation For Property
String postFix = name.toUpperCase();
if (name.length() > 1) {
    postFix = name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = null;
try {
    method = cls.getMethod("get" + postFix);
} catch (NoSuchMethodException e) {
...
TgetAnnotationFromAnnotation(Annotation annotation, Class annotationClass)
Check annotation and its meta reflection to see if it's match annotationClass if match, return the instance of that annotation, otherwise return null
if (annotation.annotationType() == annotationClass) {
    return (T) annotation;
} else if (annotation.annotationType().getName().startsWith("java.lang.annotation")) {
    return null; 
} else {
    Class<? extends Annotation> annotationType = annotation.annotationType();
    Annotation[] metaAnnotations = annotationType.getAnnotations();
    for (Annotation metaAnnotation : metaAnnotations) {
...
ListgetAnnotationFromEntityFields(Class entity, Class annotation)
Obtains list of annotation objects from entity
List<Object> objs = null;
Field[] fields = entity.getDeclaredFields();
if (fields != null) {
    for (Field field : fields) {
        if (field.isAnnotationPresent(annotation)) {
            if (objs == null) {
                objs = new ArrayList<Object>();
            objs.add(field.getAnnotation(annotation));
return objs;
AnnotationgetAnnotationFromEntityOrInterface(Class annotationType, Class entity)
Given an array and a typed predicate, determines if the array has an object that matches the condition of the predicate.
Annotation result = entity.getAnnotation(annotationType);
if (result == null) {
    for (Class inter : entity.getInterfaces()) {
        result = inter.getAnnotation(annotationType);
        if (result != null) {
            break;
return result;
TgetAnnotationFromField(Object object, Class annotationClass)
Get the annotation from the given object
Field[] declaredFields = object.getClass().getDeclaredFields();
for (Field field : declaredFields) {
    if (field.isAnnotationPresent(annotationClass)) {
        return field.getAnnotation(annotationClass);
return null;
AnnotationgetAnnotationFromJoinpointMethod(ProceedingJoinPoint joinpoint, Class annotationClass)
get Annotation From Joinpoint Method
MethodSignature signature = (MethodSignature) joinpoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Annotation annotation = joinpoint.getTarget().getClass().getMethod(methodName, parameterTypes)
        .getAnnotation(annotationClass);
return annotation;
TgetAnnotationFromStackTrace(Class annotationClass)
Returns first occurrence of specified annotation if one is found on method or class within current thread stack trace.
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
for (StackTraceElement stack : trace) {
    Method method = null;
    Class<?> clazz = null;
    try {
        String methodName = stack.getMethodName();
        clazz = Class.forName(stack.getClassName());
        method = clazz.getMethod(methodName, null);
...
AgetAnnotationFromWeldBean(Object target, Class annotation)
Gets the annotation from weld bean.
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) {
...