Java Fundamentals Consolidation Note (6)-annotations
- Java Fundamentals Consolidation Note 6-annotations
- Application structure diagram of annotations
- Meta annotations
- Custom annotations
- Sample code
- Resources
Annotations (Annotation), also called Meta data. A description of the code level. It is a feature introduced in JDK1.5 and later versions, with classes, interfaces, and enumerations at the same level. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on, to explain these elements, comments.
Api
Package Java.lang.annotation
Application structure diagram of annotations
Call/struct Relationship:a<–b<–c
A,b,c explained as follows:
A: Annotation Class
@interface A{}
B: Class with "annotation class" applied
@AClass B{}
C: Class for reflection operation on "class with annotated class applied"
Class C{ publicvoidf(){ B.class.isAnnotationPresent(A.class); A a = B.class.getAnnotion(A.class); }}
Meta annotations
The role of meta annotations is to be responsible for annotating other annotations. The four meta-annotations were:@Target,@Retention,@Documented,@Inherited
Indicates at what level the annotation information is saved. Optional parameter values in the enumeration type RetentionPolicy , including RetentionPolicy.SOURCE , RetentionPolicy.CLASS (default), corresponding to RetentionPolicy.RUNTIME : Java source file –>class file –> byte code in memory
RetentionPolicy.SOURCE 注解将被编译器丢弃 RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃RetentionPolicy.RUNTIME
Indicates where the annotation is used, and the possible values are in the enumeration class ElemenetType , including
ElemenetType.CONSTRUCTOR 构造器声明 ElemenetType.FIELD 域声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE 局部变量声明 ElemenetType.METHOD 方法声明 ElemenetType.PACKAGE 包声明 ElemenetType.PARAMETER 参数声明 ElemenetType.TYPE
Include this note in Javadoc, which means that the annotation is extracted as a document by the Javadoc tool. The content in the Doc document differs depending on the content of the information for this annotation. Equivalent to @see,@param wait
Allow subclasses to inherit annotations from parent class
Custom annotations
@interfaceWhen using custom annotations, the interface is automatically inherited java.lang.annotation.Annotation and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interfaceused to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.
To define the annotation format:
public @interface 注解名 {定义体}
Supported data types for annotation parameters:
1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. Arrays of all types above
Sample code
Reference to the end of the "reference" in the "Java annotations of the major role and how to use the method (end)"
The following example is an expanded version of the a<–b<–c mentioned above. A note was customized @A , then annotated in Class B @A , and finally, in Class C, the @A information in the reflection read was used
package com.iot.annotation;import java.lang.annotation.*;/** * Created by brian on 2016/2/20. */@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})@Retention(RetentionPolicy.RUNTIME)public @interface A { String name(); intdefault0; Class<Long> gid();}
PackageCom.iot.annotation;ImportJava.util.HashMap;ImportJava.util.Map;/** * Created by Brian on 2016/2/20. * *@A(name="Type", Gid=long.class)//class annotations Public class B { @A(name="param", id=1, Gid=long.class)//class member annotations PrivateInteger age;@A(name="Construct", id=2, Gid=long.class)//Construction method Annotations Public B(){}@A(name="Public Method", id=3, Gid=long.class)//class method annotations Public void a(){ }@A(name="protected method", id=4, Gid=long.class)//class method annotations protected void b() {map<string,string> m =NewHashmap<string,string> (0); }@A(name="Private Method", id=5, Gid=long.class)//class method annotations Private void C() {map<string,string> m =NewHashmap<string,string> (0); } Public void b(Integer a) { }}
PackageCom.iot.annotation;ImportJava.lang.annotation.Annotation;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Method;/** * Created by Brian on 2016/2/20. * * Public class C { /** * Simply print out the class annotations used in class B * This method only prints annotations of type types * @throws classnotfoundexception */ Public Static void parsetypeannotation()throwsclassnotfoundexception{Class clazz = Class.forName ("com.iot.annotation.b"); annotation[] annotations = clazz.getannotations (); for(Annotation annotation:annotations) {A A = (a) annotation; System.out.println ("id ="+a.id () +"; name ="+a.name () +"; gid ="+a.gid ()); } }/** * Simply print out the method annotations used in Class B * This method only prints the annotation for the type of method * / Public Static void parsemethodannotation() {method[] methods = B.class.getdeclaredmethods (); for(Method method:methods) {/ * * To determine if there are annotations of the specified annotation type in the method */ BooleanHasannotation = Method.isannotationpresent (A.class);if(hasannotation) {/ * * Returns the specified type annotation of the method based on the annotation type */A annotation = method.getannotation (A.class); System.out.println ("method ="+ method.getname () +" ; id = "+ annotation.id () +" ; Description = "+ annotation.name () +"; Gid= "+ Annotation.gid ()); } } }/** * Simply print out the method annotations used in Class B * This method only prints the annotation for the type of method * / Public Static void parseconstructannotation() {constructor[] constructors = b.class.getconstructors (); for(Constructor constructor:constructors) {/ * * Determine if there are annotations of the specified annotation type in the construction method * / BooleanHasannotation = Constructor.isannotationpresent (A.class);if(hasannotation) {/ * * Returns the specified type annotation of the method based on the annotation type */a annotation = (a) constructor.getannotation (A.class); System.out.println ("constructor ="+ constructor.getname () +" ; id = "+ annotation.id () +" ; Description = "+ annotation.name () +"; Gid= "+annotation.gid ()); } } } Public Static void Main(string[] args)throwsclassnotfoundexception {parsetypeannotation (); Parsemethodannotation (); Parseconstructannotation (); }}
Resources
- Some important functions of Java annotations and how to use them (end)
- Discussion on the alternative Package-info.java file
- In-depth understanding of Java: Annotations (Annotation) Getting Started with custom annotations
Author @brianway More articles: personal website | CSDN | Oschina
Java Fundamentals Consolidation Note (6)-annotations