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 design your own Annotation in Java

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

Share

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

This article is about how to design your own Annotation in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Take a look at the three default annotation types since java1.5:

One is @ Override: can only be used on methods to tell others that this method overrides the parent class.

One is @ Deprecated: advise others not to use the old API, compile with a warning message, which can be set on all the elements in the program.

One is @ SuppressWarnings: this type can temporarily turn off some warning messages.

If you are not clear about the specific usage of the above three types, you can baidu or google, it is very simple.

Part two: talk about the concept of annotation, and then talk about how to design your own annotation.

First, in the java.lang.annotation package that comes with jdk, open the following source files:

1. Source file Target.java

Code

@ Documented

@ Retention (RetentionPolicy.RUNTIME)

@ Target (ElementType.ANNOTATION_TYPE)

Public @ interface Target {

ElementType [] value ()

}

@ interface is a keyword. When designing an annotations, you must define a type as @ interface, instead of using the class or interface keyword (do you think sun is a little stingy, but so similar to interface).

2. Source file Retention.java

Code

@ Documented

@ Retention (RetentionPolicy.RUNTIME)

@ Target (ElementType.ANNOTATION_TYPE)

Public @ interface Retention {

RetentionPolicy value ()

}

See here, everyone may be blurred, do not know what to say, don't worry, look down.

Both RetentionPolicy,ElementType fields are used in the above files, and you may guess that these are two java files. Indeed, the source code for these two files is as follows:

3. Source file RetentionPolicy.java

Code

Public enum RetentionPolicy {

SOURCE

CLASS

RUNTIME

}

This is an enum type with three values, SOURCE,CLASS and RUNTIME.

SOURCE represents that the information of this Annotation type will only be retained in the program source code, and if the source code is compiled, the Annotation data will disappear and will not be retained in the compiled .class file.

ClASS means that the information of this Annotation type is retained in the program source code, as well as in the compiled .class file, and will not be loaded into the virtual machine (JVM) during execution. Note that when you do not set a Retention value of type Annotation, the system default value is CLASS.

The third, RUNTIME, means to keep information in source code and compiled .class files, which will be loaded into JVM during execution.

For example, if the Retention in @ Override is set to SOURCE, the check information is not needed if the compilation is successful. On the contrary, the Retention in @ Deprecated is set to RUNTIME, which means that in addition to warning us which Deprecated method is used at compile time, you can also find out whether the method is Deprecated.

4. Source file ElementType.java

Code

Public enum ElementType {

TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR

LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE

}

The ElementType in @ Target is used to specify which elements the Annotation type can be used on. To explain: TYPE (type), FIELD (property), METHOD (method), PARAMETER (parameter), CONSTRUCTOR (constructor), LOCAL_VARIABLE (local variable), ANNOTATION_TYPE,PACKAGE (package), where TYPE (type) refers to can be used on Class,Interface,Enum and Annotation types.

In addition, from the source code of 1, we can see that @ Target uses itself to declare itself and can only be used on ANNOTATION_TYPE.

If an Annotation type does not specify which elements @ Target is used on, then it can be used on any element, which refers to the above eight types.

Here are a few correct examples:

@ Target (ElementType.METHOD)

@ Target (value=ElementType.METHOD)

@ Target (ElementType.METHOD,ElementType.CONSTRUCTOR)

Refer to the javadoc documentation for details.

The source files of 1 and 2 above all use @ Documented,@Documented to enable the information of this design Annotation type to be displayed on the javaAPI documentation; if it is not added, the information generated by this type will not be found when using javadoc to generate API documents.

In addition, if you need to inherit Annotation data to subclasses, the Annotation type @ Inherited will be used.

Thank you for reading! This is the end of this article on "how to design your own Annotation in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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