Reprinted from: http://www.iteye.com/topic/400085
Annotation provides a formal method for us to use the information in the Code Zhongtian domain.
You can easily use the data at a specific time (using the data through parsing annotations ).
The annotation syntax is relatively simple. Besides the use of the @ symbol, it is basically consistent with the inherent Syntax of Java. Java has three built-in
Annotation, which is defined in the Java. lang package.
@ OverrideIndicates that the current method overwrites the parent class.
@ DeprecatedIt indicates that the current element is not in favor of use.
@ SuppresswarningsIndicates disabling some improper compiler warning information.
The following is an example of defining Annotations:
Java code
- Package test_annotation;
- Import java. Lang. annotation. incluented;
- Import java. Lang. annotation. inherited;
- Import java. Lang. annotation. retention;
- Import java. Lang. annotation. target;
- Import java. Lang. annotation. elementtype;
- Import java. Lang. annotation. retentionpolicy;
- /*
- * Meta annotation @ target, @ retention, @ incluented, @ inherited
- *
- * @ Target indicates where the annotation is used. Possible elemenettype parameters include:
- * Elemenettype. constructor Declaration
- * Elemenettype. Field field Declaration (including Enum instances)
- * Elemenettype. local_variable local variable Declaration
- * Elemenettype. Method method declaration
- * Elemenettype. Package package Declaration
- * Elemenettype. Parameter parameter Declaration
- * Elemenettype. Type class, interface (including annotation type) or enum Declaration
- *
- * @ Retention indicates the level at which the annotation information is saved. Optional retentionpolicy parameters include:
- * The retentionpolicy. Source annotation will be discarded by the compiler.
- * The retentionpolicy. Class annotation is available in the class file, but is discarded by the VM.
- * Retentionpolicy. runtime VM also retains annotations during runtime. Therefore, the annotation information can be read through the reflection mechanism.
- *
- * @ Brief ented include this annotation in javadoc
- *
- * @ Inherited allows subclass to inherit the annotation in the parent class
- *
- */
- @ Target (elementtype. Method)
- @ Retention (retentionpolicy. runtime)
- @ Brief ented
- @ Inherited
- /*
- * Define annotation Test
- * The annotation contains two elements: ID and description.
- * The default value of the description element is "no description"
- */
- Public @ Interface Test {
- Public int ID ();
- Public String description () Default "no description ";
- }
Package test_annotation; import Java. lang. annotation. documented; import Java. lang. annotation. inherited; import Java. lang. annotation. retention; import Java. lang. annotation. target; import Java. lang. annotation. elementtype; import Java. lang. annotation. retentionpolicy;/** meta annotation @ target, @ retention, @ incluented, @ inherited ** @ target indicates where the annotation is used. Possible elemenettype parameters include: * elemenettype. constructor Declaration * elemenettyp E. field field Declaration (including Enum instances) * elemenettype. local_variable local variable Declaration * elemenettype. method method declaration * elemenettype. package package Declaration * elemenettype. parameter parameter Declaration * elemenettype. type class, interface (including annotation type) or enum declaration ** @ retention indicates the level at which the annotation information is saved. Optional retentionpolicy parameters include: * retentionpolicy. the source annotation will be discarded by the compiler * retentionpolicy. the class annotation is available in the class file, but it will be discarded by the VM * retentionpolicy. the runtime VM also keeps comments at runtime, so the annotation information can be read through the reflection mechanism. ** @ Brief ented include this annotation in javadoc ** @ inherited allows the subclass to inherit the annotation in the parent class **/@ target (elementtype. method) @ retention (retentionpolicy. runtime) @ brief ented @ inherited/** definition annotation test * contains two elements: ID and Description * description. The default value is "no description" */Public @ Interface Test {public int ID (); public String description () Default "no description ";}
The following is an example of using annotation and parsing annotation.
Java code
- Package test_annotation;
- Import java. Lang. Reflect. method;
- Public class test_1 {
- /*
- * Three annotated Methods
- */
- @ Test (ID = 1, description = "Hello method_1 ")
- Public void method_1 (){
- }
- @ Test (ID = 2)
- Public void method_2 (){
- }
- @ Test (ID = 3, description = "last method ")
- Public void method_3 (){
- }
- /*
- * Parse the annotation and print the information of all annotation methods of test_1.
- */
- Public static void main (string [] ARGs ){
- Method [] Methods = test_1.class.getdeclaredmethods ();
- For (method: Methods ){
- /*
- * Determine whether there are annotations of the specified annotation type in the Method
- */
- Boolean hasannotation = method. isannotationpresent (test. Class );
- If (hasannotation ){
- /*
- * Return the specified type annotation of a method based on the annotation type
- */
- Test annotation = method. getannotation (test. Class );
- System. Out. println ("test (method =" + method. getname ()
- + ", Id =" + annotation. ID () + ", description ="
- + Annotation. Description () + ")");
- }
- }
- }
- }
Package test_annotation; import Java. lang. reflect. method; public class test_1 {/** three methods annotated */@ test (ID = 1, description = "Hello method_1") Public void method_1 () {}@ test (ID = 2) Public void method_2 () {}@ test (ID = 3, description = "last method") Public void method_3 () {}/** parse the annotation and print the information of all annotation methods of test_1 class */public static void main (string [] ARGs) {method [] Methods = test_1.class.getdeclaredmethods (); For (method: Methods) {/** determines whether there are annotations for the specified annotation type in the Method */Boolean hasannotation = method. isannotationpresent (test. class); If (hasannotation) {/** specify the type annotation of the method returned Based on the annotation type */test annotation = method. getannotation (test. class); system. out. println ("test (method =" + method. getname () + ", id =" + annotation. ID () + ", description =" + annotation. description () + ")");}}}}
The output result is as follows: Test (method = method_1, id = 1, description = Hello method_1) test (method = method_2, id = 2, description = no description) test (method = method_3, id = 3, description = last method)