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(Annotation[] annotaions, Class T)
find an annotation from an array
for (int i = 0; i < annotaions.length; i++) {
    if (annotaions[i].annotationType().equals(T)) {
        return (T) annotaions[i];
return null;
AgetAnnotation(Annotation[] annotations, Class annotationType)
Gets the annotation that has the specified type, or null if there isn't one
for (Annotation anno : annotations)
    if (annotationType.isAssignableFrom(anno.getClass()))
        return (A) anno;
return null;
OptionalgetAnnotation(Annotation[] annotations, Class annotationClazz)
get Annotation
for (Annotation a : annotations) {
    if (annotationClazz.isAssignableFrom(a.getClass())) {
        return Optional.of((T) a);
return Optional.empty();
TgetAnnotation(Annotation[] annotations, Class annotationType)
Finds an annotation of given annotationType from the given annotations .
for (Annotation annotation : annotations) {
    if (annotation.annotationType().equals(annotationType)) {
        return (T) annotation;
return null;
TgetAnnotation(Annotation[] annotations, Class clazz)
get Annotation
for (Annotation annotation : annotations)
    if (clazz.isAssignableFrom(annotation.getClass()))
        return clazz.cast(annotation);
return null;
TgetAnnotation(Class clazz, Class annotation)
get Annotation
if (clazz.isAnnotationPresent(annotation)) {
    Annotation value = clazz.getAnnotation(annotation);
    return annotation.cast(value);
return null;
AnnotationgetAnnotation(Class cls, Class annotationCls)
get Annotation
if (cls == null) {
    return null;
Annotation ret = cls.getAnnotation(annotationCls);
if (ret != null) {
    return ret;
ret = getAnnotation(cls.getSuperclass(), annotationCls);
...
AnnotationgetAnnotation(Class cls, Class annotationCls)
get Annotation
Annotation res = cls.getAnnotation(annotationCls);
if (res != null) {
    return res;
} else if (!cls.equals(Object.class)) {
    return getAnnotation(cls.getSuperclass(), annotationCls);
} else {
    return null;
AnnotationgetAnnotation(Class theExaminedClass, Class theExpectedAnnotation)
The method returns the expected annotation.
return getAnnotation(theExaminedClass, theExpectedAnnotation, false);
AnnotationgetAnnotation(Class anno, Class cl)
get Annotation
return cl.getAnnotation(anno);