Annotation, that is, annotation, is a new feature of Java 5. The explanation in thinking Java is: annotation is one of the important language changes introduced to Java se5. They can provide information to fully describe the information required by the program, which cannot be expressed in Java. Therefore, annotations allow us to store additional information about the program in a format that will be tested and verified by the compiler. Annotations can be used to generate descriptor files, or even new class definitions, and help ease the burden of writing sample code. By using annotations, we can save these metadata in the Java source code and use the annotation API to construct processing tools for our own annotations. At the same time, annotations also have the following advantages: more clean and easy-to-read code and compiler type checks. Although Java se5 pre-defines some metadata, in general, it is necessary for programmers to add new annotations themselves and use them in their own way. All in all, annotation is mainly between source code and API file description. Annotation provides some instructions and explanations for the program code. Make yourself and others easy to see.
In this instance code, annotation is used to mark custom error codes. The Code is as follows:
Error. Java annotation Definition
Package COM. test. util; </P> <p> Import Java. lang. annotation. retention; <br/> Import Java. lang. annotation. retentionpolicy; </P> <p>/** <br/> * Class Name: <br> <br/> * class description: <br> <br/> * @ version V1.0 <br/> * @ author lu7kang 2011-3-6 <br/> */<br/> @ retention (retentionpolicy. runtime) <br/> Public @ interface error {<br/> string MSG () Default ""; <br/>}
The error code customized by errorcode. java. Use the @ error annotation to describe the error code.
Package COM. test. util; </P> <p>/** <br/> * Class Name: Management System Error Codes <br> <br/> * class description: <br> <br/> * @ version V1.0 <br/> * @ author lu7kang 2011-3-6 <br/> */<br/> Public Enum errorcode {< /P> <p> @ error (MSG = "log on to the system first ") <br/> sys_00, <br/> @ error (MSG = "no operation permission") <br/> sys_01, <br/> @ error (MSG = "Logon timeout") <br/> sys_02, <br/> @ error (MSG = "system exception") <br/> sys_03; </P> <p>/** <br/> * Return Error Code <br/> */<br/> Public String getcode () {<br/> return this. name (); <br/>}</P> <p>/** <br/> * error message returned <br/> */<br/> Public String getmessage () {<br/> Error error = NULL; <br/> try {<br/> error = This. getclass (). getfield (this. getcode ()). getannotation (error. class); <br/>}catch (exception e) {<br/> return NULL; <br/>}< br/> return error. MSG (); <br/>}</P> <p >}< br/>
Test. Java test class
Package COM. test. util; </P> <p>/** <br/> * Class Name: <br> <br/> * class description: <br> <br/> * @ version V1.0 <br/> * @ author lu7kang 2011-3-7 <br/> */<br/> public class test {< /P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> system. out. println (errorcode. sys_00.getmessage (); <br/> system. out. println (errorcode. sys_01.getmessage (); <br/> system. out. println (errorcode. sys_02.getmessage (); <br/> system. out. println (errorcode. sys_03.getmessage (); <br/>}</P> <p>}
Running result:
Log on to the system first.
No operation permission
Logon timeout
System exception