Annotations (also known as metadata ) provide a formalized way for us to add information to our code so that we can use it very conveniently at a later point in time.
1 Basic syntax
The annotated method does not differ from the other methods. Annotations can work with any modifier on a method, such as public, static, or void. From a grammatical point of view, annotations are used almost exactly the same way as modifiers.
1.1 Defining annotations
The definition of an annotation looks much like the definition of an interface. In fact, as with any other Java interface, annotations will be compiled into class files .
Some meta annotations (meta-annotation)are required when defining annotations, such @Target
as @Retention
.
In annotations, some elements are typically included to represent certain values . These values are available to the program or tool when parsing the annotations. The annotated element looks like the method of the interface, and the only difference is that you can specify a default value for it. There are restrictions on the types of elements.
Annotations without elements are called tag annotations (marker annotation).
All annotations are inherited annotation
.
Package Net.mrliuli.annotations;import java.lang.annotation.*; @Target (Elementtype.method) @Retention ( retentionpolicy.runtime) public @interface Test {}//(marker annotation)
1.2 Three standard annotations and four types of meta annotations
java.lang
three standard annotations defined in:
@Overrided
@Deprecated
@SuppressWarnings
Four types of meta-annotations:
@Target indicates where the annotation can be used.
@Retension indicates at what level the annotation information needs to be saved.
@Documented include this annotation in Javadoc.
@Inherited allow subclasses to inherit annotations from the parent class.
1.3 About annotations
There is a restriction on the type of the annotation element, which cannot be any type, and a compiler with a type other than the allowed type will give an error.
Default value limit:
@Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interface the simulationnull{public int ID ( ) default-1; Public String description () Default "";}
First, the element cannot have an indeterminate value. In other words, the element must either have a default value or provide the value of the element when using annotations.
Second, for elements of a non-primitive type, either when declared in the source code or when a default value is defined in the annotation interface, it cannot be null
used as its value. This constraint makes it difficult for the annotation processor to represent the presence or absence of an element, because in the declaration of each annotation, all elements are present and have corresponding values. To circumvent this constraint, we can only define some special values, such as an empty string or a negative number, to indicate that an element does not exist:
Generates an external file. Some frameworks require some additional information to work with your source code, which is best suited for annotations to show their value. Technologies such as enterprise JavaBean need to deploy a profile. Web Service, custom tag libraries, and object/relational mapping tools, such as TopLink and hibernate, typically require XML profiles that are detached from the source code.
2 Writing the annotation processor
An important part of the process of using annotations is creating and using the note processor , which is used to read the annotations .
Related articles:
Java Programming Thought Lesson (v) 18th Chapter-java IO system
Java Programming thought Lesson (vi) 19th-enumeration type