Java Annotations in 10 minutes

Photo by Humphrey Muleba on Unsplash

Built-in Annotations

Retention Policies

  1. RetentionPolicy.SOURCE — The marked annotation is retained only in the source level and is ignored by the compiler. Can be used by the compiler to detect errors or suppress warnings.
  2. RetentionPolicy.CLASS — The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM). Software tools can process annotation information to generate code, XML files, and so forth.
  3. RetentionPolicy.RUNTIME — The marked annotation is retained by the JVM so it can be used by the runtime environment. Some annotations are available to be examined at runtime.

Element Types

  1. ElementType.ANNOTATION_TYPE can be applied to an annotation type.
  2. ElementType.CONSTRUCTOR can be applied to a constructor.
  3. ElementType.FIELD can be applied to a field or property.
  4. ElementType.LOCAL_VARIABLE can be applied to a local variable.
  5. ElementType.METHOD can be applied to a method-level annotation.
  6. ElementType.PACKAGE can be applied to a package declaration.
  7. ElementType.PARAMETER can be applied to the parameters of a method.
  8. ElementType.TYPE can be applied to any element of a class

Custom Annotation

From the definition of @Override annotation, we can see annotation is `interface` with a preceding at sign character: `public @interface Override`. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values.

Example 1 RetentionPolicy.SOURCE

Example 2 RetentionPolicy.RUNTIME

Annotation Definition

More complicated example

Annotations are often used by frameworks as a way of conveniently applying behaviours to user-defined classes and methods that must otherwise be declared in an external source (such as an XML configuration file) or programmatically (with API calls). Like Spring Framework Annotations: Link.

Bonus Information

Meta annotations

Annotations that apply to annotations are called meta annotations. Besides Retention and Target. Other meta annotations are:

  • @Documented: indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
  • @Inherited: indicates that the annotation type can be inherited from the super class. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, the class’ superclass is queried for the annotation type. This annotation applies only to class declarations.
  • @Repeatable: introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use.

Multiple annotations

Type Annotations

As of the Java SE 8 release, annotations can also be applied to the use of types. Here are some examples:

Reference

  1. https://docs.oracle.com/javase/tutorial/java/annotations/
  2. https://en.wikipedia.org/wiki/Java_annotation
  3. Sample code at Github.

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store