public static Optional < Class < ? > > getClassWithAnnotation ( final Class < ? > source , final Class < ? extends Annotation > annotationClass ) { Class < ? > nextSource = source ; while ( nextSource != Object . class ) { if ( nextSource . isAnnotationPresent ( annotationClass ) ) { return Optional . of ( nextSource ) ; } else { nextSource = nextSource . getSuperclass ( ) ; } } return Optional . empty ( ) ; }
Class that returns if a class or any subclass is annotated with given annotation .