To be able to use the isAnnotationPresent method on an AnnotatedElement at runtime, an annotation must be explicitly annotated with a RUNTIME retention policy. Otherwise, the annotation is not retained at runtime and cannot be observed using reflection.

Explicitly annotate annotations with a RUNTIME retention policy if you want to observe their presence using AnnotatedElement.isAnnotationPresent at runtime.

In the following example, the call to isAnnotationPresent returns false because the annotation cannot be observed using reflection.

To correct this, the annotation is annotated with a RUNTIME retention policy.

  • Java API Specification: Annotation Type Retention, RetentionPolicy.RUNTIME, AnnotatedElement.isAnnotationPresent().