Some rules written by custom annotation classes:
1. The annotation type is defined as @interface, and all annotation automatically inherit the Java.lang.Annotation interface and can no longer inherit other classes or interfaces.
2. Parameter members can only be decorated with public or default
3. Parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string, Enum, Class, annotations and other data types. As well as these types of arrays.
4. To get the annotation information for a class method and a field, you must get the annotation object through the Java reflection technique, because you have no other way to get the annotation object
5. Annotations can also have no members defined, but this makes the annotations useless.
When customizing the annotation class, you can specify the target (class, Method, field, constructor, and so on), the lifetime of the annotation (runtime, class file, or source code is valid), whether the annotations are included in the Javadoc and whether the subclasses are allowed to inherit annotations from the parent class, as follows:
[email protected] represents the annotation target, and 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
[Email protected] indicates the lifetime of the annotation, and 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
[Email protected] indicates that the note is included in the Javadoc
4. @Inherited indicates that subclasses are allowed to inherit annotations from the parent class
Source
1 Myannotationclass.java
[Java]View PlainCopy
- Package com.java.annotation;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- /**
- * Class annotations
- * */
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.type)
- Public @interface Myannotationclass {
- Public String msg ();
- }
2 Myannotationmethod.java
[Java]View PlainCopy
- Package com.java.annotation;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- /**
- * Method Annotations
- **/
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.method)
- Public @interface Myannotationmethod {
- Public String Common ();
- }
3 Myannotationfield.java
[Java]View PlainCopy
- Package com.java.annotation;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.field)
- Public @interface Myannotationfield {
- Boolean request ();
- }
4 Myannotationdemo.java
[Java]View PlainCopy
- Package com.java.annotation;
- @MyAnnotationClass (msg = "This is a class annotation")
- Public class Myannotationdemo {
- Public Myannotationdemo () {
- }
- Public Myannotationdemo (String text) {
- this.text = text;
- }
- @MyAnnotationMethod (common = "This is a method annotation")
- public Void Method () {
- }
- @MyAnnotationField (Request = true)
- private String text;
- }
5 Myannotationtest.java
[Java]View PlainCopy
- Package com.java.annotation;
- Import Java.lang.reflect.Field;
- Import Java.lang.reflect.Method;
- Public class Myannotationtest {
- public static void Main (string[] args) {
- Myannotationdemo demo = new Myannotationdemo ("Hello Rollen");
- Myannotationclass Annotationclass = Demo.getclass (). Getannotation (Myannotationclass. Class);
- System.out.println (Annotationclass.msg ());
- method = null;
- try {
- method = Demo.getclass (). GetMethod ("method",new class[0]);
- } catch (SecurityException e) {
- E.printstacktrace ();
- } catch (Nosuchmethodexception e) {
- E.printstacktrace ();
- }
- Myannotationmethod Annotationmethod = method.getannotation (myannotationmethod. Class);
- System.out.println (Annotationmethod.common ());
- Field field = null;
- try {
- field = Demo.getclass (). Getdeclaredfield ("text");
- } catch (SecurityException e) {
- E.printstacktrace ();
- } catch (Nosuchfieldexception e) {
- E.printstacktrace ();
- }
- Myannotationfield Annotationfield = field.getannotation (Myannotationfield. Class);
- System.out.println (Annotationfield.request ());
- }
- }
Run results
:
csdn:http://download.csdn.net/detail/haishu_zheng/9569169
Github:https://github.com/zhenghaishu/annotationdemo
Java Custom Annotations