JavaWeb - 2. Annotations

In this article, we will talk about annotations in Java.

In fact, this part of the content can be regarded as the supplementary content of Javaweb, which also includes related content of Junit testing and reflection. The Junit test is a relatively simple content, so I won’t write it here; and the related content of reflection can be found in the Java Basics column, which contains detailed descriptions.

Table of contents

1. Annotation overview

2. Annotations already defined in jdk

2.1 @Override annotation

2.2 @Deprecated annotation

2.3 @SuppressWarnings(parameter)

3. Custom annotations

4. Parse annotations in the program

5. Specific cases


1. Annotation overview

First, let's have a comprehensive understanding of annotations

Annotation concept: used to explain the program, it is for the computer to see

Format: @+annotation name

Explanation: We know that there are comments in java. The concept of comments is: describe the program in words and show it to programmers; we give the concept of comments by analogy. The only difference is that the annotation is for the computer to read

Classification of annotations:

  1. Compilation check: Through the annotations marked in the code, the compiler can implement basic compilation checks, such as @Override
  2. Writing documentation: Generate documentation through the annotations identified in the code (generate doc documentation, that is, api documentation, and generate class documentation commands: javadoc class name)
  3. Code analysis: analyze the code through the annotations identified in the code (using reflection)

Explanation: Compilation check is very simple, that is, the program checks the code when compiling to see if it conforms to the grammatical format. A typical example is the annotation @Override used when rewriting the method; writing documents is also easy to understand. That is, we add annotations to the program, and then if we want to generate api documents, then the annotated parts will be generated; code analysis will not be explained here

What we need to learn about annotations:

  1. Annotations defined in jdk
  2. custom annotation
  3. Using (parsing) annotations in programs

2. Annotations already defined in jdk

First of all, let's take a look at the annotations that have been defined in jdk. Mainly understand the following three:

  1. @Override detects whether the marked method is of the base parent class (interface)
  2. @Deprecated The content identified by this annotation indicates that it is outdated
  3. @SuppressWarnings (parameter) is used to suppress warnings, and the parameters generally pass all, indicating that all warnings are suppressed

2.1 @Override annotation

I won't go into too much detail about this annotation, we have used it many times

Just look at the code

very simple, not much to say

2.2 @Deprecated annotation

This annotation is used to indicate that the content of the logo is outdated

NOTE: Yes, but outdated. For example, a program has a new function, and the old function is outdated, so we need to express the old function, but because there are still people who will use the old function, the old function can still be used

Look at the code:

The comment on line 16 indicates that the method show1() is outdated, so when inputting on line 28, a horizontal line is drawn on the show1() displayed by idea, but note that the show1() method can still be used

2.3 @SuppressWarnings(parameter)

@SuppressWarnings (parameter) annotation is used to suppress warnings

Take a look at the code in detail:

These marked yellow positions are warnings, not errors, and do not affect the operation of the program

Then we add annotations to see:

 As shown in the picture, the warning is gone

The parameters inside are generally passed in all, which means to suppress all warnings

For this annotation, we generally mark it directly on the class, so that the warning of the entire class can be suppressed

 As shown in the figure, our entire class has no warnings

3. Custom annotations

Next, let's try custom annotations

Let's take a look at the format of annotations in java:

It is roughly composed of two parts. The first part: meta-annotation, which is two lines 49 and 50; the second part: annotation definition, which is line 51

So the annotation format is as follows:

元注解
public @interface MyAnno {

}

We follow it to define an annotation:

 

 Let me explain a little more here. We define an annotation, compile it, and decompile it with the javap command to see the essence of the annotation.

The essence of annotations: annotations are essentially an interface, which inherits the Annotation interface by default

Content decompiled from the interface: public interface MyAnno extends java.lang.annotation.Annotation{}

So, the annotation is essentially an interface, and the content that can be written in the interface (constant, abstract method) can be written in the annotation

But there are requirements:

  1. The return value type of the attribute can only include: basic data types, String, enumeration, annotations, arrays of the above types, and can only include these five categories
  2. The attribute is defined, and the attribute must be assigned a value when using it. The assignment format is attribute name=value; you can also use the default keyword to assign a default value to the attribute
  3. If only one attribute needs to be assigned, and the attribute name is value, then you can directly write the value without writing value

Let's look at a custom annotation:

This is a custom annotation, which contains the content

Let's take a look at the use of this annotation:

 Very simple key-value form

Next, let's talk about meta-annotations:

Meta-annotations: Annotations used to describe annotations are
meta-annotations that have been defined by jdk and need to be mastered:

  1. @Target: used to describe where the annotation can act, three values: TYPE: only acts on the class; METHOD: only acts on the method; FIELD: only acts on the member variable
  2. @Retention: Describes the stage when annotations are retained @Retention(RetentionPolicy.RUNTIME), indicating that the currently described annotations will be retained in the class bytecode file and read by jvm; the annotations we write ourselves generally choose RUNTIME
  3. @Document: Whether the description annotation is extracted into the api document
  4. @Inherited: Subclasses describing the class annotated by the annotation described by this annotation will automatically inherit this annotation

Let's look at a specific example:

 When we write custom annotations, we generally only use the first two annotations, and the latter two are generally not used

The definition of annotation is so much content

4. Parse annotations in the program

Let's look at the analysis of annotations in the program through specific examples

As shown in the figure, this is an annotation we customized

 This is our custom annotation used in the program

 This is the result of running

When we parse annotations in the program, we are essentially obtaining the contents of the annotations in the program, which includes the following three parts:

  1. Get the bytecode file at the location defined by the annotation
  2. Get the specified annotation
  3. Call the abstract method in the annotation to get the configured property value

 This is also corresponding in the above program, the running method uses reflection technology

5. Specific cases

Scenario: You have written a calculator that can perform four operations. Now you are required to write a test framework to test whether your calculator can operate correctly. If there is an error, please return the file to point out the error

code show as below:

 This is the calculator class

This is the annotation we wrote

 Here is our test framework

This is the result of running

Finally, we summarize

We will use annotations instead of custom annotations in the future. Who are the annotations for? The annotation is for the computer and for the analysis program (this analysis program is similar to our TestCheck class), and the annotation is for both. And the annotation is not part of our program, the annotation can be understood as a label, it is not part of the program. 

 

おすすめ

転載: blog.csdn.net/m0_52096593/article/details/131463446