DirectCodeFor more information, see notes ..
The first is the custom annotation labeled by @ retention ---- myannotation. Java
Package COM. jadyer. annotation. retention; </P> <p> Import Java. lang. annotation. retention; <br/> Import Java. lang. annotation. retentionpolicy; </P> <p> @ retention (retentionpolicy. runtime) <br/> Public @ interface myannotation {<br/> string Hello () Default "Haerbin"; <br/> string world (); <br/>}< br/>/********************** [@ retention is used to inform compilation of <SPAN class = 'wp _ keywordlink'> Program </span> how to handle annotation ]************************ ***** * *************************/<Br/> // Java. lang. annotation. the retention type can indicate how the compiler treats the custom annotation type when defining the annotation type. <br/> // the compiler will leave the annotation information on the default. class file, but not read by the virtual machine. It is only used to provide information when compiling a program or running a tool. <br/> // when using the retention type, Java is required. lang. annotation. retentionpolicy Enumeration type <br/> // Java. lang. annotation. retentionpolicy. class ---- default value. The Compilation Program stores annotation in the class file <br/> // Java. lang. annotation. retentionpolicy. source --- the compiled program completes the task after processing the annotation information <br/> // The comment will not be recorded in the class file, but only exist in. <br/> // Java. lang. annotation. retentionpolicy. runtime -- the Compilation Program stores annotation in the class file and can be read by JVM <br/> // that is, the compiler records the annotation in the class file, JVM retains comments at runtime <br/> // Therefore, reflection can be used together) mechanism for JVM to read annotation information <br/>/********************** [@ suppresswarnings is used to inform the Compilation Program suppression warning ]************************************* * ************/<br/> // Where the retentionpolicy of @ suppresswarnings annotation is source, that is, only the Compilation Program is notified during compilation to suppress the warning <br/> // If @ suppresswarnings defines a warning type that does not exist, then the program will not report an error <br/> // If @ suppresswarnings defines a warning type with a duplicate name, <br/> /*************************** **************************************** **************************************** ******/
Then the custom @ myannotation annotation test class ---- mytest. Java
Package COM. jadyer. annotation. retention; </P> <p> public class mytest {<br/> @ deprecated <br/> @ suppresswarnings ("unchecked ") <br/> @ myannotation (Hello = "Beijing", World = "Chongqing") <br/> Public void output () {<br/> system. out. println ("output something"); <br/>}< br/>}
Finally, the class that uses the reflection mechanism to read annotation information in mytest. Java ---- myreflection. Java
Package COM. jadyer. annotation. retention; </P> <p> Import Java. lang. annotation. annotation; <br/> Import Java. lang. reflect. method; </P> <p>/** <br/> * use the reflection mechanism to read mytest. annotation information in Java <br/> * @ see console to print the result, <br/> * @ see output something <br/> * @ see Beijing <br/> * @ see Chongqing <br/> * @ see Java. lang. deprecated <br/> * @ see COM. jadyer. annotation. retention. myannotation <br/> */<br/> public class myreflection {<br/> Public static void main (string [] ARGs) throws exception {<br/> mytest = new mytest (); <br/> class <mytest> C = mytest. class; <br/> method = C. getmethod ("output", new class [] {}); </P> <p> // determine whether a specific annotation exists before the method <br/> If (method. isannotationpresent (myannotation. class) {<br/> // call the output () method in mytest <br/> method. invoke (mytest, new object [] {}); </P> <p> // obtain a specific annotation <br/> myannotation = method. getannotation (myannotation. class); </P> <p> // when assigning values to elements in annotation, it can be treated as an attribute, it can be taken as a method <br/> string Hello = myannotation. hello (); <br/> string world = myannotation. world (); </P> <p> system. out. println (Hello); <br/> system. out. println (World); <br/>}</P> <p> annotation [] annotations = method. getannotations (); <br/> for (annotation: Annotations) {<br/> // The @ suppresswarnings annotation is not printed, because its retentionpolicy is source <br/> system. out. println (annotation. annotationtype (). getname (); <br/>}< br/>}