@@ -143,19 +143,22 @@ public static <A extends Annotation> Optional<A> findAnnotation(Optional<? exten
143143 return Optional .empty ();
144144 }
145145
146- return findAnnotation (element .get (), annotationType , new HashSet <>());
146+ boolean inherited = annotationType .isAnnotationPresent (Inherited .class );
147+
148+ return findAnnotation (element .get (), annotationType , inherited , new HashSet <>());
147149 }
148150
149151 /**
150152 * @see org.junit.platform.commons.support.AnnotationSupport#findAnnotation(AnnotatedElement, Class)
151153 */
152154 public static <A extends Annotation > Optional <A > findAnnotation (AnnotatedElement element , Class <A > annotationType ) {
153- return findAnnotation (element , annotationType , new HashSet <>());
155+ boolean inherited = annotationType .isAnnotationPresent (Inherited .class );
156+ return findAnnotation (element , annotationType , inherited , new HashSet <>());
154157 }
155158
156159 @ SuppressWarnings ("unchecked" )
157160 private static <A extends Annotation > Optional <A > findAnnotation (AnnotatedElement element , Class <A > annotationType ,
158- Set <Annotation > visited ) {
161+ boolean inherited , Set <Annotation > visited ) {
159162
160163 Preconditions .notNull (annotationType , "annotationType must not be null" );
161164
@@ -179,22 +182,34 @@ private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElemen
179182
180183 // Meta-present on directly present annotations?
181184 Optional <A > directMetaAnnotation = findMetaAnnotation (annotationType , element .getDeclaredAnnotations (), key ,
182- visited );
185+ inherited , visited );
183186 if (directMetaAnnotation .isPresent ()) {
184187 return directMetaAnnotation ;
185188 }
186189
187- // Search on interfaces
188190 if (element instanceof Class ) {
189191 Class <?> clazz = (Class <?>) element ;
192+
193+ // Search on interfaces
190194 for (Class <?> ifc : clazz .getInterfaces ()) {
191195 if (ifc != Annotation .class ) {
192- Optional <A > annotationOnInterface = findAnnotation (ifc , annotationType , visited );
196+ Optional <A > annotationOnInterface = findAnnotation (ifc , annotationType , inherited , visited );
193197 if (annotationOnInterface .isPresent ()) {
194198 return annotationOnInterface ;
195199 }
196200 }
197201 }
202+
203+ // Search in class hierarchy
204+ if (inherited ) {
205+ Class <?> superclass = clazz .getSuperclass ();
206+ if (superclass != null && superclass != Object .class ) {
207+ Optional <A > annotationOnSuperclass = findAnnotation (superclass , annotationType , inherited , visited );
208+ if (annotationOnSuperclass .isPresent ()) {
209+ return annotationOnSuperclass ;
210+ }
211+ }
212+ }
198213 }
199214
200215 // Indirectly present?
@@ -205,16 +220,17 @@ private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElemen
205220 }
206221
207222 // Meta-present on indirectly present annotations?
208- return findMetaAnnotation (annotationType , element .getAnnotations (), key , visited );
223+ return findMetaAnnotation (annotationType , element .getAnnotations (), key , inherited , visited );
209224 }
210225
211226 private static <A extends Annotation > Optional <A > findMetaAnnotation (Class <A > annotationType ,
212- Annotation [] candidates , AnnotationCacheKey key , Set <Annotation > visited ) {
227+ Annotation [] candidates , AnnotationCacheKey key , boolean inherited , Set <Annotation > visited ) {
213228
214229 for (Annotation candidateAnnotation : candidates ) {
215230 Class <? extends Annotation > candidateAnnotationType = candidateAnnotation .annotationType ();
216231 if (!isInJavaLangAnnotationPackage (candidateAnnotationType ) && visited .add (candidateAnnotation )) {
217- Optional <A > metaAnnotation = findAnnotation (candidateAnnotationType , annotationType , visited );
232+ Optional <A > metaAnnotation = findAnnotation (candidateAnnotationType , annotationType , inherited ,
233+ visited );
218234 if (metaAnnotation .isPresent ()) {
219235 annotationCache .put (key , metaAnnotation .get ());
220236 return metaAnnotation ;
0 commit comments