From http://www.cnblogs.com/ggjucheng/archive/2012/12/03/2800487.html
English from http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
Annotation isProgramProvide data, but it is not part of the program. They are not annotatedCodeDirectly affects applications.
Annotations are used as follows:
- Compiler information-the compiler uses annotations to detect errors or suppress warnings.
- Processing during compilation and deployment-software tools can process annotated information to generate code, XML files, and so on.
- Runtime processing-some annotations are checked during runtime.
Annotations can be applied to declared classes, fields, methods, and other program elements.
@ Author (name= "Benjamin Franklin", Date= "3/27/2003")ClassMyclass () {}or @ suppresswarnings (Value= "Unchecked")VoidMymethod (){}
If an element is named value, the name can be ignored. For example:
@ Suppresswarnings ("unchecked")VoidMymethod (){}
If the annotation has no elements, parentheses can be ignored. For example:
@ OverrideVoidMysupermethod (){}
Document
Many annotations replace comments already in the code.
Assuming that a traditional software group starts at the beginning of each class, annotations provide important information:
Public ClassGeneration3listExtendsGeneration2list {//Author: John Doe//Date: 3/17/2002//Current revision: 6//Last modified: 4/12/2004//By: Jane Doe//Reviewers: Alice, Bill, Cindy//Class Code goes here}
To use annotations to provide the same metadata, you must first declare an annotation type. The syntax is:
@InterfaceClasspreamble {string author (); string date ();IntCurrentrevision ()Default1; String lastmodified ()Default"N/"; String lastmodifiedby ()Default"N/";//Note use of ArrayString [] reviewers ();}
Annotation declaration, just like adding a @ character (@ is at, that is, annotation type) before the interface declaration ). Annotation type is actually a form of interface, which will be covered in Later courses. For the moment, you do not need to understand the interface
The body of the annotation declaration, including the declaration of the annotation element, looks like a method. Note: The optional default values can be defined here.
Once the annotation is defined, you can fill in the annotation value when using the annotation, as shown in the following figure:
@ classpreamble (author = "John Doe" , date =" 3/17/2002 ", currentrevision = 6 , lastmodified =" 4/12/2004 ", lastmodifiedby = "Jane Doe" , /// note array notation reviewers = {" Alice "," Bob ", "Cindy" }) Public class generation3list extends generation2list { /// class code goes here }
Note: To make @ classpreamble information appear in the file generated by javadoc, you must use@ Brief ented annotation Definition@ Classpreambl
E
//Import this to use @ documentedImportJava. Lang. annotation .*; @ Documented @InterfaceClasspreamble {//Annotation element definitions}
Compiler usage Annotation
There are several annotation types predefined by the Language Specification:@ Deprecated
,@ Override
,@ Suppresswarnings.
@ deprecated - @ deprecated
indicates that the elements of the logo are discarded and should not be used again. The compiler generates a warning in any program that uses the @ deprecated class, method, and field. When an element is discarded, it should also use javadoc's @ deprecated
to mark the document, as shown in the following example. The use of the "@" symbol in the two javadoc annotations and annotations is not a coincidence-they are related concepts. In addition, note that the javadoc Mark starts with the lowercase letter "D" and the annotation to start with the uppercase letter "D ".
//Javadoc comment follows/***@ Deprecated* Explanation of why it * was deprecated*/@ DeprecatedStatic VoidDeprecatedmethod () {}}@ override-@ override annotation notifies the compiler to overwrite the elements declared by the parent class.//Mark Method as a superclass Method//That has been overridden@ OverrideIntOverriddenmethod (){}
Although annotations are not required when overwriting a method, they can avoid errors. If a method is marked@ Override, but it cannot correctly override any method of the parent class. The compiler will generate an error.
@ Suppresswarnings-@ Suppresswarnings
Annotation, which tells the compiler to suppress specific warnings in normal cases. In the following example, an obsolete method is used, and the compiler generates a warning normally. In this case, this annotation causes the warning to be blocked.
//Use a deprecated method and tell//Compiler not to generate a warning@ Suppresswarnings ("deprecation")VoidUsedeprecatedmethod (){//Deprecation warning//-SuppressedObjectone. deprecatedmethod ();}
The warning for each compiler belongs to one category. The Java language specification has two categories: "deprecation" and "unchecked ". "Unchecked" generates a warning when you use the legacy code of the generic type previously written for interaction. To suppress warnings of more categories, use the following syntax:
@ Suppresswarnings ({"unchecked", "deprecation "})
Annotation Processing
More advanced usage of annotation, including annotation processing, reading a Java program, and executing some actions according to the annotation. For example, it can generate a secondarySource codeReduces the workload of the sample code created by programmers and always follows the predictable mode. To promote this work, JDK 5.0 contains an annotation processing tool and becomes apt. In JDK 6.0, APT is part of the Java compiler standard.
Annotations are used during runtime. annotation types must be used.@ Retention (retentionpolicy. runtime)
Annotation, for example:
Import JAVA. lang. annotation. * ;@ retention (retentionpolicy. runtime) @ interface annotationforruntime { /// elements that give information /// for runtime processing }