Java Reflection Annotation Find findAnnotation(Class clazz, Class annotationClass, Set> set)

Here you can find the source of findAnnotation(Class clazz, Class annotationClass, Set> set)

Description

find Annotation

License

Apache License

Declaration

private static Annotation findAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass,
            Set<Class<?>> set) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;

import java.util.Arrays;

import java.util.Set;

public class Main {
    private static Annotation findAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass,
            Set<Class<?>> set) {
        if (clazz == null || set.contains(clazz) || clazz.equals(Object.class)) {
            return null;
        }//  www. ja v a  2  s  .c  om

        set.add(clazz);

        Annotation result = findAnnotation(clazz.getSuperclass(), annotationClass, set);

        if (result != null) {
            return result;
        }

        Annotation findResult = Arrays.stream(clazz.getInterfaces())
                .map((Class<?> interf) -> findAnnotation(interf, annotationClass, set))
                .filter((Annotation annotation) -> annotation != null).findFirst().orElse(null);

        if (findResult != null) {
            return findResult;
        }

        Annotation interfaceAnnotation = Arrays.stream(clazz.getDeclaredAnnotations())
                .filter((Annotation annotation) -> annotation.annotationType().equals(annotationClass)).findFirst()
                .orElse(null);

        if (interfaceAnnotation != null) {
            return interfaceAnnotation;
        }

        return Arrays.stream(clazz.getDeclaredAnnotations())
                .map((Annotation annotation) -> findAnnotation(annotation.annotationType(), annotationClass, set))
                .filter((Annotation annotation) -> annotation != null).findFirst().orElse(null);
    }
}

Related

  1. findAnnotation(Annotation[] searchList, Class annotation)
  2. findAnnotation(Class clazz, Class annotationType)
  3. findAnnotation(Class clazz, Class annotationType)
  4. findAnnotation(Class aClass, Class annotationClass)
  5. findAnnotation(Class classy, Class targetAnnotation)
  6. findAnnotation(Class clazz, Class annotationClass)
  7. findAnnotation(Class clazz, Class annotationType)
  8. findAnnotation(Class clazz, Class annotationType)
  9. findAnnotation(Class clazz, Class annotationType)