Detailed introduction to the basis of Java annotation

Detailed introduction to the basis of Java annotation

preface

Annotation is a very popular supplement introduced by Java. It provides a new way of structured and type checking, so that programmers can add metadata to code without causing code clutter and difficult to read. Using annotations can help us avoid writing cumbersome deployment description files and other generated files.

The syntax of annotation is relatively simple. Except for the use of @ symbol, it is basically consistent with the inherent syntax of Java. However, because there are few built-in annotations provided in the Java source code, most students do not know much about annotations, although we have all contacted them, such as several built-in annotations in Java:

However, this does not make us realize the power and convenience of annotations. In fact, Java also provides four additional annotations, which are specifically responsible for the creation of new annotations. Today we will learn how to create and use annotations.

Definition of annotation class

Let's first look at the definition of an annotation class:

Except for the @ symbol, the annotation class is defined much like an empty interface. When defining annotations, you need some meta annotations, such as @ target and @ retention. Java provides four meta annotations, which are defined as follows:

@Target: indicates where the annotation can be used.

Values (ElementType) include: constructor: used to describe constructor field: used to describe domain local_variable: used to describe local variable method: used to describe method package: used to describe package parameter: used to describe parameter type: used to describe class, interface (including annotation type) or enum declaration

@Retention: indicates the level at which the annotation information needs to be saved.

The values (retentionpolicy) include: Source: valid in the source file (i.e. source file retention) class: valid in the class file (i.e. class retention) runtime: valid at runtime (i.e. runtime retention). Therefore, the annotation information can be read through the reflection mechanism.

@Documented: indicates that this annotation is included in Javadoc.

@Inherited: indicates that the child class is allowed to inherit the annotation in the parent class.

It can be seen that the definition annotation format is: public @ interface annotation name {definition body}

The elements defined in the annotation class are called annotation elements. The available types of annotation elements are as follows:

If you use other types, the compiler will report an error. Note that no packaging type is allowed, but this is not a limitation due to the existence of automatic packaging. Annotations can also be used as element types. For example, we define another annotation class:

As you can see, all annotation elements have a default value. The compiler is too picky about the default value of the element. First, the element must have a default value; Second, null cannot be the default value. Therefore, we can only define some special values, such as empty string or negative number, to indicate that an element does not exist.

Use of annotation classes

After the annotation class is defined, how to use it?

In order to reflect the convenience and power of annotations, we write a demo here, and use annotations to automatically generate an SQL command for building database tables.

In addition, we provide two annotation classes:

OK, now we have four annotation classes.

@Dbtable represents the database table, and the annotation element name represents the table name@ Constraints represents the condition supplement to each column of the data table, including whether the primarykey is the primary key, false by default, whether allownull is allowed to be empty, true by default, whether unique data is unique, false by default@ Sqlstring represents the string column in the table. The annotation element value represents the column length, name represents the column name, and constraints represent some constraints on the column@ Sqlinteger represents the int column in the table, name represents the column name, and constraints represent some constraints on the column;

Next, we define a simple bean class in which the above four annotation classes are applied:

The annotated elements are expressed in the form of name value pairs and need to be placed in parentheses after the @ annotation class name declaration. If parentheses are not used, it means that all default values are used.

1. Class annotation @ dbtable gives the value member, which will be used as the name of the table;

2. The bean attributes firstname and LastName are annotated as @ sqlstring. These annotations have two interesting points: first, they all use the default value of the embedded @ constraints annotation; Second, they all use shortcuts. What is a shortcut? If an element named value is defined in the programmer's annotation, and if the element is the only element that needs to be assigned when applying the annotation, there is no need to use the syntax of name value pairs, but only give the value required by the value element in parentheses. This can be applied to elements of any legal type. Of course, this also limits the programmer from having to name this element value. All bean attributes use default values, and handle is the primary key.

3. All bean attributes use default values, and handle is the primary key.

Implement annotation processor

When the annotation class is used, we also need an annotation processor to parse the bean we define, so as to convert the annotation into the operation we need.

The annotation processor for parsing beans is defined below. Our purpose is to output an SQL statement. Reflection technology is mainly used.

Final output:

summary

Finally, through this article, we should learn the following points:

1. Understand the description and use of four meta annotations in Java; 2. Can customize annotation classes; 3. Understand the definition and rules of annotation elements; 4. Can use annotation class; 5. Can write a simple annotation processor to parse annotations.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>