Ref:Java Annotations tutorial,Wiki,How do Annotations Work In Java?,
Annotation is special kind of Java construct used to decorate a class, method, field, parameter, variable, constructor, or package. It’s the vehicle chosen by JSR-175 to provide metadata.Java annotations are typically used for the following purposes:
- Compiler instructions
- Build-time instructions
- Runtime instructions
Basics
Annotations have a number of uses, among them:
- Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
- Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing — Some annotations are available to be examined at runtime.
The format of an Annotation:In its simplest form, an annotation looks like @Entity
,the annotation can include elements, which can be named or unnamed,if there is just one element named value, then the name can be omitted,if the annotation has no elements, then the parentheses can be omitted, as shown in the previous @Override
example,it is also possible to use multiple annotations on the same declaration.If the annotations have the same type, then this is called a repeating annotation,repeating annotations are supported as of the Java SE 8 release.
The predefined annotation types defined in java.lang are @Deprecated
, @Override
, and @SuppressWarnings
.Note that the Javadoc tag starts with a lowercase d (deprecated
)and the annotation starts with an uppercase D(Deprecated
).
Every compiler warning belongs to a category. The Java Language Specification lists two categories: deprecation
and unchecked
. The unchecked warning can occur when interfacing with legacy code written before the advent of generics. To suppress multiple categories of warnings, use the following syntax:@SuppressWarnings({"unchecked", "deprecation"})
.
Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line.As of the Java SE 8 release, annotations can also be applied to the use of types.
Declaring an Annotation Type
Declaring an annotation type, syntax for doing this is:
1 2 3 4 5 6 7 8 9 |
|
The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface.The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.Annotations only support primitives, string and enumerations. All attributes of annotations are defined as methods and default values can also be provided.
1 2 3 4 5 6 7 8 9 |
|
Note: To make the information in @ClassPreamble appear in Javadoc-generated documentation, you must annotate the @ClassPreamble definition with the @Documented annotation:
1 2 3 4 5 6 7 8 9 |
|
You can also define that your annotation is a qualifier for the @Inject
annotation.
1 2 3 4 5 6 |
|