Annotation in the world of Java is overwhelming, have the time to write this simple annotations article, is about annotation primer article, I hope you can throw bricks, learn together ...
Don't talk nonsense, practice is the hard truth.
Part I: Learn about the default three annotation types from java1.5:
One is @override: it can only be used on the method, to tell others that this method is to rewrite the parent class.
One is @deprecated: it is recommended that others do not use the old API, the compile time will be used to generate a warning message, can be set in the program all the elements.
One is @suppresswarnings: This type can be used to temporarily turn off some warning message messages.
If you do not know the above three types of specific usage, you can Baidu or Google a bit, very simple.
Part II: Talk about the concept of annotation first, and then how to design their own annotation.
First, in the JDK's own java.lang.annotation package, open the following several source files:
1. source File Target.java
@Documented @Retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface Target { elementtype[] value (); }
One of the @interface is a keyword that must be defined as a @interface when designing a annotations, and not with a class or interface keyword (or if sun is a little stingy, It happened to be so like interface).
2. source File Retention.java
@Documented @Retention (retentionpolicy.runtime) @Target (Elementtype.annotation_type) public @Interface Retention { retentionpolicy value (); }
See here, everyone may be blurred, do not know what to say, don't worry, look down.
In the above file all use the Retentionpolicy,elementtype these two fields, you may guess this is two Java files. Indeed, the source code for these two files is as follows:
3. source file Retentionpolicy.java
Public enum Retentionpolicy { SOURCE, CLASS, RUNTIME }
This is an enum type with a total of three values, namely Source,class and RUNTIME.
Source represents the annotation type of information will only be retained in the program source code, if the source code is compiled, the annotation data will disappear, and will not be kept in the compiled. class file.
Class means that this annotation type of information is kept in the program source code, but also in the compiled. class file, which does not load this information into the virtual machine (JVM) when executed. . Note that when you do not set a retention value for a annotation type, the system default value is class.
The third, runtime, indicates that the information is preserved in the source code, the compiled. class file, and this information is loaded into the JVM when it is executed.
For example, if the retention in @override is set to source, the compilation succeeds without the information of these checks; instead, the retention inside the @Deprecated is set to runtime, This means that in addition to being warned at compile time which deprecated method is used, it is possible to find out whether the method is deprecated when executed.
4. source File Elementtype.java
Public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, local_variable, Annotation_type,package }
The ElementType inside the @target is used to specify which elements the annotation type can be used on. Description: Type (type), FIELD (attribute), method (methods), PARAMETER (parameter), CONSTRUCTOR (constructor), local_variable (local variable), Annotation_type,package (package), where the type (type) refers to the Class,interface,enum and ANNOTATION types that can be used.
In addition, from the source code of 1 can be seen, @Target oneself also used to declare themselves, can only be used on annotation_type.
If a annotation type does not indicate which elements the @target is used on, then it can be used on any element, where the element refers to the eight types above.
To give some examples of the right:
@Target (elementtype.method) @Target (Value=elementtype.method) @Target ( Elementtype.method,elementtype.constructor)
refer to Javadoc documentation
above for source files 1 and 2, both of which use the @documented,@ The purpose of the documented is to allow this type of annotation information to be displayed on the JAVAAPI documentation; If you do not add it, you will not find the information generated by this type when you use Javadoc to generate the API documentation.
Another point, if you need to inherit the annotation data to subclasses, then you will use the @inherited annotation type.
Part Three: The following design is the simplest example of a annotation, which is a common example of four files;
1, Description.java
package lighter.iteye.com; 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 (); }
Note: All annotation will automatically inherit the Java.lang.annotation interface, so it is no longer possible to inherit another class or interface.
The most important point is how the parameters in the annotation type are set:
First, it can only be decorated with public or default access rights. For example, String value (), where the method is set to Defaul default type.
Second, parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string,enum,class,annotations data types, As well as these types of arrays. For example, String value (), where the parameter member is string.
Thirdly, if there is only one parameter member, it is best to set the parameter name to "value", followed by parentheses. Example: the example above has only one parameter member.
2, Name.java
Packagelighter.iteye.com; Importjava.lang.annotation.Documented; ImportJava.lang.annotation.ElementType; Importjava.lang.annotation.Retention; ImportJava.lang.annotation.RetentionPolicy; ImportJava.lang.annotation.Target; //Note that the @target here are different from the @description, and the parameter members are different .@Target (Elementtype.method) @Retention (retentionpolicy.runtime) @Documented Public@InterfaceName {String originate (); String community (); }
3, Javaeyer.java
Packagelighter.iteye.com; @Description ("Javaeye, be the best software development Exchange Community") Public classJavaeyer {@Name (originate= "Founder: Robbin", community= "Javaeye") PublicString GetName () {return NULL; } @Name (Originate= "Founder: Jiangnan White", community= "Springside") PublicString getName2 () {return"Borrow two-bit ID one, write this example, please forgive me!"; } }
4. Finally, write a class that can run the Extract javaeyer information testannotation
Packagelighter.iteye.com; ImportJava.lang.reflect.Method; ImportJava.util.HashSet; ImportJava.util.Set; Public classtestannotation {/*** Author Lighter * Description: For specific're wrong about annotation API usage See Javadoc documentation*/ Public Static voidMain (string[] args)throwsException {String class_name= "Lighter.iteye.com.JavaEyer"; Class Test=Class.forName (class_name); Method[] Method=Test.getmethods (); BooleanFlag = Test.isannotationpresent (Description.class); if(flag) {Description des= (Description) test.getannotation (Description.class); System.out.println ("Description:" +Des.value ()); System.out.println ("-----------------"); } //Save all the methods of javaeyer that have been used to @name to set .Set<method> set =NewHashset<method>(); for(inti=0;i<method.length;i++) { BooleanOtherflag = Method[i].isannotationpresent (Name.class); if(Otherflag) Set.add (Method[i]); } for(Method m:set) {name Name= M.getannotation (Name.class); System.out.println (Name.originate ()); System.out.println ("Community created:" +name.community ()); } } }
5. Operation Result:
Description: Javaeye, the best software development Exchange Community
-----------------
Founder: Robbin
Community created: Javaeye
Founder: Jiangnan White
Community created: Springside
In addition, special in the Office statement, if reproduced please specify the source: http://lighter.iteye.com.
This article is reproduced in others,:-D
Java Annotations Annotation Initial Solution