If there are no methods and tasks to read annotations, then annotations will not be more useful than annotations. An important part of the process of using annotations is created using annotation processor. JavaSE5 extends the reflection mechanism API to help programmers quickly construct custom annotation processors.
Annotated processor class library (java.lang.reflect.AnnotatedElement):
Java uses the Annotation interface to represent the annotation in front of the program element, which is the parent interface of all Annotation types. In addition, Java has added an AnnotatedElement interface under the java.lang.reflect package. This interface represents program elements that can accept annotations in the program. The interface mainly has the following implementation classes:
Class: Class definition
Constructor: Constructor definition
Field: Cumulative member variable definition
Method: Method definition of class
Package: package definition of class
The java.lang.reflect package mainly contains some tool classes that implement reflection functions. In fact, all the reflection APIs provided by the java.lang.reflect package expand the ability to read runtime Annotation information. When an Annotation type is defined as an Annotation at runtime, the annotation can only be visible at runtime, and the Annotation saved in the class file when the class file is loaded will be read by the virtual machine.
The AnnotatedElement interface is the parent interface of all program elements (Class, Method and Constructor). Therefore, after the program obtains the AnnotatedElement object of a certain class through reflection, the program can call the following four methods of the object to access the Annotation information:
Method 1: <TextendsAnnotation>TgetAnnotation(Class<T>annotationClass): Returns an annotation of the specified type that exists on the program element. If the annotation of the type does not exist, return null.
Method 2: Annotation[]getAnnotations(): Returns all annotations that exist on the program element.
Method 3: booleanisAnnotationPresent(Class<?extendsAnnotation>annotationClass): determines whether the program element contains annotations of the specified type. If it exists, it will return true, otherwise it will return false.
Method 4: Annotation[]getDeclaredAnnotations(): Returns all comments that exist directly on this element. Unlike other methods in this interface, this method ignores inherited annotations. (If no comment exists directly on this element, an array of length zero is returned.) The caller of the method can modify the returned array at will; this will not have any effect on the array returned by other callers.
A simple annotation processor:
/********************* Annotation declaration*******************//** * Fruit name annotation* @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitName {String value() default "";}/** * Fruit color annotation* @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitColor {/** * Color enum * @author peida * */public enum Color{BULE,RED,GREEN};/** * Color attribute* @return */Color fruitColor() default Color.GREEN;}/** * Fruit supplier annotation* @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitProvider {/** * Supplier number* @return */public int id() default -1;/** * Supplier name* @return */public String name() default "";/** * Supplier address* @return */public String address() default "";}/**************** Annotation uses **********************/public class Apple {@FruitName("Apple") private String appleName;@FruitColor(fruitColor=Color.RED) private String appleColor;@FruitProvider(id=1,name="Shaanxi Hongfuji Group",address="Hongfuji Building, No. 89, Yan'an Road, Xi'an City, Shaanxi Province") private String appleProvider;public void setAppleColor(String appleColor) {this.appleColor = appleColor;}public String getAppleColor() {return appleColor;}public void setAppleName(String appleName) {this.appleName = appleName;}public String getAppleName() {return appleName;}public void setAppleProvider(String appleProvider) {this.appleProvider = appleProvider;}public String getAppleProvider() {return appleProvider;}public void displayName(){System.out.println("The name of the fruit is: apple");}}/******************Annotation processor*************************/public class FruitInfoUtil {public static void getFruitInfo(Class<?> clazz){String strFruitName=" Fruit name: ";String strFruitColor=" Fruit color: ";String strFruitProvicer="Supplier information: ";Field[] fields = clazz.getDeclaredFields();for (Field field :fields){if(field.isAnnotationPresent(FruitName.class)){FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);strFruitName=strFruitName+fruitName.value();System.out.println(strFruitName);} else if(field.isAnnotationPresent(FruitColor.class)){FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class);strFruitColor=strFruitColor+fruitColor.fruitColor().toString();System.out.println(strFruitColor);} else if(field.isAnnotationPresent(FruitProvider.class)){FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class);strFruitProvicer=" Supplier number: "+fruitProvider.id()+" Supplier name: "+fruitProvider.name()+" Supplier address: "+fruitProvider.address();System.out.println(strFruitProvicer);}}}}/*************** Output result****************************/public class FruitRun {/** * @param args */public static void main(String[] args) {FruitInfoUtil.getFruitInfo(Apple.class);}}======================================================= Fruit name: Apple Fruit color: RED Supplier number: 1 Supplier name: Shaanxi Hongfuji Group Supplier address: Hongfuji Building, No. 89, Yan'an Road, Xi'an City, Shaanxi ProvinceI have basically read the basic knowledge points of Java annotations (see the map below). In the next article, we design a simple ORM framework based on annotations to comprehensively apply and further deepen the understanding and application of various knowledge points of annotations.
Summarize
The above is all about the simple examples of Java annotation processor in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!