Applying Marker Annotations

A marker annotation is a shorthand notation typically for use with marker annotation types, which have no elements in their type declaration.

Click here to view code image

// Marker annotation type
@interface Tag {}                       // No element type declarations

The marker annotation of annotation type Tag is applied to a class as shown below, with just the name of the annotation type preceded by the at-sign (@)—that is, the parentheses are omitted.

Click here to view code image

@Tag class Gizmo {}                     // Marker annotation

The normal annotation of annotation type Tag is applied to a class, where the empty list of element-value pairs is explicitly specified:

Click here to view code image

@Tag() class Gizmo {}                   // Normal annotation

A marker annotation can also be used if all elements are optional annotation elements in the annotation type declaration—that is, they specify default values.

Click here to view code image

// Annotation type declaration where all elements have default values.
@interface Option {
  Color color()  default Color.WHITE;   // Optional annotation element
  Size size()    default Size.M;        // Optional annotation element
  enum Color {RED, WHITE, BLUE}
  enum Size  {S, M, L, XL}
}

A marker annotation of annotation type Option is applied to the class below:

Click here to view code image

@Option                                                    // Marker annotation
class Item {}

The above marker annotation is equivalent to the following normal annotation:

Click here to view code image

@Option(color = Option.Color.WHITE, size = Option.Size.M)  // Normal annotation
class Item {}

Applying Single-Element Annotations

A single-element annotation is a shorthand notation typically for use with single-element annotation types, whose only annotation type element is declared as a value() element. Single-element annotation will not work if the element in the single-element annotation type is not declared as a value() element. However, there is no requirement that the single element should be either required or optional. Note that there is no restriction on declaring a value() element for any annotation type, but that alone does not guarantee that single-element annotation can be used.

Click here to view code image

// Single-element annotation type
@interface Author {
  String value();                            // Single annotation type element
}

A single-element annotation of annotation type Author is applied to the class below, where only the value of the single value() element is specified:

Click here to view code image

@Author(“Tom”)                               // Single-element annotation
class Connection {}

The above single-element annotation is equivalent to the following normal annotation:

Click here to view code image

@Author(value = “Tom”)                       // Normal annotation
class Connection {}

Single-element annotation can also be used if one element is declared as a value() element and all other elements are optional annotation elements in the annotation type declaration—that is, they specify default values.

Click here to view code image

//Annotation type declaration with a value() element and other elements with
default values.
@interface ExtraOption {
  int value();                               // Required annotation element
  Color color()  default Color.WHITE;        // Optional annotation element
  Size size()    default Size.M;             // Optional annotation element
  enum Color {RED, WHITE, BLUE}
  enum Size  {S, M, L, XL}
}

A single-element annotation of annotation type ExtraOption is applied to the class below:

Click here to view code image

@ExtraOption(10)                                   // Single-element annotation
class ItemV2 {}

The above single-element annotation is equivalent to the following normal annotation:

Click here to view code image

@ExtraOption(value=10, color=ExtraOption.Color.WHITE,      // Normal annotation
             size=ExtraOption.Size.M)
class ItemV3 {}

Following is an example of using array-valued single-element annotation—that is, where the element type of the single annotation type element is an array type:

Click here to view code image

// Array-valued single-element annotation type declaration:
@interface Problems {
  String[] value();                                // Array-type value() element
}
@Problems({“Code smell”, “Exception not caught”})  // Array-valued single-element
class ItemV4 {}                                    // annotation

@Problems(value = {“Code smell”, “Exception not caught”})  // Normal annotation
class ItemV5 {}

Using a single-element array-valued single-element annotation—that is, where the element type of the single value() element is an array type, but only a single array element is specified in the annotation—is shown below.

Click here to view code image @Problems(“Code smell”)   // Single-element array-valued single-element annotation
class ItemV6 {}

@Problems({“Code smell”}) // Single-element array-valued single-element annotation
class ItemV7 {}

@Problems(value = “Code smell”)      // Normal annotation
class ItemV8 {}

@Problems(value = {“Code smell”})    // Normal annotation
class ItemV9 {}

Leave a Reply

Your email address will not be published. Required fields are marked *