Java Utililty Methods Reflection Annotation Find

List of utility methods to do Reflection Annotation Find

Description

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

Method

TfindAnnotation(Class clazz, Class annotationType)
Look for the annotationType in the class or one of its ancestors.
T a = clazz.getAnnotation(annotationType);
if (a != null) {
    return a;
Class<?> parentClass = clazz.getSuperclass();
if (parentClass != null && !parentClass.equals(Object.class)) {
    a = findAnnotation(parentClass, annotationType);
if (a == null) {
    for (Class<?> i : clazz.getInterfaces()) {
        a = findAnnotation(i, annotationType);
        if (a != null) {
            return a;
return a;
TfindAnnotation(Class klass, Class annotationClass)
Returns the annotation on the given class or the package of the class.
T ann = klass.getAnnotation(annotationClass);
while (ann == null && klass != null) {
    ann = klass.getAnnotation(annotationClass);
    if (ann == null)
        ann = klass.getPackage().getAnnotation(annotationClass);
    if (ann == null) {
        klass = klass.getSuperclass();
        if (klass != null) {
...
AfindAnnotation(final Class clazz, final Class type)
Find a method annotation on the declaring class or in any of it's implemented interfaces
final List<Class<?>> classes = allImplementedTypes(clazz);
for (final Class<?> c : classes) {
    try {
        final A annotation = c.getAnnotation(type);
        if (annotation != null) {
            return annotation;
    } catch (final Exception e) {
...
TfindAnnotation(final Class clazz, final Class annotation)
find Annotation
checkNotNull(clazz, "clazz is null");
checkNotNull(annotation, "annotation is null");
Class<?> currentClazz = clazz;
while (currentClazz != null) {
    T a = currentClazz.getAnnotation(annotation);
    if (a != null) {
        return a;
    Class<?> enclosingClass = currentClazz.getEnclosingClass();
    while (enclosingClass != null) {
        a = findAnnotation(enclosingClass, annotation);
        if (a != null) {
            return a;
        enclosingClass = enclosingClass.getEnclosingClass();
    final Class<?>[] interfaces = currentClazz.getInterfaces();
    for (final Class<?> interfaceClass : interfaces) {
        a = findAnnotation(interfaceClass, annotation);
        if (a != null) {
            return a;
    currentClazz = currentClazz.getSuperclass();
return null;
AfindAnnotation(final Class type, final Class annotationType)
Recursively search a type's inheritance hierarchy for an annotation.
A annotation = type.getAnnotation(annotationType);
if (annotation != null) {
    return annotation;
for (final Class<?> subType : type.getInterfaces()) {
    annotation = findAnnotation(subType, annotationType);
    if (annotation != null) {
        return annotation;
...
AfindAnnotation(final Class targetAnnotation, final Class annotatedType)
find Annotation
A foundAnnotation = annotatedType.getAnnotation(targetAnnotation);
if (foundAnnotation == null) {
    for (Annotation annotation : annotatedType.getAnnotations()) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType.isAnnotationPresent(targetAnnotation)) {
            foundAnnotation = annotationType.getAnnotation(targetAnnotation);
            break;
return foundAnnotation;
TfindAnnotation(final Class annotationClass, final Class beanClass, final Field field)
find Annotation
final T fieldAnnotation = field.getAnnotation(annotationClass);
return fieldAnnotation == null ? beanClass.getAnnotation(annotationClass) : fieldAnnotation;
OptionalfindAnnotation(final Set annotations, final Class annotationType)
find Annotation
return annotations.stream().filter(annotation -> matches(annotation, annotationType)).findFirst()
        .map(annotationType::cast);
TfindAnnotation(Method method, Class annotationType)
find Annotation
T anno = method.getAnnotation(annotationType);
if (anno != null) {
    return anno;
return findAnnotation(method.getDeclaringClass(), method, annotationType);
OptionalfindAnnotation(Method method, Class type)
find Annotation
Class<?> clazz = method.getDeclaringClass();
T annotation = method.getAnnotation(type);
while (annotation == null) {
    clazz = clazz.getSuperclass();
    if (clazz == null || Object.class == clazz) {
        break;
    try {
...