Application of Java annotation-dynamic parsing of annotation during runtime (3)

Source: Internet
Author: User

In the first two articles:

This section introduces the basic concepts of annotation and how to customize annotation.
This article describes how to dynamically parse annotation during runtime.
As mentioned above, annotation is only the additional information appended to the code. annotation itself will not affect the code execution.

In this case, what role does annotation play?
1. Compilation tools or other tools can automatically generate configuration files, documents, and other external files based on the annotation information attached to the code.
For example, Sun provides the apt tool (annotation processing tool), which is a command line tool that can handle annotation. APT provides source code-level parsing during compilation, new source code and other files can be generated during parsing, And the generated source code can also be compiled.
2. Other programs can dynamically parse annotation information in the program to be executed at runtime, and perform different operations according to the appended annotation information.
For example, the ejb3 specification widely uses the Annotation Feature. For example, if the @ stateless annotation is specified for the class in pojo, The EJB container registers the pojo as a stateless Session Bean based on this annotation. Ejb3 uses annotation to greatly simplify the EJB development and configuration process. We will introduce the principles and usage of EJB annotation in other articles.

This article uses a simple example to illustrate how to dynamically parse annotation at runtime. We will introduce the apt tool in other recent articles.

For example, we have defined myannotation3 annotation:
Myannotation3.java

Java code
  1. Package com. Test. annotation;
  2. Import java. Lang. annotation. annotation;
  3. Import java. Lang. annotation. inherited;
  4. Import java. Lang. annotation. retention;
  5. Import java. Lang. annotation. retentionpolicy;
  6. @ Retention (retentionpolicy. runtime)
  7. Public @ interface myannotation3 {
  8. Public String Value ();
  9. Public String [] multivalues ();
  10. Int number () default 0;
  11. }

 

A comment named myannotation3 is defined above.
Let's define another getmyannotation class, which uses myannotation3 annotation:
Getmyannotation. Java:

 

 

Java code
  1. Package com. Test. annotation. test;
  2. Import java. Lang. annotation. annotation;
  3. Import java. Lang. Reflect. field;
  4. Import java. Lang. Reflect. method;
  5. Import javax. EJB. EJB;
  6. Import javax. Naming. initialcontext;
  7. Import javax. Naming. namingexception;
  8. Import com. Test. annotation. myannotation3;
  9. // Append the myannotation3 annotation to the getmyannotation class
  10. @ Myannotation3 (value = "class getmyannotation", multivalues = {"1", "2 "})
  11. Public class getmyannotation {
  12. // Append the myannotation3 annotation to the testfield1 attribute
  13. @ Myannotation3 (value = "Call testfield1", multivalues = {"1"}, number = 1)
  14. Private string testfield1;
  15. // Append the myannotation3 annotation to the testmethod1 Method
  16. @ Myannotation3 (value = "Call testmethod1", multivalues = {"1", "2"}, number = 1)
  17. Public void testmethod1 (){
  18. }
  19. @ Deprecated
  20. @ Myannotation3 (value = "Call testmethod2", multivalues = {"3", "4", "5 "})
  21. Public void testmethod2 (){
  22. }
  23. }

In the above example, getmyannotation is very simple and does not have any functions in it. However, the myannotation3 annotations are added for class, field, and method declarations.
Next we will use the program testmyannotation3 to parse the myannotation3 annotation in getmyannotation.
Annotation parsing during runtime
Testmyannotation3.java

 

 

 

Java code
  1. Public class testmyannotation3 {
  2. Public static void main (string [] ARGs ){
  3. System. Out. println ("-- class annotations --");
  4. If (getmyannotation. Class. isannotationpresent (myannotation3.class )){
  5. System. Out. println ("[getmyannotation]. annotation :");
  6. Myannotation3 classannotation = getmyannotation. Class
  7. . Getannotation (myannotation3.class );
  8. Printmyannotation3 (classannotation );
  9. }
  10. System. Out. println ("-- fields annotations --");
  11. Field [] fields = getmyannotation. Class. getdeclaredfields ();
  12. For (field: fields ){
  13. If (field. isannotationpresent (myannotation3.class )){
  14. System. Out. println ("[getmyannotation." + field. getname ()
  15. + "]. Annotation :");
  16. Myannotation3 fieldannotation = Field
  17. . Getannotation (myannotation3.class );
  18. Printmyannotation3 (fieldannotation );
  19. }
  20. }
  21. System. Out. println ("-- Methods annotations --");
  22. Method [] Methods = getmyannotation. Class. getdeclaredmethods ();
  23. For (method: Methods ){
  24. System. Out. println ("[getmyannotation." + method. getname ()
  25. + "]. Annotation :");
  26. If (method. isannotationpresent (myannotation3.class )){
  27. Myannotation3 methodannotation = Method
  28. . Getannotation (myannotation3.class );
  29. Printmyannotation3 (methodannotation );
  30. }
  31. }
  32. }
  33. Private Static void printmyannotation3 (myannotation3 annotation3 ){
  34. If (annotation3 = NULL ){
  35. Return;
  36. }
  37. System. Out. println ("{value =" + annotation3.value ());
  38. String multivalues = "";
  39. For (string value: annotation3.multivalues ()){
  40. Multivalues + = "," + value;
  41. }
  42. System. Out. println ("multivalues =" + multivalues );
  43. System. Out. println ("number =" + annotation3.number () + "}");
  44. }
  45. }

 

 

Output:

 

 

-- Class annotations --
[Getmyannotation]. annotation:
{Value = Class getmyannotation
Multivalues =, 1, 2
Number = 0}
-- Fields annotations --
[Getmyannotation. testfield1]. annotation:
{Value = call testfield1
Multivalues =, 1
Number = 1}
-- Methods annotations --
[Getmyannotation. testmethod1]. annotation:
{Value = call testmethod1
Multivalues =, 1, 2
Number = 1}
[Getmyannotation. testmethod2]. annotation:
{Value = call testmethod2
Multivalues =, 3, 4, 5
Number = 0}

Jdk1.5APIS related to annotation provided in later versions:

 

Java code
  1. Interface java. Lang. Reflect. annotatedelement {
  2. Boolean isannotationpresent (class <? Extends annotation> annotationclass );
  3. <T extends annotation> T getannotation (class <t> annotationclass );
  4. Annotation [] getannotations ();
  5. Annotation [] getdeclaredannotations ();
  6. }

 

 

This interface is used to obtain annotation information attached to classes, constructor, field, method, and package.
Jdk1.5 has implemented the annotatedelement interface for several classes related to this:

 

Therefore, the reflection function can be used to dynamically parse the attached annotation information in the program.
Summary:
This article provides an example to illustrate how to dynamically parse annotation. You can use the Annotation Feature of Java to complete more complex functions.

 

Related Article

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.