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 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 runtimes;
Retentionpolicy.class>> default tools are visible, visible to. CLASS files, but not visible to run;
The retentionpolicy.runtime>> runtime is visible; it is not known by the. class file to tell the JVM the value at run time;
The following example is a run-time introspection check that when a meta-annotation requires more than one qualified value, it must be separated by the {} and a comma.
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 annotated books, compile and run the Javadoc DOS command. 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 will assume that the subclass does not need to inherit the feature, as follows:
/** * @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-annotation