Annotation Class:
1 packagecom.www.yanwu.Annotation;2 3 Importjava.lang.annotation.Documented;4 Importjava.lang.annotation.ElementType;5 Importjava.lang.annotation.Inherited;6 Importjava.lang.annotation.Retention;7 Importjava.lang.annotation.RetentionPolicy;8 Importjava.lang.annotation.Target;9 Ten one /** a * @ClassName: Demo2 - * @Description: Custom annotations - * @authorHarvey the * @date July 27, 2017 pm 9:42:21 - * - */ - @Inherited + @Documented -@Target (value={elementtype.type,elementtype.method}) + @Retention (retentionpolicy.runtime) a public@InterfaceDemo2 { atString Studentname ()default""; - intAge ()default0; - intID ()default-1; -}View Code
@Target annotations, Scope of action
@Retention annotations can provide a retention policy for annotations to the compiler when defining annotations (without the default class level).
The class compiler will record annotations in the class file, but the VM does not need to keep annotations at run Time. the runtime compiler records annotations in the class file, and the VM retains comments at run time, so it can be read in a reflective manner. The comment that the SOURCE compiler will Discard.
Test Note class:
1 packagecom.www.yanwu.Annotation;2 3 Importjava.util.ArrayList;4 Importjava.util.Date;5 Importjava.util.List;6 /** 7 * @ClassName: Demo18 * @Description: Test9 * @authorHarveyTen * @date July 27, 2017 pm 9:42:21 one * a */ -@Demo2 (age=100,id=20,studentname= "hello") - public classDemo1 { the - @Override - publicString toString () { - //TODO auto-generated Method Stub + return Super. toString (); - } + a@Demo2 (age=10,id=2,studentname= "harvey") at public voidtest001 () { -System.out.println ("hello"); - } -@SuppressWarnings ("all") - public Static voidmain (string[] Args) { -Date d=NewDate (); in //D.parse ("the"); - //test001 (); to System.out.println (d); +List list=NewArrayList (); - } the * $}View Code
Parsing annotations:
1 packagecom.www.yanwu.Annotation;2 3 4 Importjava.lang.annotation.Annotation;5 Importjava.lang.reflect.Method;6 7 /** 8 * @ClassName: Demo39 * @Demo3: TODO (here's A word describing the role of this class)Ten * @authorHarvey one * @date July 27, 2017 pm 9:42:58 a * - */ - public classDemo3 { the public Static voidTest ()throwsClassNotFoundException { - - } - + public Static voidMain (string[] Args)throwsClassNotFoundException { - /* + * 1. Load class with class loader a * class.forname ("class name String") (note: class name string must be full name, package name + class Name) at */ -Class C = Class.forName ("com.www.yanwu.Annotation.Demo1"); - - //2. Determine if there is an annotation on the class and get an instance of the annotation above the class - if(c.isannotationpresent (Demo2.class)){ -Demo2 d = (Demo2) c.getannotation (Demo2.class); in System.out.println (d.studentname ()); - System.out.println (d.age ()); to System.out.println (d.id ()); + } - the //3. Determine if there is an annotation on the method and get an example of the method above annotated *System.out.println ("==========================="); $method[] ms =C.getmethods (); Panax Notoginseng for(Method Method:ms) { - if(method.isannotationpresent (Demo2.class)){ theDemo2 d = (Demo2) method.getannotation (Demo2.class); + System.out.println (method.getname ()); a System.out.println (d.studentname ()); the System.out.println (d.age ()); + System.out.println (d.id ()); - } $ } $ //Another method for interpreting annotations on methods -System.out.println ("==========================="); - for(Method Method:ms) { theannotation[] as =method.getannotations (); - for(Annotation Annotation:as) {Wuyi if(annotationinstanceofDemo2) { the System.out.println (((Demo2) annotation). ID ()); - System.out.println (((Demo2) annotation). studentname ()); wu System.out.println (((Demo2) annotation). Age ()); - } about } $ } - -System.out.println ("hello"); - } a + the}View Code
Java Custom Annotations