Java Annotation initial solution, javaannotation
Annotation is unfolding in the world of java. If you are free to write this simple annotations article, it is an article about Annotation entry. I hope you can give it a brick, learning Together ......
Practice is the final principle.
Part 1: Learn about the default three annotation types starting from java1.5:
One is @ Override: it can only be used on methods. It is used to tell others that this method is used to rewrite the parent class.
One is @ Deprecated. It is recommended that you do not use the old API. Warnings are generated during compilation and can be set to all elements in the program.
One is @ SuppressWarnings: this type can temporarily disable some warning messages.
If you do not know the specific usage of the above three types, you can use baidu or google, which is very simple.
Part 2: introduce the concept of annotation. Let's talk about how to design your own annotation.
First, open the following source files in the java. lang. annotation package that comes with jdk:
1. source file Target. java
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
@ Interface is a keyword. When designing annotations, you must define a type as @ interface instead of using the class or interface keyword (Do you think sun is a bit stingy, just like the interface ).
2. source file Retention. java
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
You may be confused about what you are talking about. Don't worry. Let's look down.
The above files all use the RetentionPolicy and ElementType fields. You may guess these two java files. Indeed, the source code of these two files is as follows:
3. source file RetentionPolicy. java
public enum RetentionPolicy { SOURCE, CLASS, RUNTIME }
This is an enum type and has three values: SOURCE, CLASS, and RUNTIME.
SOURCE indicates that the Annotation type information will only be stored in the program SOURCE code. If the SOURCE code is compiled, the Annotation data will disappear and will not be stored in the compiled data. class file.
ClASS indicates that the Annotation type information is stored in the program source code and compiled. in the class file, this information is not loaded into the Virtual Machine (JVM) during execution. note that when you do not set a Retention value of the Annotation type, the default value is CLASS.
The third is RUNTIME, indicating that information is retained in the source code and compiled. class files. During execution, this information is loaded into the JVM.
For example, if the Retention in @ Override is set to SOURCE, the Retention in @ Deprecated is not required after compilation is successful. On the contrary, the Retention in @ Deprecated is set to RUNTIME, in addition to warning which Deprecated method is used during compilation, you can check whether the method is Deprecated during execution.
4. source file ElementType. java
public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE }
@ Target the ElementType is used to specify which elements the Annotation type can use. description: TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE. The TYPE can be used in the Class, Interface, Enum, and Annotation types.
In addition, we can see from the source code of 1 that @ Target uses itself to declare itself and can only be used on ANNOTATION_TYPE.
If an Annotation type does not specify the elements on which @ Target is used, it can be used on any element. The elements here refer to the above eight types.
Here are a few correct examples:
@Target(ElementType.METHOD) @Target(value=ElementType.METHOD) @Target(ElementType.METHOD,ElementType.CONSTRUCTOR)
For more information, see javadoc documentation.
The source files 1 and 2 above use @ incluented. The purpose of @ incluented is to display the Annotation type information in the javaAPI instruction document, this type of generated information cannot be found when you use javadoc to generate an API document.
In addition, if you need to inherit the Annotation data to the subclass, the Annotation type @ Inherited will be used.
Part 3: The following describes how to design the simplest Annotation example. This example shares 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 cannot inherit other classes or interfaces.
The most important thing is how to set the parameters in the Annotation type:
First, only public or default access permissions can be modified. For example, String value (); here, the method is set to defaul default type.
Second, parameter members can only use eight basic data types: byte, short, char, int, long, float, double, and boolean, and data types such as String, Enum, Class, and annotations, and these types of arrays. for example, String value (); here the parameter member is String.
Third, if there is only one parameter member, it is best to set the parameter name to "value" with parentheses. For example, the preceding example only contains one parameter member.
2. Name. 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; // note the difference between @ Target and @ Description, and the parameter members are different @ Target (ElementType. METHOD) @ Retention (RetentionPolicy. RUNTIME) @ Documented public @ interface Name {String originate (); String community ();}
3. JavaEyer. java
Package lighter.iteye.com; @ Description ("javaeye, the best software development and communication community") public class JavaEyer {@ Name (originate = "Founder: robbin", community = "javaEye ") public String getName () {return null;} @ Name (originate = "Founder: Jiangnan White", community = "springside") public String getName2 () {return "borrow two IDs for one use. Please forgive me for writing this example! ";}}
4. Finally, write a class TestAnnotation that can run to extract JavaEyer information.
Package lighter.iteye.com; import java. lang. reflect. method; import java. util. hashSet; import java. util. set; public class TestAnnotation {/*** author lighter * Description: For details about the usage of the Annotation API, see javaDoc Documentation */public static void main (String [] args) throws Exception {String CLASS_NAME = "lighter.iteye.com. javaEyer "; Class test = Class. forName (CLASS_NAME); Method [] method = 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 ("-----------------");} // save all the methods that use @ Name in JavaEyer to Set <Method> Set = new HashSet <Method> (); for (int I = 0; I <method. length; I ++) {boolean otherFlag = method [I]. isAnnotationPresent (Name. class); if (otherFlag) set. add (method [I]);} for (Method m: set) {Name = m. getAnnotation (Name. class); System. out. println (name. originate (); System. out. println ("created community:" + name. community ());}}}
5. Running result:
Description: javaeye, the best software development and communication community.
-----------------
Founder: robbin
Created community: javaEye
Founder: Jiangnan Baiyi
Created community: springside
In addition, the statement at, if reproduced please indicate the source: http://lighter.iteye.com.
This article reposted by others:-D