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

AnnotationgetAnnotation(Class type, Class annotationType)
Returns the first annotation of the specified annotation type for the given type.
If no annotation is found for the type, the hierarchical ancestors are examined.
Annotation annotation = null;
for (Class<?> t = type; annotation == null && t != null; t = t.getSuperclass())
    annotation = t.getAnnotation(annotationType);
return annotation;
AgetAnnotation(Class type, Class ann)
get Annotation
A a;
do {
    a = type.getAnnotation(ann);
    Class<?>[] faces = type.getInterfaces();
    for (int i = 0; a == null && i < faces.length; i++) {
        Class<?> face = faces[i];
        a = face.getAnnotation(ann);
} while (a == null && !Object.class.equals(type = type.getSuperclass()));
return a;
AgetAnnotation(Class type, Class annotationType)
get Annotation
return getAnnotation(type, annotationType, true);
TgetAnnotation(Class type, Class annotationClass)
Return the provided Class' annotation for the specified type if such an annotation is present, else null.
T t = type.getAnnotation(annotationClass);
if (t != null) {
    return t;
for (Class<?> i : type.getInterfaces()) {
    if ((t = getAnnotation(i, annotationClass)) != null) {
        return t;
return null;
AgetAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)
get Annotation
return getAnnotation(ac, false, m, directAnnotations);
AgetAnnotation(Class annotationType, Class classType)
get Annotation
return classType.getAnnotation(annotationType);
EgetAnnotation(Class at, Enum enu)
get Annotation
try {
    return enu.getClass().getField(enu.name()).getAnnotation(at);
} catch (Exception e) {
return null;
TgetAnnotation(Class realClass, String pAttributeName, Class annotationClass)
get Annotation
T annotation = null;
Field field = null;
try {
    field = realClass.getDeclaredField(pAttributeName);
} catch (NoSuchFieldException e) {
if (field != null) {
    annotation = field.getAnnotation(annotationClass);
...
TgetAnnotation(Class a, Class c)
Similar to Class#getAnnotation(Class) except also searches annotations on interfaces.
if (c == null)
    return null;
T t = getDeclaredAnnotation(a, c);
if (t != null)
    return t;
t = getAnnotation(a, c.getSuperclass());
if (t != null)
    return t;
...
TgetAnnotation(Class annotation, Class ownerClass, Method method)
Retrieves the annotation from the method, if it is not on the method, the parent is checked.
if (method == null) {
    return null;
if (method.isAnnotationPresent(annotation)) {
    return method.getAnnotation(annotation);
} else {
    for (Class<?> aInterface : ownerClass.getInterfaces()) {
        Method inheritedMethod = getInheritedMethod(aInterface, method);
...