Custom annotation in Java require @interface keywords and a few built-in annotation.
to use. notes are @target, @Retention, @Documented, @Inherited, for the following purposes:
@Target indicates where the annotation is used, the possible elemenettype parameters include:
Elemenettype.constructor Constructor Declaration
Elemenettype.field domain declarations (including enum instances)
Elemenettype.local_variable local variable declaration
Elemenettype.method Method declaration
elemenettype.package Package Declaration
Elemenettype.parameter parameter declaration
Elemenettype.type class, interface (including annotation type) or enum declaration
@Retention indicates at what level the annotation information is saved. The optional retentionpolicy parameters include:
Retentionpolicy.source annotations will be discarded by the compiler
Retentionpolicy.class annotations are available in the CLASS file, but are discarded by the VM
Retentionpolicy.runtime VMS will also retain annotations at run time, so the information of annotations can be read through the reflection mechanism.
@Documented include this annotation in Javadoc
@Inherited allow subclasses to inherit annotations from the parent class
The purpose of the @Documented is to allow this type of annotation information to be displayed on the JAVAAPI description document;
If not added, the information generated by this type will not be found when using Javadoc to generate the API documentation.
Another point, if you need to inherit the annotation data to the subclass, then you will use the @inherited this one.
Here's a custom two simple annatation. package annotation;
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.type)
@Retention (retentionpolicy.runtime) @Documented
Public @interface Description {
String value () default "no description";
}
package annotation;
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.method)
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface Name {
String originate ();
String community ();
}
package annotation;
@Description ("Javaeye, To is best")
public class Javaeyer {
@Name (originate = "Founder: Robbin", community = "Javaeye")
Public String Geteyename () {
return null;
}
@Name (originate = "Founder: Jiangnan white", community = "Springside")
Public String Getsidename () {
Return "Excuse me";
}
}
package annotation;
Import Java.lang.reflect.Method;
Import Java.util.HashSet; Import Java.util.Set;
public class Annotationtest {
@SuppressWarnings ("Unchecked")
public static void Main (string[] args) throws ClassNotFoundException {
Final String class_name = "annotation. Javaeyer ";
Class test = Class.forName (class_name);
Method[] methods = Test.getmethods ();
Boolean flag = Test.isannotationpresent (Description.class);
if (flag) {
Description des = (Description) test.getannotation (Description.class);
System.out.println ("description:" + des.value ());
System.out.println ("-----------------");
}
set<method> set = new hashset<method> ();
for (int i = 0; i < methods.length; i++) {
Boolean otherflag = Methods[i].isannotationpresent (Name.class);
if (Otherflag) {
Set.add (Methods[i]);
}
}
for (Method Method:set) {
Name name = Method.getannotation (Name.class);
System.out.println (Name.originate ());
SYSTEM.OUT.PRINTLN ("Community created:" + name.community ());
}
}
Java custom Annotation (downloaded from Baidu Library)