Java Annotations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 34: Line 34:
</syntaxhighlight>
</syntaxhighlight>


 
@Target is a [[#Meta-Annotation|meta-annotation]] that indicates the context in which the annotated annotation is applicable. Possible values:
 
* ElementType.TYPE - for Class, interface (including annotation type) or enum declaration.
      // This annotation can only be applied to class methods.
* ElementType.FIELD - for field declaration (includes enum constants).
 
* ElementType.METHOD - for method declaration.
The compiler reserves a set of special annotations (including [[@Deprecated]], [[@Override]] and @[[SuppressWarnings]]) for syntactic purposes.
* ElementType.PARAMETER - for parameter declaration.
* ElementType.CONSTRUCTOR - for constructor declaration.
* ElementType.LOCAL_VARIABLE - for local variable declaration.
* ElementType.ANNOTATION_TYPE - for annotation type declaration.
* ElementType.PACKAGE - for method declaration.
* ElementType.TYPE_PARAMETER - for package declaration.
* ElementType.TYPE_USE - for use of a type.
* ElementType.MODULE - for module declaration.


=Meta-Annotation=
=Meta-Annotation=


Annotations that are applied to other annotations: [[#@Retention|@Retention]], [[#@Target|@Target]].
Annotations that are applied to other annotations: [[#@Retention|@Retention]], [[#@Target|@Target]].

Revision as of 03:17, 1 November 2018

Internal

Overview

Custom Annotations

Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword.

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.

Annotations themselves may be annotated to indicate where and when they can be used"

@Retention

@Retention(RetentionPolicy.RUNTIME)

@Retention is a meta-annotation that specifies how long the annotations annotated with it are to be retained. Possible values:

  • RetentionPolicy.SOURCE: annotations are to be discarded by the compiler.
  • RetentionPolicy.CLASS (default): Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
  • RetentionPolicy.RUNTIME: Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

A Retention meta-annotation has effect only if the meta-annotated type is used directly for annotation.

@Target

@Target({ElementType.METHOD})

@Target is a meta-annotation that indicates the context in which the annotated annotation is applicable. Possible values:

  • ElementType.TYPE - for Class, interface (including annotation type) or enum declaration.
  • ElementType.FIELD - for field declaration (includes enum constants).
  • ElementType.METHOD - for method declaration.
  • ElementType.PARAMETER - for parameter declaration.
  • ElementType.CONSTRUCTOR - for constructor declaration.
  • ElementType.LOCAL_VARIABLE - for local variable declaration.
  • ElementType.ANNOTATION_TYPE - for annotation type declaration.
  • ElementType.PACKAGE - for method declaration.
  • ElementType.TYPE_PARAMETER - for package declaration.
  • ElementType.TYPE_USE - for use of a type.
  • ElementType.MODULE - for module declaration.

Meta-Annotation

Annotations that are applied to other annotations: @Retention, @Target.