Home  >  Article  >  Java  >  How do you implement annotations commonly used in Java?

How do you implement annotations commonly used in Java?

php是最好的语言
php是最好的语言Original
2018-07-27 09:29:191317browse

「Java」Do you know how the Java annotations we often use are implemented? Annotations are equivalent to tags. The difference between them and annotations is that the content of the annotations can be used. Next, let us get to know Java annotations.

To use annotations, we can divide them into three parts: the annotation itself, the annotation usage class, and the annotation processing class, as shown below: Picture description

The structure of the annotation

Now, let’s see how the code works

Step one: Create new annotations
Create two new annotations, namely PlayerName and PlayerInfor

PlayerName:

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;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

@Documented

public @interface PlayerName {

String value() default "";

}

PlayerInfor:

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;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

@ Documented

public @interface PlayerInfor {

public enum Color{Blue,Red,White};

Color color() default Color.Blue;

int number() default 7;

int age();

String club() default "Manchester United";

}

Second Step: Define the entity class and use the newly created annotation
Create a new FootballPlayer class

FootballPlayer:

public class FootballPlayer {

//Bind the PlayerName annotation In the name attribute of the entity class

@PlayerName("Rooney")

public String name;

//Bind the PlayerInfor annotation to the playInfo attribute of the entity class

@PlayerInfor(color=Color.Red,number=10,age=30)

public String playInfo;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPlayInfo() {

return playInfo;

}

public void setPlayInfo(String playInfo) {

this.playInfo = playInfo;

}

}

Step 3: Define annotation processing class to process annotations of entity classes
Create a new AnnoUtil tool class

AnnoUtil:

import java.lang.reflect.Field;

public class AnnoUtil {

public static void getPlayerInfo(Class clazz){

/Get all attributes of the entity class/

Field[] fields = clazz.getFields();

//Loop to extract annotations from the entity class attributes

for(Field field:fields){

//Match annotation

if(field.isAnnotationPresent(PlayerName.class)){

//Get annotation

PlayerName name = field.getAnnotation(PlayerName.class);

//Use annotations

System.out.println("Athlete name:" name.value());

}else if(field.isAnnotationPresent(PlayerInfor.class)){

//Get the annotation

PlayerInfor infor = field.getAnnotation(PlayerInfor.class);

//Use annotations

System.out.println("Team affiliation: " infor.club() ", representative color: "

infor.color() ", number: " infor.number() ", age: " infor.age());

}

}

}

}

Step 4: Call the processing method of the processing class
public static void main(String[] args) {

AnnoUtil.getPlayerInfo(FootballPlayer.class);

}

The output results are as follows:

Athlete’s name: Rooney

Team: Manchester United, representative color: Red, number: 10, age: 30

From the above program, it can be understood that the relationship between entity classes and annotations is like articles and notes, and the annotation processing class is like people; people read articles with notes, analyze and process them, and output the results they want to express.

Attachment:
Java provides four meta-annotations, which are specifically responsible for annotating other annotations. They are as follows

@Retention meta-annotation, indicating at what level the annotation information needs to be saved ( life cycle). Optional RetentionPoicy parameters include:

RetentionPolicy.SOURCE: Stays in the java source file, and is discarded by the compiler

RetentionPolicy.CLASS: Stays in the class file, but will be discarded by the VM (default )

RetentionPolicy.RUNTIME: bytecode in memory, VM will also retain annotations at runtime, so the annotation information can be read through the reflection mechanism

@Target meta-annotation, default value For any element, indicates where the annotation is used. Available ElementType parameters include

ElementType.CONSTRUCTOR: constructor declaration

ElementType.FIELD: member variables, objects, properties (including enum instances)

ElementType.LOCAL_VARIABLE: local Variable declaration

ElementType.METHOD: Method declaration

ElementType.PACKAGE: Package declaration

ElementType.PARAMETER: Parameter declaration

ElementType.TYPE: Class, Interface (including annotation type) or enum declaration

@Documented includes annotations in JavaDoc

@Inheried allows subclasses to inherit annotations in parent classes [1]: /img/bVbekSZ

I have just compiled a set of the latest 0 basic introductory and advanced tutorials in 2018. I share them selflessly. You can get them by adding Java learning qun: 678-241-563. Included are: development tools and installation packages, as well as system learning routes. picture.

Related articles:

Summary of the most commonly used annotations in java

##Java annotation tutorials and custom annotations

Related videos:

Comprehensive analysis of Java annotations

The above is the detailed content of How do you implement annotations commonly used in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn