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?
- Using the DK metadata annotation:retention
- @Retention (Retentionpolicy.runtime)
- Using the JDK's meta-data annotation:target
- @Target (Elementtype.method)
- Defines a tag comment that does not contain any member variables, that is, the metadata is not passed in
- Public @interface testable{
- }
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?
- public class MyTest {
- Use the @testable tag comment to specify that the method is testable
- @Testable
- public static void M1 () {
- }
- public static void M2 () {
- }
- Use the @testable tag comment to specify that the method is testable
- @Testable
- public static void M3 () {
- throw new RuntimeException ("Boom");
- }
- public static void M4 () {
- }
- Use the @testable tag comment to specify that the method is testable
- @Testable
- public static void M5 () {
- }
- public static void M6 () {
- }
- Use the @testable tag comment to specify that the method is testable
- @Testable
- public static void M7 () {
- throw new RuntimeException ("Crash");
- }
- public static void M8 () {
- }
- }
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?
- public class Testprocessor {
- public static void process (String clazz) throws classnotfoundexception{
- int passed=0;
- int failed=0;
- Traverse all methods of the Obj object
- For (Method m:class.forname (clazz). GetMethods ()) {
- If you include a @testable tag comment
- if (M.isannotationpresent (Testable.class)) {
- try{
- Call the M method
- M.invoke (NULL);
- Passed plus 1
- passed++;
- }catch (Exception ex) {
- System.out.printf ("Method" +m+ "Run failed, exception" +ex.getcause () + "\ n");
- failed++;
- }
- }
- }
- Statistical test Results
- SYSTEM.OUT.PRINTLN ("Total run:" + (passed+failed) + "method, where: \ n failed:" +failed+ ", A, \ n succeeded:" +passed+ "!\n");
- }
- }
Where the program's main program is as follows
[Java]View Plaincopyprint?
- public class Runtests {
- public static void Main (string[] args) throws exception{
- Handling MyTest Classes
- Testprocessor.process ("annotation. MyTest ");
- }
- }
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