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
- Package com. Test. annotation;
- Import java. Lang. annotation. annotation;
- Import java. Lang. annotation. inherited;
- Import java. Lang. annotation. retention;
- Import java. Lang. annotation. retentionpolicy;
- @ Retention (retentionpolicy. runtime)
- Public @ interface myannotation3 {
- Public String Value ();
- Public String [] multivalues ();
- Int number () default 0;
- }
A comment named myannotation3 is defined above.
Let's define another getmyannotation class, which uses myannotation3 annotation:
Getmyannotation. Java:
Java code
- Package com. Test. annotation. test;
- Import java. Lang. annotation. annotation;
- Import java. Lang. Reflect. field;
- Import java. Lang. Reflect. method;
- Import javax. EJB. EJB;
- Import javax. Naming. initialcontext;
- Import javax. Naming. namingexception;
- Import com. Test. annotation. myannotation3;
- // Append the myannotation3 annotation to the getmyannotation class
- @ Myannotation3 (value = "class getmyannotation", multivalues = {"1", "2 "})
- Public class getmyannotation {
- // Append the myannotation3 annotation to the testfield1 attribute
- @ Myannotation3 (value = "Call testfield1", multivalues = {"1"}, number = 1)
- Private string testfield1;
- // Append the myannotation3 annotation to the testmethod1 Method
- @ Myannotation3 (value = "Call testmethod1", multivalues = {"1", "2"}, number = 1)
- Public void testmethod1 (){
- }
- @ Deprecated
- @ Myannotation3 (value = "Call testmethod2", multivalues = {"3", "4", "5 "})
- Public void testmethod2 (){
- }
- }
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
- Public class testmyannotation3 {
- Public static void main (string [] ARGs ){
- System. Out. println ("-- class annotations --");
- If (getmyannotation. Class. isannotationpresent (myannotation3.class )){
- System. Out. println ("[getmyannotation]. annotation :");
- Myannotation3 classannotation = getmyannotation. Class
- . Getannotation (myannotation3.class );
- Printmyannotation3 (classannotation );
- }
- System. Out. println ("-- fields annotations --");
- Field [] fields = getmyannotation. Class. getdeclaredfields ();
- For (field: fields ){
- If (field. isannotationpresent (myannotation3.class )){
- System. Out. println ("[getmyannotation." + field. getname ()
- + "]. Annotation :");
- Myannotation3 fieldannotation = Field
- . Getannotation (myannotation3.class );
- Printmyannotation3 (fieldannotation );
- }
- }
- System. Out. println ("-- Methods annotations --");
- Method [] Methods = getmyannotation. Class. getdeclaredmethods ();
- For (method: Methods ){
- System. Out. println ("[getmyannotation." + method. getname ()
- + "]. Annotation :");
- If (method. isannotationpresent (myannotation3.class )){
- Myannotation3 methodannotation = Method
- . Getannotation (myannotation3.class );
- Printmyannotation3 (methodannotation );
- }
- }
- }
- Private Static void printmyannotation3 (myannotation3 annotation3 ){
- If (annotation3 = NULL ){
- Return;
- }
- System. Out. println ("{value =" + annotation3.value ());
- String multivalues = "";
- For (string value: annotation3.multivalues ()){
- Multivalues + = "," + value;
- }
- System. Out. println ("multivalues =" + multivalues );
- System. Out. println ("number =" + annotation3.number () + "}");
- }
- }
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
- Interface java. Lang. Reflect. annotatedelement {
- Boolean isannotationpresent (class <? Extends annotation> annotationclass );
- <T extends annotation> T getannotation (class <t> annotationclass );
- Annotation [] getannotations ();
- Annotation [] getdeclaredannotations ();
- }
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.