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

How to customize and use Java annotations

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to customize and use Java annotations, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Java annotation how to customize and use the article, let's take a look.

I. the concept of annotations 1. Official interpretation of annotations

Notes

Called metadata, a code-level description, it is a feature introduced by JDK1.5 and later versions, and classes, interfaces, enumerations at the same level, it can be declared in front of packages, classes, fields, local variables, method parameters, etc., used to describe and comment these elements.

Function classification of annotations

Write documentation: generate documents from metadata represented in code [generate doc documents]

Code analysis: analysis through metadata represented in the code [using reflection]

Compilation check: enables the compiler to implement basic compilation checks through the metadata represented in the code [Override]

Annotations are classified according to operating mechanism

Source code comments: comments only exist in the source code and do not exist after being compiled into a .class file

Compile-time comments: comments exist in the .class file based on the existence of the source code, but do not exist at run time, for example: @ Override

Runtime comments: annotations still exist at run time and can affect the running process of the program, for example: @ Autowired

2. The difference between notes and notes.

(1) comments: used to describe the code, explain the program, the main purpose is to show the computer, and can affect the operation of the program.

(2) comments: used to describe the role of the code and some key knowledge points, the use of words to describe the program, is for programmers to see, so that programmers can understand the annotated code as quickly as possible.

Built-in comments and meta-notes 1. Commonly used built-in notes

@ Override: check whether the method of the annotation tag inherits from the parent class

@ Deprecated: indicates that the tagged content is out of date, suggesting that it may be updated and erased in the near future

@ SuppressWarnings: suppresses warnings, that is, the marked parts do not generate warnings. The common parameter: @ SuppressWarnings ("all")

@ SafeVarargs: parameter safe type annotation, which is designed to remind developers not to do unsafe operations with parameters. Its existence will prevent the compiler from generating warnings such as unchecked.

2. Commonly used meta-annotations

Meta-annotation: an annotation used to describe an annotation, used when creating an annotation

1. @ Target attribute value:

ElementType.TYPE: can modify classes, interfaces, or enumerated types

ElementType.METHOD: can modify the method

ElementType.FIELD: can modify member variables

ElementType.PARAMETER: can modify parameters

ElementType.CONSTRUCTOR: can modify the constructor

ElementType.ANNOTATION_TYPE: ability to modify annotations

ElementType.PACKAGE: can decorate the package

ElementType.LOCAL_VARIABLE: can modify local variables

2.@Retention attribute value:

RetentionPolicy.SOURCE: annotations only exist in the source code, and then disappear after being compiled into class

RetentionPolicy.CLASS: annotations exist in both source code and class, but not at runtime. This is the default value for Retention.

RetentionPolicy.RUNTIME: annotations exist in source code, class, and runtime, and must be defined as this type if you want to use reflection

3.@Documented: the purpose of this comment is to indicate that the comments marked by this comment can be included in the javadoc file.

4.@Inherited: describes whether annotations can be inherited by subclasses

3. Custom notes 1. Basic knowledge of custom notes

1. Format:

@ Inherited// meta-annotation public @ interface zhujie {}

two。 Essence of annotations: annotations are essentially an interface, which inherits Annotation by default

Public interface MyAnno extends java.lang.annotation.Annotion

3. Attribute: what can be defined in the interface (member methods, abstract methods)

Return value of the property:

Eight basic data types

String classes, interfaces, enumerations

Notes

Arrays of the above types

Considerations for attribute assignment

If you use the default keyword to initialize an attribute by default when defining an attribute, you can use annotations without assigning a value to the attribute, otherwise you must assign a value to the attribute

If there is only one attribute that needs to be assigned, and the name of the attribute is value, value can be omitted when assigning a value to the attribute using annotations, and the value can be defined directly

When assigning values to an array, the value needs to be wrapped with {}. If there is only one value in the array, {} can be omitted.

2. Demonstrate the use of custom annotations

Custom Annotation annotation

@ Retention (value = RetentionPolicy.RUNTIME) @ Target (value = ElementType.TYPE) public @ interface annotation {String name () default "wooden fish"; int age (); int [] score ();}

Use the class TestAnnotation annotated above

/ / name has a default value and does not need to be assigned to name, but you can also reassign @ annotation (age=20,score= {99100100}) public class TestAnnotation {public static void main (String [] args) throws ClassNotFoundException {Class clazz = Class.forName ("test.TestAnnotation"); annotation annotation = (annotation) clazz.getAnnotation (annotation.class); System.out.println ("name:" + annotation.name () + "age:" + annotation.age ()) System.out.print ("score:"); int [] score=annotation.score (); for (int score1:score) {System.out.print (score1+ ");} 3. Demonstrate the role of annotations in the program

Two methods:

Class object calls isAnnotationPresent (Class

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

Development

Wechat

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

12
Report