Annotation (2) in java -- how annotation works

Source: Internet
Author: User

The previous article has introduced how to use the three standard annotation of JDK. This article will introduce the principle of annotation and how to customize annotation and use annotation to complete some practical functions.

Define Annotation

The @ interface keyword is used to define the new annotation type. In a sense, it indicates that annotation is similar to the interface definition. The following code defines an annotation

// Use DK Metadata Annotation: retention @ retention (retentionpolicy. runtime) // use the JDK Metadata Annotation: Target @ target (elementtype. method) // defines a tag comment that does not contain any member variables. That is, metadata public @ interface testable {} cannot be input {}

Use Annotation

After annotation is defined, the annotation can be used anywhere in the program. The use of annotation is similar to the modifier such as public and final, generally, classes, methods, variables, interfaces, and other definitions can be modified (by default, annotation is used to modify any program element ). The following code uses the annotation method above.

Public class mytest {// use @ testable to mark the annotation to specify that this method is testable @ testablepublic static void M1 () {} public static void m2 () {} // use @ testable to mark the annotation to specify that this method is testable @ testablepublic static void m3 () {Throw new runtimeexception ("boom");} public static void M4 () {} // use @ testable to mark the annotation and specify that this method is testable. @ testablepublic static void M5 () {} public static void M6 () {} // use @ testable to mark the annotation to specify that this method is testable @ testablepublic static void M7 () {Throw new runtimeexception ("crash");} public static void M8 () {}}

Extract Annotation

After a class or method is modified using annotation, annotation information needs to be extracted through reflection. Note that after an annotation type is defined as runtime annotation, the annotation is visible during runtime. When the class file is loaded, the annotation stored in the class file will be read by the virtual machine. The following code extracts annotation information and performs corresponding operations based on the information. This method is called if a method is marked as testable.

Public class testprocessor {public static void process (string clazz) throws classnotfoundexception {int passed = 0; int failed = 0; // traverses all methods of the OBJ object for (method M: class. forname (clazz ). getmethods () {// If the annotation if (M. isannotationpresent (testable. class) {try {// call m method M. invoke (null); // passed plus 1 passed ++;} catch (exception ex) {system. out. printf ("method" + M + "failed to run, exception" + ex. getcause () + "\ n"); failed ++ ;}}// measure the test result system. out . Println ("A total of:" + (passed + failed) + "methods run, where: \ n failed:" + failed + ", and \ n succeeded: "+ passed +! \ N ");}}

The main program of the program is as follows:

Public class runtests {public static void main (string [] ARGs) throws exception {// process mytest class testprocessor. Process ("annotation. mytest ");}}

The running result is as follows:

Working Principle

The above is just a simple use of Annotation. The more complex annotation is the same as the above example, but the corresponding logical judgment and the content involved in Annotation are more complicated. However, the core part is to use an operation class to extract annotation information and perform operations through reflection. In short:No reflection, no comment (annotation).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.