Download java and java
Annotaton (annotation) was introduced after jdk5.0. Many mainstream frameworks support annotation.
Annotations are generally used to create documents, track dependencies in code, and perform compilation checks. Exist with @ annotation name
Common annotations in jdk include:
@ Override overwrites the method in the superclass
@ Deprecated discarded code
@ SuppressWarings warning
Some other annotations are used to create custom annotations.
Custom Annotation
@ Target application annotation location: field, method, class ..
@ Retemtop usage level (runtime, class, source)
@ Documente whether it is included in javadoc
@ Inherited allows subclass to inherit the annotation in the parent class
The purpose of defining an annotation is to use it. The most important part of the annotation is the processing of the annotation, which will involve the annotation processor. In principle, the annotation processor obtains the annotation information on the checked method through the reflection mechanism, and then performs specific processing based on the value of the annotation element.
Based on the reflection mechanism we learned in the previous section
Let's define and use an annotation.
1 Define annotation class
importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target; @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interfaceUseCase { public String name(); public String desciption() default "no description";}
2. Define User classes
Package com. bjpower. node. spring; public class User {private String userName; private String password; @ UseCase (name = "password", desciption = "password") public String getPassword () {return password ;} public void setPassword (String password) {this. password = password;} @ UseCase (name = "userName", desciption = "userName") public String getUserName () {return userName;} public void setUserName (String userName) {this. userName = userName;} public User () {} public User (String username) {this. userName = username ;}}
3. Obtain annotation Methods
Public static void getrefect (String clas) {try {Class <?> Cls = Class. forName (clas); // obtain the Class Object Method [] methods = cls. getDeclaredMethods (); // obtain all methods of the class for (Method method: methods) {// determine whether a specified annotation exists in the method. if yes, obtain the annotation and print if (Method. isAnnotationPresent (UseCase. class) = true) {UseCase usercase = method. getAnnotation (UseCase. class); // obtain the annotation class System. out. println ("user information:" + usercase. name () + "User Description:" + usercase. desciption () ;}} catch (Exception e) {e. printStackTrace ();}}
4 client Test
public class Client {public static void main(String[] args) {getrefect("com.bjpower.node.spring.User");}}
Test results:
User information: password user Description: password
User information: userName user Description: User Name
Summary:
Annotation parsing uses reflection and dynamic parsing. Compared with annotation, annotation has its own unique advantages. we can specifically describe some fields or methods to display page information.