Java has provided the annotation (annotation) function since j2se5. The annotation of Java can be attached to packages, classes, methods, and fields, which is equivalent to adding additional auxiliary information to them. Annotation attached to package, class, method, field, etc. If there is no external parsing tool, etc, it will not affect Java source code or class, nor affect their execution.
However, external tools such as javac and EJB containers can be used to parse annotation attached to package, class, method, and field, and process annotation accordingly, for example, changing the behavior of objects/methods during running.
Java standard Annotation
@ Deprecated is equivalent to @ deprecated of javadoc. It is not recommended to specify the object class and method marked by @ deprecated. It is mainly used for compiling tools such as javac.
@ Override indicates that the object method reloads the method of the parent class. During compilation, compilation tools such as javac will determine whether the overload method is correct based on this annotation.
@ Suppresswarnings tells javac and other compilers to ignore the specified warning information.
@ Target the defined annotation can be attached to those objects.
@ Retention Annotation
Use of java standard Annotation
@ Deprecated:
1. @ deprecated
2.Public ClassTestbean {
3 ....
4 .}
@ Deprecated
Public class testbean {
...
}
@ Suppresswarnings
1. @ suppresswarnings ("serial ")
2.Public ClassTestbeanImplementsJava. Io. serializable {
3 ....
4 .}
5.
6. @ suppresswarnings (value = {"serial", "unchecked "})
7.PublicString dosomething (){
8 ....
9 .}
@ Suppresswarnings ("serial ")
Public class testbean implements java. Io. serializable {
...
}
@ Suppresswarnings (value = {"serial", "unchecked "})
Public String dosomething (){
...
}
@ Override
1. @ override
2.PublicString dosomething (){
3 ....
4 .}
@ Override
Public String dosomething (){
...
}
Annotation Definition
Definition method:
@ Interface annotation name {definition body}
Definition Example 1:
Public @ InterfaceMyannotation {}
Public @ interface myannotation {}
This example defines an annotation without any attributes/methods.
Example 2:
1.Public @ InterfaceMyannotation {
2.PublicString Value ();
3 .}
Public @ interface myannotation {
Public String Value ();
}
This example defines Annotation with only one method as value. Generally, there is only one annotation method, and the method name must be defined as value.
Example 3:
1. @ retention (retentionpolicy. runtime)
2. @ target (elementtype. Method)
3.Public @ InterfaceMyannotation {
4.PublicString Value ();
5.PublicString [] multivalues ();
6.IntNumber ()Default0;
7.
8 .}
@ Retention (retentionpolicy. runtime)
@ Target (elementtype. Method)
Public @ interface myannotation {
Public String Value ();
Public String [] multivalues ();
Int number () default 0;
}
This example defines an annotation with multiple methods. Set the default value of one method number to 0. The multivalues method is defined as an array. The annotation definition can be modified using metaannotation. Metaannotation has the following two:
@ Retention
@ Target
@ Retention and @ target are described below.
@ Retention
@ Retention can be set to a value of the retentionpolicy type.
Example:
@ Retention (retentionpolicy. runtime)
|
RetentionpolicyValue |
Description |
|
Retentionpolicy. Class |
The annotation information will be stored in the class file during compilation by the compiler, but will not be loaded in the VM during execution. That is to say, the annotation information cannot be dynamically obtained during execution. If @ retention is not set, this will be the default value. |
|
Retentionpolicy. Runtime |
The annotation information will be stored in the class file during compilation by the compiler, and will be loaded by the VM during execution. |
|
Retentionpolicy. Source |
The annotation information will be discarded by the compiler during compilation. |
@ Target
@ Target indicates the Java element to which annotation can be attached and can be set to a value of the Java. Lang. annotation. elementtype array type.
Example 1:
1. @ target (elementtype. Method)
@ Target (elementtype. Method)
Example 2:
1. @ target (value = {elementtype. constructor, elementtype. Field, elementtype. local_variable, elementtype. Method })
@ Target (value = {elementtype. constructor, elementtype. Field, elementtype. local_variable, elementtype. Method })
Elementtype is an enumeration type with the following definitions:
|
ElementtypeValue |
Description |
|
Elementtype. annotation_type |
Meta annotation applied to other annotations |
|
Elementtype. Constructor |
Constructor |
|
Elementtype. Field |
Field |
|
Elementtype. local_variable |
Local variables in the Method |
|
Elementtype. Method |
Method |
|
Elementtype. Package |
Package |
|
Elementtype. Parameter |
Method Parameters |
|
Elementtype. Type |
Class, interface, or enumeration Declaration
|