Reference
Concept: Java provides a way and method for associating any information and any metadata with elements in the original program
JDK built-in system annotations :
@Override used to decorate the method that this method overrides the parent class;
@Deprecated used to modify methods that are obsolete;
@Suppvisewarnings is used to notify the Java compiler that a specific compilation warning is suppressed.
annotations are divided by the operating mechanism
SOURCE annotations: Annotations only exist in the source code, compiled into a. class file will not exist;
Compile-time annotations: annotations exist in both the source code and the. class file (example: JDK comes with annotations);
Runtime Annotations: It also works in the run phase, even with annotations (autowired) that affect the running logic;
meta-Annotations for custom annotations :
- @Target-Scope (constructor (construction method Declaration), Field (field Declaration), local_variable (local variable Declaration), method (methods Declaration), package (packet Declaration), parameter (parameter declaration), Type (class, interface Declaration))
- @Retention-The Life cycle (source: is displayed only in the source code and is discarded at compile time. Class: At compile time, it is recorded in class and ignored at runtime. Runtime: Present at run time, can be read by reflection)
- inherited-identification annotations (subclass inheritance allowed)
- documented-Generating Javadoc
- If the member has only one, must be value, this parameter assignment can not write value=xxx
Members are declared with no arguments and no exception. The member type is restricted, and the legal type includes the original type and String,class,annotation, enumeration.
The annotation is used above, and the method of obtaining the annotation information in the program is reflection .
Class CLS = Class.forName ("");//Use class loader to load class
Cls.isannotationpresent (Xx.class);//Determine if the CLS has an XX annotation on this class, and find annotations on the class
xx a = (xx) cls.getannotation (xx.class);//Get Annotations on it
Attached below is an example of how the framework uses annotations to describe parameters.
Structure diagram
A.java
Package Javabasic;Import Java. Lang. Annotation. Documented;Import Java. Lang. Annotation. ElementType;Import Java. Lang. Annotation. Retention;Import Java. Lang. Annotation. Retentionpolicy;Import Java. Lang. Annotation. Target;@Target ({ElementType. PARAMETER, ElementType. METHOD}) @Retention (Retentionpolicy. RUNTIME) @Documentedpublic @interface A {String value ();String name () default"Bingone";int age () default -;}
B.java
Package Javabasic;Import Java. Lang. Annotation. Documented;Import Java. Lang. Annotation. ElementType;Import Java. Lang. Annotation. Retention;Import Java. Lang. Annotation. Retentionpolicy;Import Java. Lang. Annotation. Target;@Target ({ElementType. PARAMETER, ElementType. METHOD}) @Retention (Retentionpolicy. RUNTIME) @Documentedpublic @interface B {String value ();String name () default"Bingone";}
Dataclass.java
package javaBasic;publicclass DataClass { public String name; publicint age; publicDataClassint age) { super(); this.name = name; this.age = age; }}
Runclass.java
package javaBasic;publicclass RunClass { publicstaticvoidrun(@A("str") String str,@B("age")int age){ System.out.println("In run Method str:""age:" + age); }}
Testclass.java
PackageJavabasic;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.lang.annotation.Annotation;ImportJava.lang.reflect.Method;/** * Pass parameters via annotations * @author gg_gogoing * */ Public class testclass { Public Static void Parsemethod(DataClass data,object obj,string mthname) {//Verify if there are annotations //If there are two methods: //1. At compile time already have the corresponding table, look up the table can //2. The following traversal method. if(objinstanceofRunclass) {String str =NULL;intAge =0; Method [] methods = (method[]) Obj.getclass (). GetMethods (); for(Method method:methods) {if(Method.getname (). Equals (Mthname)) {annotation[][] annotations = method.getparameterannotations (); for(annotation[] tt:annotations) { for(Annotation T:tt) {if(tinstanceofA) {str = data.name; }Else if(tinstanceofB) {age = Data.age; }}} runclass.run (str, age); } } } } Public Static void Main(string[] args)throwsException, RuntimeException {//Main method //Do not pass parameters directly, but instead pass data from a classDataClass DC =NewDataClass ("Gg_gogoing", -); Parsemethod (DC,NewRunclass (),"Run"); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java annotations Getting Started instance && annotation Pass