The Java 8 type annotations (JSR 308) allow type checkers to perform static code analysis. For example, The Checker Framework can check for possible nullness via @NonNull
annotations.
Various projects define their own NonNull annotations, for example:
org.checkerframework.checker.nullness.qual.NonNull
edu.umd.cs.findbugs.annotations.NonNull
javax.annotation.Nonnull
javax.validation.constraints.NotNull
lombok.NonNull
org.eclipse.jdt.annotation.NonNull
- etc. (see The Checker Framework Manual, section 3.7)
For such annotations, I would expect the @interface
to have @Retention(RetentionPolicy.CLASS)
, because they are usually not needed at runtime. Most importantly, the code does not have any runtime dependencies on the respective library.
While org.eclipse.jdt.annotation.NonNull
follows this approach, most other NonNull annotations, like javax.annotation.Nonnull
(JSR 305) and org.checkerframework.checker.nullness.qual.NonNull
itself, have @Retention(RetentionPolicy.RUNTIME)
. Is there any particular reason for the RetentionPolicy.RUNTIME
in these annotations?
Clarification: The Checker Framework supports annotations in comments for backward compatibility. However, using those in Java 8 just to avoid runtime dependencies seems like a dirty hack.