Java Annotations: Difference between revisions
Line 24: | Line 24: | ||
Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword. | Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword. | ||
<syntaxhighlight lang='java'> | |||
public @interface Encrypted { | |||
} | |||
</syntaxhighlight> | |||
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. | 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. | ||
Line 32: | Line 37: | ||
If you write custom annotations, you could use reflection at runtime to detect them and use them in a certain way, or you must also write custom compiler plug-ins to process them. There is a Pluggable Annotation Processing API, specified by [https://jcp.org/en/jsr/detail?id=269 JSR 269], which provides support for developing custom annotation processors. | If you write custom annotations, you could use reflection at runtime to detect them and use them in a certain way, or you must also write custom compiler plug-ins to process them. There is a Pluggable Annotation Processing API, specified by [https://jcp.org/en/jsr/detail?id=269 JSR 269], which provides support for developing custom annotation processors. | ||
Annotations themselves may be annotated to indicate where and when they can be used: | Annotations themselves may be annotated to indicate where and when they can be used: |
Revision as of 04:32, 1 November 2018
External
- https://jcp.org/en/jsr/detail?id=175
- https://www.developer.com/java/other/article.php/3556176
- https://docs.oracle.com/javase/6/docs/technotes/guides/language/annotations.html
- https://www.theregister.co.uk/2006/02/24/java_annotations/
- https://web.archive.org/web/20140223113106/http://www.somanyword.com/2014/02/how-to-create-and-implement-custom-annotations-in-java/
- http://www.tutorialsdesk.com/2014/10/java-annotations-tutorial-with-examples.html
- https://www.javaquery.com/2015/10/understanding-annotations-in-java.html
Internal
Overview
Annotations are a a form of syntactic metadata providing data about a program that is not part of the program it self. Syntactically, they are a special kind of interface type.
Annotations have no direct effect on the operation of the code they annotate.
Annotations have been first Introduced to the language in Java 5 and enhanced with JSR-308 in Java 8.
Custom Annotations
Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword.
public @interface Encrypted {
}
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.
The compiler reserves a set of special annotations (including @Deprecated, @Override and @SuppressWarnings) for syntactic purposes.
If you write custom annotations, you could use reflection at runtime to detect them and use them in a certain way, or you must also write custom compiler plug-ins to process them. There is a Pluggable Annotation Processing API, specified by JSR 269, which provides support for developing custom annotation processors.
Annotations themselves may be annotated to indicate where and when they can be used:
@Retention(RUNTIME)
@Target({METHOD, FIELD, TYPE})
public @interface Encrypted {
}
@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.
Predefined Annotations
- @Target
- @Retention
- @Inherited
- @Override
- @SuppressWarnings
- @Deprecated
- @SafeVarargs
- @Repeatable
- @FunctionalInterface
JSR 308
- JSR 308 Explained: Java Type Annotations https://www.oracle.com/technetwork/articles/java/ma14-architect-annotations-2177655.html