Annotation (2) in Java----how annotation works

Source: Internet
Author: User

Annotation in Java (2)----Annotation Working principle Category: programming language 2013-03-18 01:06 3280 people read reviews (6) Favorite Report

The previous article has described how to use the JDK's three standard annotation, this article will introduce the principles of annotation, and how to customize annotation, and use annotation to accomplish some practical functions.

define Annotation

Defining a new annotation type uses the @interface keyword, which means that annotation and interface definitions are similar in some sense. The following code defines a annotation

[Java]View Plaincopyprint?
    1. Using the DK metadata annotation:retention
    2. @Retention (Retentionpolicy.runtime)
    3. Using the JDK's meta-data annotation:target
    4. @Target (Elementtype.method)
    5. Defines a tag comment that does not contain any member variables, that is, the metadata is not passed in
    6. Public @interface testable{
    7. }

Using annotation

After defining annotation, you can use the annotation anywhere in the program, and for annotation, like the modifier for public and final, you can usually modify the definitions of classes, methods, variables, interfaces, and so on (by default, Annotation is used to modify any program element). The following code uses the above annotation modification method.

[Java]View Plaincopyprint?
  1. public class MyTest {
  2. Use the @testable tag comment to specify that the method is testable
  3. @Testable
  4. public static void M1 () {
  5. }
  6. public static void M2 () {
  7. }
  8. Use the @testable tag comment to specify that the method is testable
  9. @Testable
  10. public static void M3 () {
  11. throw new RuntimeException ("Boom");
  12. }
  13. public static void M4 () {
  14. }
  15. Use the @testable tag comment to specify that the method is testable
  16. @Testable
  17. public static void M5 () {
  18. }
  19. public static void M6 () {
  20. }
  21. Use the @testable tag comment to specify that the method is testable
  22. @Testable
  23. public static void M7 () {
  24. throw new RuntimeException ("Crash");
  25. }
  26. public static void M8 () {
  27. }
  28. }

extract annotation

After a class or a method uses annotation adornments, it is necessary to extract annotation information through reflection. It is important to note that when a annotation type is defined as run-time annotation, the comment is visible to the runtime, and annotation that is stored in the class file when the class file is loaded will be read by the virtual machine. The following code is to extract the annotation information, according to the information to do the corresponding operation. In this case, this method is called if a method is marked as testable.

[Java]View Plaincopyprint?
  1. public class Testprocessor {
  2. public static void process (String clazz) throws classnotfoundexception{
  3. int passed=0;
  4. int failed=0;
  5. Traverse all methods of the Obj object
  6. For (Method m:class.forname (clazz). GetMethods ()) {
  7. If you include a @testable tag comment
  8. if (M.isannotationpresent (Testable.class)) {
  9. try{
  10. Call the M method
  11. M.invoke (NULL);
  12. Passed plus 1
  13. passed++;
  14. }catch (Exception ex) {
  15. System.out.printf ("Method" +m+ "Run failed, exception" +ex.getcause () + "\ n");
  16. failed++;
  17. }
  18. }
  19. }
  20. Statistical test Results
  21. SYSTEM.OUT.PRINTLN ("Total run:" + (passed+failed) + "method, where: \ n failed:" +failed+ ", A, \ n succeeded:" +passed+ "!\n");
  22. }
  23. }

Where the program's main program is as follows

[Java]View Plaincopyprint?
    1. public class Runtests {
    2. public static void Main (string[] args) throws exception{
    3. Handling MyTest Classes
    4. Testprocessor.process ("annotation. MyTest ");
    5. }
    6. }

The operation results are as follows

Working principle

The above is just a simple annotation use, more complex annotation and the above example principle is the same, but the corresponding logical judgment and the annotation involved in the content is more complex. But the core part is the use of an operation class to extract Annotation information through reflection, to operate, in short: no reflection, no annotation (Annotation).

Annotation (2) in Java----how annotation works

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.