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

Case Analysis of java Annotation and Meta Annotation

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "java annotation and meta-annotation case analysis". In daily operation, I believe that many people have doubts about java annotation and meta-annotation case analysis. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods, hoping to help you answer the doubts of "java annotation and meta-annotation case analysis". Next, please follow the editor to study!

I. the basic introduction of the notes

Annotation, also known as Metadata, was introduced by JDK1.5 and later to modify data information such as interpreters, classes, methods, properties, constructors, local variables, and so on. It can be used to create documents, track dependencies in code, and even perform basic compile-time checks.

Annotations exist in the code with the'@ annotation name'. According to the number of annotation parameters, annotations can be divided into three categories: tagged annotations, single-valued annotations and complete annotations. Like comments, annotations do not affect program logic, but annotations can be compiled or run, equivalent to supplementary information embedded in the code.

In addition, you can choose at compile time whether the annotation in the code exists only at the source code level, or it can also appear in the class file or in the runtime (SOURCE/CLASS/RUNTIME).

In JavaSE, annotations are used for simple purposes, such as marking obsolete features, ignoring warnings, and so on. Annotations play a more important role in JavaEE, such as configuring any aspect of the application, replacing the cumbersome code and XML configuration left over from previous versions of java EE.

Second, the basic introduction of Annotation. 1. Three basic Annotaton

@ Override: qualifies a method by overriding the parent method. This annotation can only be used for methods.

@ Deprecated: used to indicate that a program element (class, method, etc.) is out of date

SuppressWarnings: suppresses compiler warnings

2. Code example

@ Override

Class Son extends Father {@ Override public void play () {}}

Note:

The play of the parent class is overridden when @ Override represents the play method of the subclass

If @ Override is not written here, the parent class ply will still be rewritten.

If you write the @ Override annotation, the compiler checks to see if the method actually overrides the method of the parent class, and if it does, it compiles, and if it does not constitute an override, it compiles an error.

@ Override can only modify methods, not other classes, packages, properties, and so on.

@ Deprecated

@ Deprecatedclass A {@ Deprecated public static int name = 1; @ Deprecated public void play () {}}

Note:

Obsolete is not unusable, but it is not recommended, but it can still be used

Can modify methods, classes, fields, packages, parameters, etc.

Its function is to achieve compatibility and transition between new and old versions.

@ SuppressWarnings

@ SuppressWarnings ("all") public class word {}

Note:

The scope of SuppressWarnings is related to where you place it. For example, if @ SuppressWarnings is placed in the main method, the scope for suppressing warnings is main.

According to the source code of @ SuppressWarnings, the annotations are aimed at classes, fields, functions, function input parameters, constructors and local variables of functions.

3. @ SuppressWarnings's specified warning type keyword interprets all suppression all warnings boxing suppression warnings related to package / disassembly jobs cast suppression warnings related to forced transition jobs dep-ann suppression warnings related to elimination annotations deprecation suppression related warnings related to omission of break in switch statements finally suppression warnings related to unreturned finally blocks hiding suppression and hidden variables Domain variable related warnings incomplete-switch suppression related to omitted items in switch statements (enum case) javadoc suppression related warnings javadoc related warnings related to non-nls string text null suppression related warnings related to null value analysis rawtypes suppression related warnings related to the use of raw type resource suppression warnings related to the use of Closeable type resources restriction suppression warnings related to the use of unrecommended or prohibited references Serial suppresses warnings related to serializable class omissions serialVersionUID fields static-access suppresses warnings related to incorrect static access static-method suppresses warnings related to methods that may be declared static super suppresses warnings related to replacement methods but does not contain super calls synthetic-access suppresses warnings related to unoptimized access to internal classes sync-override suppression omits synchronized due to replacement synchronization methods Warning unchecked suppresses warnings related to unchecked operations unqualified-field-access suppresses warnings related to unqualified field access unused suppresses warnings related to unused and deactivated code 4. Meta-note 1. Basic introduction

▶ what is meta-comment:

Meta-annotations are annotations that explain annotations. The objects of their annotations are the annotations we introduced earlier, such as @ Override, @ Deprecated, @ SuppressWarnings.

▶ four meta-annotations:

Retention: specify the scope of the annotation, three kinds of SOURCE,CLASS,RUNTIME

Target: specify where annotations can be used

Documented: specifies whether the comment will be reflected in javadoc

Inherited: subclasses inherit parent annotations

Notes for ▶ comments:

Comments exist only in the source code and are not included in the class bytecode file

The default retention policy, comments exist in the class bytecode file, but are not available at run time

Annotations exist in the class bytecode file and can be obtained by reflection at run time

First of all, it is necessary to make it clear that the life cycle length SOURCE < CLASS < RUNTIME, where the former can work, the latter must also work.

①: generally speaking, if you need to obtain annotation information dynamically at run time, you can only use RUNTIME annotation.

②: if you want to do some preprocessing at compile time, such as generating some auxiliary code (such as ButterKnife), annotate it with CLASS

③: if you are just doing some checking operations such as @ Override and @ SuppressWarnings, you can choose the SOURCE annotation.

2. Retention comments

Explanation:

Can only be used to modify an Annotation definition to specify how long the Annotation can be retained. @ Rentention contains a member variable of type RetentionPolicy, and you must specify a value for the value member variable (there are three values) when using @ Rentention.

Three values:

RetentionPolicy.SOURCE: after the compiler uses it, it directly discards comments for this policy.

RetentionPolicy.CLASS: the compiler will record the comments in the class file. When you run the Java program, JVM does not retain comments. This is the default value.

RetentionPolicy.RUNTIME: the compiler will record the comments in the class file. When you run the Java program, JVM retains comments. The program can get this comment through reflection.

3. Target comments

Explanation:

Used to modify the Annotation definition, specifying which program elements the modified Annotation can be used to modify. @ Target also contains a member variable named value.

4. Documented comments

Explanation:

Documented: used to specify that the Annotation class decorated by this meta-annotation will be extracted into a document by the javadoc tool, that is, when the document is generated, the annotation can be seen.

Note:

Annotations defined as @ Documented must have the Retention value set to RUNTIME.

5. Inherited comments

Explanation:

Annotations modified by @ Inherited will be inherited, and if a class uses annotations modified by @ Inherited, its subclasses will automatically have that annotation

At this point, the study of "java annotations and meta-annotation case analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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