Java annotation summary:
There are three levels of annotations in java:
1. built-in Annotation
2. Declare annotations
3. Annotations
I. built-in annotations include:
1. @ Deprecated: This method or class is no longer recommended
2. @ Override: This method is inherited from the parent class/interface.
3. @ SafeVarargs: a warning message is generated if the parameter is of a non-specific type.
4. @ SuppressWarnings: tells the compiler to ignore the specified warning without warning after compilation.
Ii. Declaration annotation:
It is some meta information appended to the code. It is used for parsing and using some tools during compilation and runtime, and serves as a description and configuration function.
Annotations do not and cannot affect the actual logic of the Code. They only play an auxiliary role. Included in the java. lang. annotation package.
1. Mark annotation: This annotation does not have any elements and is used to notify other developers-the method to be annotated has not yet been implemented: as shown in Figure
@interface isNotImplements{}public class AnnoatationDemo{@isNotImplementspublic void isNotImplementFunction(){}}
2. Single value annotation: This type of annotation only receives single value type, data members are specified by value, such:
@interface Task1{String value();}@interface Task2{int value();}public class AnnoatationDemo{@isNotImplementspublic void isNotImplementMethod(){}@Task1(value = "singlevalu")public void task1Method(){}@Task2(value = 0)public void task2Method(){}}
3. multi-value annotation: This type of annotation can receive multiple values. The description of these values is defined by yourself, for example:
1. The first is the custom annotation:
package cn.veji.hibernate.po;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Privilege {String[] value();}
2. Define the annotation class:
package cn.veji.hibernate.po;package cn.veji.hibernate.po;public class TestPrivilege {@Privilege( { "a" })public void a() {}@Privilege( { "b" })public void b() {}@Privilege( { "c" })public void c() {}}
3. Read the help class of the annotation value:
Package cn. veji. hibernate. po; package cn. veji. hibernate. test; import java. lang. annotation. annotation; import java. lang. reflect. method; import java. util. hashMap; import java. util. map; import cn. veji. hibernate. po. privilege; import cn. veji. hibernate. po. testPrivilege; public class AnnotationUtil {public static AnnotationUtil anno = null; public static AnnotationUtil getInstance () {if (anno = null) {anno = new AnnotationUtil ();} return anno ;} /*** read Annotation value ** @ param annotationClasss process Annotation class name * @ param annotationField process Annotation class attribute name * @ param className process Annotation class name * @ return *@ throws Exception */@ SuppressWarnings ("all ") public Map
LoadVlaue (Class annotationClasss, String annotationField, String className) throws Exception {System. out. println ("processing Annotation class name =" + annotationClasss. getName (); System. out. println ("processing Annotation class property name =" + annotationField); System. out. println ("Name of the call class for processing Annotation =" + className); Map
Map = new HashMap
(); Method [] methods = Class. forName (className ). getDeclaredMethods (); for (Method method: methods) {if (method. isAnnotationPresent (annotationClasss) {Annotation p = method. getAnnotation (annotationClasss); Method m = p. getClass (). getDeclaredMethod (annotationField, null); String [] values = (String []) m. invoke (p, null); for (String key: values) {System. out. println ("annotation value =" + key); map. put (key, key) ;}} System. out. println ("map quantity =" + map. size (); return map;} public static void main (String [] args) throws Exception {AnnotationUtil. getInstance (). loadVlaue (Privilege. class, "value", TestPrivilege. class. getName ());}}
4.Execution result:
Processing Annotation class name = cn. veji. hibernate. po. privilege processes Annotation class attributes === value processes Annotation call class names === cn. veji. hibernate. po. testPrivilege annotation value === c annotation value === a annotation value === bmap quantity === 3
3. Add annotation for Annotation
1. Tartget annotation: This annotation is used to specify which elements in the code can use annotation of defined types, such:
@ Target (ElementType. METHOD) // indicates that the annotation can only act on the METHOD @ interface Task1 {}2. Retention annotation: This annotation is used to set the visibility of the annotation (visible to the compiler, visible to the tool, and visible to the runtime)
@ Retention (RetentionPolicy. RUNTIME) // visible at RUNTIME @ interface Task2 {}@ Retention (RetentionPolicy. CLASS) // work visible @ interface Task3 {}@ Retention (RetentionPolicy. SOURCE) // visible to the compiler @ interface Task4 {}