Java reflection-annotation based on the framework and java reflection framework Annotation

Source: Internet
Author: User

Java reflection-annotation based on the framework and java reflection framework Annotation
1. Concept

First, you must understand the functions of annotations. The function of annotation is simple and clear. It is a mark. If an annotation is added to a program, it is equivalent to marking the program (for example, class or method). If no annotation is added, it indicates that no markup is added. In the future, we can get through reflection in other programs, which classes are annotated, and which classes are not annotated. In this way, the two types can be processed differently.

For example, we can define an annotation @ SpringClazz in Spring. If the annotation is added to the class, it indicates that the class is managed by the Spring container. When this class is used, our factory will return the [proxy object] of this target class ]. Of course, if this annotation is not added, this class is not managed by the Spring container, so our factory directly returns the object of the target class. (For details, see the previous article http://blog.csdn.net/wang379275614/article/details/47112195)

The above just defines a simple annotation, annotation and attribute. Next we will briefly and systematically introduce the annotation in java.


2. System Annotation

Under the java. lang package, we can see three annotations provided by jdk by default:


  


3. Metadata Annotation

That is, the annotation is used in our annotation definition. Similar to the metadata that we often say in data tables. I will not talk about the concept more. Here we will introduce two important meta annotations @ Retention and @ Target.

Ø @ Retention (specify the time when the annotation works: source code, compile time, and runtime)

Ø @ Target (specify the scope of the annotation: class [Type], method ....)

For example, the @ Override annotation is defined as follows:


  


@ Retention specifies the time when the annotation works, which indicates the stage at which we will check the corresponding target annotation. For example, the @ Override annotation must start to detect the source code. If we do not comply with the @ Override specification when writing the code, we will be prompted with an incorrect usage error. Its value isRetentionPolicyEnumeration constants include the following types: (you do not need to remember to find an annotation in the jdk api documentation)


  


@ Target: Specifies the scope of the annotation. If we specify the scope of the annotation as a method, we cannot add the annotation to other types of elements. For example, if we add the annotation to a class, an error is returned. Its value isElementTypeEnumerated constants include the following types:


  


4. annotation attributes

For example, we have defined an annotation @ Tsinghua to mark the students of Tsinghua University, but the program also needs to know which department of Tsinghua University you are from, we can add a department attribute to the annotation @ Tsinghua. For example, @ Tsinghua (department = "Math") indicates that you are a student in the Mathematics department of Tsinghua University.


5. Custom Annotation

View the Code directly:

5.1 custom annotation @ TGBAnnotation:
Package com. tgb. testAnnotation; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; @ Target ({ElementType. METHOD, ElementType. TYPE}) @ Retention (RetentionPolicy. RUNTIME) public @ interface TGBAnnotation {// annotation attribute 1 String color () default "blue"; // annotation attribute 2int [] score () default {1, 2, 3 };}

The annotation above has two annotation attributes; it works at runtime; it can be added to two elements: class and method.

5.2 Test class AnnotationTest:
Package com. tgb. testAnnotation; @ TGBAnnotation (color = "class ---> yellow", score = {10, 20}) public class AnnotationTest {public static void main (String [] args) throws Exception {// obsolete method, which is dangerous, but can also use System. runFinalizersOnExit (true); sayHi () ;}/ ** when we upgrade the version, we do not want to delete the previous code or comment out a lot of code, you can add the @ Deprecated annotation */@ Deprecatedpublic static void sayHello () {System. out. println ("Hello world! ");}/*** Test annotation */@ TGBAnnotation (color =" method ---> red ") public static void sayHi () throws Exception {// If the AnnotationTest class has annotation ItCastAnnotationif (AnnotationTest. class. isAnnotationPresent (TGBAnnotation. class) {// obtain the ItCastAnnotation annotation instance on the AnnotationTest class, and then obtain the attribute TGBAnnotation annotation = AnnotationTest on the annotation. class. getAnnotation (TGBAnnotation. class); TGBAnnotation annotation2 = AnnotationTest. class. getMethod ("sayHi", null ). getAnnotation (TGBAnnotation. class); System. err. println ("annotation ---->" + annotation. color (); // output: class ---> yellowSystem. err. println ("annotation2 --->" + annotation2.color (); // output: method ---> redint [] score = annotation. score (); for (int I: score) {System. out. println (I); // output in sequence: 10, 20 }}}}

6. Summary

The function of annotation is simple and clear. It is a mark. For example, if a Spring annotation is added to identify the class managed by the Spring container, the [proxy object] is returned when getBean is used. If this annotation is not added, the class is not added to the Spring container, getBean directly returns the target object ]. In addition, combined with the jdk's default built-in annotations and what Zhang Xiaoxiang said ,. the content in the class file is not a "bytecode". When the class Loader. when a class file is loaded into the memory, it will also handle some issues (such as security checks). After processing the binary data loaded into the memory, it is the bytecode.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.