OK, I will not replicate the link to what an annotation is. Rather, I will talk about how they can operate.
Annotations have both a Target
and a Retention
; optionally, they can also be @Documented
so that you know this annotation has been present at some time in your source code. Now, onto the target and retention since these two "meta-annotations" will definitely have an influence on what you can expect:
- the target determines what that annotation can be applied to; this can be a whole class, an instance variable, a method, a... Well, see the doc.
- the retention determines how "long" this annotation persists in your source code. The two retention policies used in majority are
SOURCE
and RUNTIME
.
In an annotation such as yours, it is typically an annotation with a runtime retention policy. Such an annotation can be used, at runtime, by specific processing code, to change the behaviour of the target so that its behaviour be controlled by this processing code. Such code is logically called an annotation processor.
Annotations have begun to take quite some an importance in some JSRs:
- JSR 330 defines
@Inject
, @Provider
etc as runtime annotations, so that frameworks willing to do dependency injection can rely on these annotations being present; this is the case, for instance, for dependency injection frameworks such as Dagger and Guice (since version 3.0);
- JSR 305 defines
@Immutable
, @ThreadSafe
, @NotThreadSafe
, @Nullable
, @Nonnull
with a source retention policy, and @Documented
; these annotations can be used by static code analysis tools, and of equal importance, they are typically @Documented
as well. Therefore you know what to expect of them.