The annotation metadata is divided into 4 sections, respectively, Target,documented,inherited,retention:
target> Specifies that annotated annotations can only be used on a type; ELEMENTTYPE specifies its type: can be used as a method
fields, classes, return values, etc.;
Statement:
/** * @author Lean @date: 2014-10-13 * /@Target (elementtype.method) public @interface workinprogress {}
Application:
/** * @author Lean @date: 2014-10-13 */public class Annotationsample {//When used in a field: the annotation @WorkInProgress Is disallowed-location//@WorkInProgressprivate int age, @WorkInProgresspublic static Boolean dosomething () {//To Do auto-generated method Stubreturn false;}}
retention> setting annotation visibility; using to Retentionpolicy enumeration
retentionpolicy.source>> compiler is visible, but not visible to. class files and execution time;
Retentionpolicy.class>> default tools are visible, visible to. CLASS files, but not visible for execution;
Retentionpolicy.runtime>> is visible during execution, and is not known by the. class file and tells the JVM at execution time;
The following example is a performing introspection check, which must be separated by {} and commas when a meta-annotation requires more than one qualified value.
such as @target ({elementtype.method,elementtype.type})
/** * @author Lean @date: 2014-10-13 */ @WorkInProgresspublic class Annotationsample {private int age;@ Workinprogresspublic static Boolean dosomething () {//TODO auto-generated method Stubreturn false;} public static void Main (string[] args) {annotationsample obj=new annotationsample (); Class Clazz=obj.getclass (); Workinprogress progress= (workinprogress) clazz.getannotation (Workinprogress.class); System.out.println (Clazz.getname ()), if (Clazz.isannotationpresent (Workinprogress.class)) {System.out.println (" Class annotationed workinprogress! ");} Method[] Methods=clazz.getmethods (); for (Method method:methods) {if (Method.isannotationpresent ( Workinprogress.class) {System.out.println ("method annotationed workinprogress!");}}} @Retention (Retentionpolicy.runtime) @Target ({elementtype.method,elementtype.type}) @interface workinprogress {}
Documented> write a document for the Annotated encyclopedia, and execute the Javadoc DOS command after compiling the execution. You can do this on the command line
location to see the resulting document.
Inherited> When you annotate a class, you want to annotate the subclass that inherits him. By default
Without using the @inherited annotation method, the system feels that the subclass does not need to inherit the feature, such as the following:
/** * @author Lean @date: 2014-10-13 * /@WorkInProgresspublic class Annotationsample {public static void main ( String[] args) throws illegalaccessexception {annotationsample obj=new annotationsample (); Class Clazz=obj.getclass (), if (Clazz.isannotationpresent (Workinprogress.class)) {System.out.println ("class annotationed workinprogress! ");} Class childclass=annotationchildclass.class;if (Childclass.isannotationpresent (Workinprogress.class)) { SYSTEM.OUT.PRINTLN ("Child class annotationed workinprogress!");}}} @Inherited @documented@retention (retentionpolicy.runtime) @Target ({elementtype.method,elementtype.type}) @ Interface Workinprogress {}class Annotationchildclass extends annotationsample{}
Java Advanced-Annotations