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

TgetAnnotation(Class clazz, Class annotation)
get Annotation
if (clazz.isAnnotationPresent(annotation))
    return clazz.getAnnotation(annotation);
Class<?>[] intfaces = clazz.getInterfaces();
for (Class<?> intface : intfaces) {
    try {
        if (intface.isAnnotationPresent(annotation))
            return intface.getAnnotation(annotation);
    } catch (SecurityException e) {
...
OptionalgetAnnotation(Class clazz, Class annotation)
Returns the specified T annotation for the potentially annotated Class class if it exists.
return Optional.ofNullable(clazz.getAnnotation(annotation));
TgetAnnotation(Class clazz, Class annotationClass)
This method scans the class and super-class hierarchy (up to, but not including java.lang.Object) for the specified annotation class.
if (clazz.getName().equals(Object.class.getName())) {
    return null;
T annotation = clazz.getAnnotation((Class<T>) annotationClass);
if (annotation == null) {
    annotation = getAnnotation(clazz.getSuperclass(), annotationClass);
return annotation;
...
TgetAnnotation(Class clazz, Class annotationClass)
get Annotation
T annotation = null;
if (hasAnnotation(clazz, annotationClass)) {
    annotation = clazz.getAnnotation(annotationClass);
return annotation;
TgetAnnotation(Class clazz, Class annotationClass)
get Annotation
T t = clazz.getAnnotation(annotationClass);
return t;
TgetAnnotation(Class clazz, Class annotationType)
get Annotation
T result = clazz.getAnnotation(annotationType);
if (result == null) {
    Class<?> superclass = clazz.getSuperclass();
    if (superclass != null) {
        return getAnnotation(superclass, annotationType);
    } else {
        return null;
} else {
    return result;
TgetAnnotation(Class clazz, Class annotationType)
Retrieve an annotation from a possibly proxied CDI/Weld class.
T an = clazz.getDeclaredAnnotation(annotationType);
if (an == null && isProxy(clazz)) {
    an = clazz.getSuperclass().getDeclaredAnnotation(annotationType);
return an;
TgetAnnotation(Class clazz, Class annotationType)
get Annotation
Iterable<T> allAnnotations = Iterables.filter(getAnnotationsRecursive(clazz), annotationType);
if (allAnnotations.iterator().hasNext()) {
    return allAnnotations.iterator().next();
return null;
TgetAnnotation(Class clazz, String fieldName, Class annotationClass)
get Annotation
try {
    Field field = clazz.getDeclaredField(fieldName);
    if (!field.isAccessible()) {
        field.setAccessible(true);
    return field.getAnnotation(annotationClass);
} catch (NoSuchFieldException e) {
    return null;
...
TgetAnnotation(Class clazz, String name, Class[] types, Class annotationType)
get Annotation
if (clazz == null) {
    return null;
try {
    Method method = clazz.getMethod(name, types);
    T annotation = method.getAnnotation(annotationType);
    if (annotation != null) {
        return annotation;
...