

The values String since() default "" and boolean forRemoval() default false were added to provide even more info for compilers and IDE to work with. Since Java 9, this previous marker annotation becomes a configuration annotation.
#Java annotations code
We can mark code as deprecated, and the compiler/IDE can access this information to tell us the code isn't supposed to be used anymore. But if we provide an annotation, the compiler makes sure we actually override a method, and not just accidentally add or overload it. If we want to override a method but have a simple type in the signature, or the wrong argument type, that error might go unnoticed. This information is not strictly necessary, but it helps to reduce mistakes. Indicates that a method overrides/replaces an inherited method.The JDK includes multiple annotations beside the ones we already encountered for creating annotation types itself: If only a single value is provided when used, we can omit the curly braces. Array of any preceding type (single-dimension only)Īrrays are handled uniquely.The allowed types are defined in the JLS 9.6.1: But in my opinion, the behavior of those two overlaps enough to be treated as (almost) equal.Ĭonfiguration annotations support multiple values. Examples: Java Language Specification (JLS) splits Configuration into Normal Annotation and Single Element Annotation. Examples: Configuration - Values present, maybe with default values for less typing when used.

The mere presence is the actual metadata. Values are optional, separating annotations into two groups: But providing additional values besides the annotation type itself is even better. The annotation accepts an array of targets:īeing able to annotate our code and check if the annotation is present at different lifecycle events is great.

ElementType.TYPE_USE - Any usage of a type, like declarations, generic parameters, or casts.ElementType.TYPE_PARAMETER - Generic type parameters.ElementType.TYPE - Classes, interfaces, enum.ElementType.PACKAGE - Package declarations.The eight available targets are defined in : That’s why we can explicitly set the acceptable targets. every annotation makes sense on every available target. We should always choose the lowest retention possible for our code to still work. The provided metadata might contain sensitive information on the inner workings of the annotated code. Which retention policy we need for our custom annotations depends on our requirements. RetentionPolicy.RUNTIME All metadata will be available at runtime.Any post-compile byte-code tools might use the metadata. Annotations are visible to the compiler, and will be available in the class files, but not at runtime. RetentionPolicy.CLASS The default retention policy.This retention policy is useful for pre-compile tools, like annotation processors. The compiler will discard the metadata, so neither compiler nor runtime has access to it. RetentionPolicy.SOURCE Annotations are only available in the source.The retention policy of annotations reflects these lifecycles and provides us with a way to specify the exact availability of metadata: Typical lifecycle of our code is as follows: Source Code ▼ ▼ ◁ Compiler ▼ Class file ▼ ▼ ◁ JVM ▼ Runtime
