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 annotationClass, Class cls)
Recursively searches for an annotation

Search order

  1. cls class
  2. cls implementing interfaces
  3. cls super class
If after all those steps, no annotation is found, null is returned.
T annotation = cls.getAnnotation(annotationClass);
if (annotation == null) {
    for (final Class<?> interfaceCls : cls.getInterfaces()) {
        annotation = getAnnotation(annotationClass, interfaceCls);
        if (annotation != null)
            break;
if (annotation == null && cls.getSuperclass() != null && cls.getSuperclass() != Object.class)
    annotation = getAnnotation(annotationClass, cls.getSuperclass());
return annotation;
TgetAnnotation(Class annotationType, AnnotatedElement... objects)
Utility method to read out repeatable annotations.
for (AnnotatedElement obj : objects) {
    T annot = obj.getAnnotation(annotationType);
    if (annot != null) {
        return annot;
return null;
TgetAnnotation(Class cl, Object o)
Looks for the annotation specified on the given object.
return getAnnotation(cl, o.getClass());
TgetAnnotation(Class clazz, Method method, int parameterIndex)
get Annotation
for (Annotation annotation : method.getParameterAnnotations()[parameterIndex]) {
    if (annotation.annotationType() == clazz) {
        return (T) annotation;
return null;
TgetAnnotation(Class cls, Annotation... annotations)
get Annotation
if (annotations == null) {
    return null;
for (Annotation annotation : annotations) {
    if (cls.isAssignableFrom(annotation.getClass())) {
        return (T) annotation;
return null;
TgetAnnotation(Class cls, Method... methods)
get Annotation
T annotation = null;
for (Method method : methods) {
    if (method != null && (annotation = method.getAnnotation(cls)) != null) {
        return annotation;
return null;
TgetAnnotation(Class lookingFor, Annotation[] annotations)
Method searchs annotation in annotations array and return it.
if (annotations != null) {
    for (int i = 0; i < annotations.length; ++i) {
        if (lookingFor.isInstance(annotations[i])) {
            return (T) annotations[i];
return null;
...
AgetAnnotation(Enum enumConstant, Class annotationClass)
get Annotation
try {
    Field field = enumConstant.getClass().getDeclaredField(enumConstant.name());
    return getAnnotation(field, annotationClass);
} catch (Exception e) {
    throw new IllegalStateException(e);
AgetAnnotation(final AnnotatedElement annotatedElement, final Class annotationClass)
Returns the annotation or null if the element is not annotated with that type.
final Optional<Annotation> annotation = Stream.of(annotatedElement.getAnnotations())
        .filter(a -> a.annotationType().getName().equals(annotationClass.getName())).findAny();
return (A) annotation.orElse(null);
TgetAnnotation(final Class c, final Class annClass)
get Annotation
final List<T> found = getAnnotations(c, annClass);
if (found != null && !found.isEmpty()) {
    return found.get(0);
} else {
    return null;