Reference thinking in Java 4th
Concept
Annotations (also known as metadata) provide a formalized way for us to add information to our code so that we can use it very conveniently later in a log file system.
The JAVA5 contains three annotations defined in the Java.lang:
@override indicates that the current method overrides the method in the superclass
The editor warns you @Deprecated the annotation is used
@SuppressWarining turn off inappropriate warnings
Defining annotations
1. Meta-Annotations: When defining annotations, some meta-annotations are required, which can be understood as the most basic unit of annotations, and the meta-annotations of Java are as follows:
2. Elements: In annotations, some elements are typically included to indicate that some worth analysis processing annotations can take advantage of these values. The annotated element looks like a method of an interface, and the only difference is that you can specify a default value for it. The available types of annotation elements are as follows:
- All basic types (Int,float,boolean)
- String
- Class
- Enum
- Annotation
- Arrays of the above types
* Annotations without elements are called tag annotations
* If other types are used, the compiler will error
For example, here is a simple note that contains an element: name, which has a default value: Annoy, the code is as follows:
Package Annotation.person;import java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;/** * Created by Zuzhaoyue on 18/5/21. */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) public @interface person {public String name () Default "annoy";}
3. Use
Annotations are used in the form of key-value pairs, as shown in the following example:
Package annotation.person;import annotation.person.person;/** * Created by Zuzhaoyue on 18/5/21. */public class< c2> personutils { @Person (name = "Meat") private String employee; @Person (name = "Tiger") private string boss; private string Lolo;}
Writing the annotation processor
If there is no tool to read the annotations, the annotations and annotations are almost there. An important part of the process of using annotations is creating and using a note processor. Here is a simple note processor, which we use to read the Personutil class and use the Getannotation () method of the reflection mechanism to find the @person tag, the code is as follows:
PackageAnnotation.person;import java.lang.reflect.field;import java.util.arraylist;import java.util.collections;import java.util.list;/** * Created by Zuzhaoyue on 18/5/21. */public class Persontracker {public static void track (LIST<STRING> names, class Clazz) {for (Fie LD Field:clazz.getDeclaredFields ()) {Person person = field.getannotation (Person.class ), if (person! = null ) {System.out.println ("Find the person named" + person.name "+")! He is a "+ field.getname ()); Names.remove (Person.name ());}} if (names! = null && names.size () > 0 ) {for (String name:names) {System.out.println ("No Name found" + Name + "Person ~" ); }}} public static void Main (String args[]) {list<string> names = new Arraylist<> (); Collections.addall (names, "Meat", "big button", "Tiger" ), track (names, Personutils.class );}}
This program uses two reflection methods: Getdeclaredfields () and Getannotation (), the overall idea is clear:
1. Use Java reflection to get all the field of the Personutil class
2. Traverse Personutil field, locate the field that has the annotation as person, then print it out and remove it from the total name list
3. Print the remaining names that are not found
Test
Execute the main () method of Persontracker, and the console displays as follows:
Debug successful
Annotation+java Getting Started