Java 9 introduced Annotations in the java.lang.annotation
In Java 9, a major feature introduced was the Java Platform Module System (JPMS), commonly referred to as Project Jigsaw. As part of this modular system, Java 9 introduced a few built-in annotations in the `java.lang.annotation` package to support module-level metadata.
Here are the built-in annotations related to the Java Module System, located in the `java.lang.annotation` package:
@Target
@Target(ElementType.MODULE)
This use of @Target indicates that some annotations are applicable only at the module level.
Java 9 added ElementType.MODULE as a new constant to the ElementType enum to support module declarations.
@Retention
@Retention(RetentionPolicy.CLASS)
Used with module-level annotations to specify that they are retained in the .class file, but not available at runtime.
Most module annotations introduced in Java 9 are retained in the .class files to assist with tools like javac or jlink.
@Documented
Indicates that module annotations should be included in the generated Javadoc.
@Native
Though not new to Java 9, this annotation becomes relevant in modules if constants are used that will be referenced in native code.
These annotations are not directly attached to `module-info.java` declarations, but Java 9 allows custom annotations to be applied to modules using these enhancements to `@Target` and `ElementType`.
Example of a custom module annotation:
Summary of Java 9 Enhancements for Module Annotations:
Feature
Description
ElementType.MODULE
Enables annotations on module-info.java
@Target(ElementType.MODULE)
Used to define annotations applicable to modules
@Retention(RetentionPolicy.CLASS)
Annotations stored in class files but not visible at runtime
Custom Module Annotations
You can define metadata for tools using module-level annotations
Built-in Annotations
The following table lists Java's built-in annotations which are in the java.lang package.
Take a bit of time to study and memorize this table.
You may be tested on the retention policy and target of these on the exam.
Deprecated
Predefined Java Annotations
Element(s)
Targets
Retention
Description
@Deprecated
boolean forRemoval
String since
PACKAGE, MODULE, TYPE
CONSTRUCTOR, METHOD
PARAMETER
FIELD, LOCAL_VARIABLE
RUNTIME
Compiler generates a warning whenever a program uses a method, class, or field with this annotation.
The @Deprecated annotation should always be present if the @deprecated javadoc tag is present, and vice-versa.
@FunctionalInterface
N/A
TYPE
RUNTIME
Compiler generates an error if the type does not satisfy the requirements of a functional interface.
Override
Predefined Java Annotations
Element(s)
Targets
Retention
Description
@Override
N/A
METHOD
SOURCE
Compiler generates an error if a method marked with this annotation does not correctly override a method in one of its super classes.
@SafeVarargs
N/A
CONSTRUCTOR, METHOD
RUNTIME
Compiler suppresses unchecked warnings relating to varargs usage.
@SuppressWarnings
String[] value
MODULE, TYPE
CONSTRUCTOR, METHOD
PARAMETER
FIELD
LOCAL_VARIABLE
SOURCE
This annotation causes certain compiler warnings to be suppressed. Some entries for value are:
deprecated
removal
unchecked
varargs
Built-in Annotations 1
Annotation
Description
Element Type/Name
Default Value
Retention
Indicates how long annotations with the annotated type are to be retained.
RetentionPolicy value
RetentionPolicy.CLASS
Target
Indicates the contexts in which an annotation type is applicable. Important Note: If @Target meta annotation is not used on an annotation,
the annotation can be used for all target types, except type parameter declarations.
ElementType[] value
N/A
Built-in Annotations 2
Type of Annotation
Element Values
Examples
Marker
Specifies no element values.
Specifies element/value mappings but all have a default value.
@MyElementlessAnnotation
Single Element
Specifies only a single element value named "value".
Specifies element/value mappings in addition to the element named value, but all other elements are declared with a default value.
Note that value is the unnamed element in the use of the annotation.
@MySecondAnnotation("THIS")
Normal
Specifies zero to many element values.
Specifies zero to many element default values.
@MyFirstAnnotation(name="THAT", value="MORE")
@SuppressWarnings is a pre-defined Java Annotation.
The interface consists of a single abstract method. An interface is not a functional interface if it has more than 1 abstract method.
You cannot use this Annotation @FunctionalInterface in front of an abstract class because it is not an interface.
Cannot apply @FunctionalInterface to an 1) abstract class or 2) enum.
If a type is annotated with this annotation type, compilers are required to generate an error message unless:
The type is an interface type and is not an annotation type, enum or class,
The annotated type satisfies the requirements of a functional interface.