Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic principle of Java annotations

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the basic principle of Java annotations". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Annotations are very popular at present, many mainstream frameworks support annotations, and they will try to use annotations when writing their own code, which is convenient for the time being, but the code is more concise.

The syntax of annotations is relatively simple, and except for the use of the @ symbol, it is basically consistent with the inherent syntax of Java. Three standard annotations are built into Java SE5:

@ Override, indicating that the current method definition will override the methods in the superclass.

@ Deprecated, the element compiler that uses the annotation for it will issue a warning because the annotation @ Deprecated is deprecated code, deprecated code.

@ SuppressWarnings, turn off improper compiler warning messages.

We will encounter some of the above three comments when writing code. Java also provides 4 annotations, which are specifically responsible for the creation of new annotations.

Define a way to annotate:

@ Target (ElementType.METHOD)

@ Retention (RetentionPolicy.RUNTIME)

Public @ interface Test {

}

Except for the @ symbol, the annotation is very much like an interface. Meta annotations are required when defining annotations, using @ Target and @ RetentionPolicy, whose meanings are nearly given in the table above.

There are usually some elements in annotations to represent certain values. Annotated elements look like interface methods, the only difference being that default values can be set for them. Comments without elements are called tag comments, and the @ Test above is a tag comment. Recommendation: SpringBoot core annotation principles, these are to be familiar with!

The available types of annotations include the following: all basic types, String, Class, enum, Annotation, array forms of the above types. An element cannot have an uncertain value, that is, it either has a default value or provides the value of the element when using annotations. And the element cannot use null as the default value.

Note when there is only one element and the name of the element is value, you can omit "value=" when using the annotation and write the desired value directly.

Let's take a look at an annotation that defines the element.

@ Target (ElementType.METHOD)

@ Retention (RetentionPolicy.RUNTIME)

Public @ interface UseCase {

Public String id ()

Public String description () default "no description"

}

If you define annotations, you must use annotations.

Public class PasswordUtils {

@ UseCase (id = 47, description = "Passwords must contain at least one numeric")

Public boolean validatePassword (String password) {

Return (password.matches ("\\ w*\\ d\\ w*"))

}

@ UseCase (id = 48)

Public String encryptPassword (String password) {

Return new StringBuilder (password). Reverse (). ToString ()

}

}

The most important part of using annotations is the processing of annotations, so the annotation processor is involved.

In principle, the annotation processor obtains the annotation information on the checked method through the reflection mechanism, and then carries on the specific processing according to the value of the annotation element.

Public static void main (String [] args) {

List useCases = new ArrayList ()

Collections.addAll (useCases, 47, 48, 49, 50)

TrackUseCases (useCases, PasswordUtils.class)

}

Public static void trackUseCases (List useCases, Class cl) {

For (Method m: cl.getDeclaredMethods ()) {

UseCase uc = m.getAnnotation (UseCase.class)

If (uc! = null) {

System.out.println ("Found Use Case:" + uc.id () + ""

+ uc.description ()

UseCases.remove (new Integer (uc.id ()

}

}

For (int I: useCases) {

System.out.println ("Warning: Missing use case-" + I)

}

}

Found Use Case:47 Passwords must contain at least one numeric

Found Use Case:48 no description

Warning: Missing use case-49

Warning: Missing use case-50

The above three pieces of code combine to be a simple example of tracking use cases in a project.

This is the end of the content of "what is the basic principle of Java comments". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report