Annotations   «Prev  Next»

Annotations Overview

The Java specification describes an annotation as a marker that associates information with the program construct but has no effect at runtime.
Oracle Annotations Specification
Java annotations are like Javadoc tags which can be read from source files, but unlike Javadoc tags, annotations can be configured (using a Retention Policy) to be retained invisible to the Java virtual machine, allowing them to be accessible at runtime.
Annotations are often used by frameworks (such as Spring or the Java persistence API) as a way to apply behaviors to user defined classes and methods which would otherwise be declared in an external source (such as an XML configuration file) or by requiring the implementation of specified API calls.

Two Types of Annotations

There are two types of annotations that specify what is being annotated:
  1. The declaration annotation or the
  2. Type annotation. The declaration annotation allows you to annotate any or all of the following types in Java. MODULE
  3. PACKAGE
  4. ANNOTATION_TYPE
  5. TYPE
  6. TYPE_PARAMETER
  7. CONSTRUCTOR
  8. METHOD
  9. PARAMETER
  10. FIELD
  11. LOCAL_VARIABLE

As of the Java SE8 release, annotations can also be applied to the following usages of types:
  1. instantiating an object,
  2. Casting, or
  3. implementing an interface.

Retention Policy

An annotation has something called a retention policy which specifies to the compiler, whether the annotation will be specified (retained) in the generated class file and whether ultimately it is information which gets picked up by the JVM which can then be read using reflection methods.

Retention Policy Code 	in .class file		Retained by JVM - 
                                                              	Read Reflectively 
SOURCE			NO				NO
CLASS			YES				NO
RUNTIME			YES				YES

An annotation is recognized by the @ sign. In its simplest form and annotation looks like:
@MyFirstAnnotation

An annotation can also include elements with values. The elements can be named or unnamed. The named element in the example below is ”name”.
@MyFirstAnnotation (name=”THIS”)

An unnamed element is allowed if only one element is configured for the annotation and his named and value.
@MySecondAnnotation(“LAST”)
can be used in place of :
@MySecondAnnotation(value=“LAST”)

If the annotation has no elements, then the parentheses can be omitted:
@MyElementlessAnnotation

can be used in place of:
@MyElementlessAnnotation()

You can use multiple annotations to describe a single declaration.
@MySecondAnnotation("LAST")
@MyFirstAnnotation(name="FIRST")
public void method(){

A public method where 2 annotations appear above the method declaration. You can use the same annotation with different element values to describe a single declaration. This feature is only available if the adaptation is declared repeatable.
Example: An example of using a repeatable annotation is shown below.
@MyFirstAnnotation(name=”THIS”)
@MyFirstAnnotation(name=”THAT”)
public class AnnotationExamples{

Three Categories of Annotations

There are three categories of annotations based upon the elements declared in the annotation.
The Java specification names these as
  1. Normal,
  2. Marker, and
  3. Single element.

Note that both Marker and Single elements annotations are also "Normal" annotations with special identifying features
(No element specified or a single element (value) specified but not named). A normal annotation must contain an element value pair for every element of the corresponding annotation type, except for those elements with default values, or a compile time error occurs.

Three Type of Annotation
Three Type of Annotation

Types of Annotation Element Values Marker 1) Specifies no element values , 2) Specifies element/value mappings but all have a default value
Examples
@MyElementlessAnnotation
Single Element
  1. Specifies only a single element value named value
  2. Specifies element/value mappings in addition to the element named value, but all other elements are declared with a default value
  3. Note that value is the unnamed element in the use of the Annotation
    @MySecondAnnotation("THIS")
Normal:
  1. Specifies zero to many element values
  2. Specifies zero to many element default values

@MyFirstAnnotation(name="THAT", value="MORE")

Annotations Overview

Creating a custom annotation is easy and edit simplest looks like the following
public @interface MyInterface{
}

When you create a new class in IntelliJ, you can specify an annotation type. To specify properties about an annotation you annotates the annotation type with one or all of the following annotations, found in
java.lang.annotation package.
https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/package-summary.html
Annotations on annotation types are called meta-annotations.

4 types of Annotations

  1. Documented
  2. Inherited
  3. Native
  4. Repeatable

Four Annotation Types
Four Annotation Types

Repeatable Example: Class<? Extends Annotation> value
Retention Target
Retention Target