Package com.java.annotation;
Import java.lang.annotation.*;
/**
* Created by LW on 14-5-30.
* Custom Annotation *
/
@Documented
@Target (elementtype.method)
@Retention (retentionpolicy.runtime)
Public @interface MethodInfo {public
String Value () default "no description";
}
/* Four defined Data
@Target (elementtype.type) : Interface, class, enumeration, annotation
@Target (Elementtype.field) : Field , enumerated constants
@Target (Elementtype.method) : Method
@Target (Elementtype.parameter) : Method parameter
@Target ( Elementtype.constructor) : Constructor
@Target (elementtype.local_variable): local variable
@Target ( Elementtype.annotation_type): note
@Target (elementtype.package) : Package
@Retention ( Retentionpolicy.source) : Valid in the source file (that is, source file retention)
@Retention (retentionpolicy.class) : Valid in class file (that is, class reservation)
@Retention (retentionpolicy.runtime): Valid at run time (that is, run-time reservation)
@Inherited: Description The subclass can inherit the annotation from the parent class
@Document: Indicates that the annotation will be included in Javadoc.
Package com.java.annotation;
Import java.lang.annotation.Annotation;
Import Java.lang.reflect.Method;
/** * Created by LW on 14-5-30. * * public class Test {/** * get annotation parameter * * @param annotationclass Annotation class * @param annotationfield annotation class word
Segment name * @param aclass using the annotation's class name * @param methodname the method name using the annotation * @throws Exception * * public static void Getannotationpar (class Annotationclass, String Annotationfield, Class AClass, String methodname) thro
WS Exception {method Aclassmethod = Aclass.getmethod (methodname);
Annotation Annotation = aclassmethod.getannotation (Annotationclass);
Method method = Annotation.getclass (). Getdeclaredmethod (Annotationfield);
System.out.println (Method.invoke (annotation)); The public static void main (string[] args) throws Exception {Test.getannotationpar (Methodinfo.class, "Value",
Test2annotation.class, "testmyannotation");
} class Test2annotation { @MethodInfo (Value = "Custom Annotation @methodinfo") public void Testmyannotation () {}}
Source: Https://github.com/xiaohulu/util_xiaohulu/tree/master/src/com/java/annotation