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 use common annotations in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to use the commonly used notes in Java". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the commonly used notes in Java" can help you solve your doubts.

1. What are the notes

Java annotations are used to provide metadata for Java code. After reading this sentence, you may still look confused. In human words, annotations do not directly affect your code execution, but only provide information. Next, I will re-understand annotations (Annotation) from the following aspects: the definition of annotations, meta-annotations, annotation attributes, custom annotations, and annotations provided by JDK.

2. What are the three commonly used annotations supported by jdk:

@ SuppressWarnings the purpose of this annotation is to prevent the compiler from issuing certain warning messages. It can have the following parameters:

Deprecation: outdated class or method warning. Unchecked: warning when an unchecked conversion has been performed. Allthrough: warning when the Switch block goes directly to the next situation without Break. Path: warning when there is a path that does not exist in the classpath, source file path, and so on. Serial: warning when a serialVersionUID definition is missing on a serializable class. Finally: warning if any finally clause cannot be completed. All: warning for all of the above

@ Deprecated the purpose of this annotation is to mark an outdated class or method.

Override this annotation is used in front of a method to identify that the method is a method that overrides the parent class.

2.2 yuan note

Meta-annotations are mainly used to annotate custom annotations, including @ Retention, @ Target, @ Document, @ Inherited and @ Repeatable (added by JDK1.8).

Here are two of the most commonly used notes:

The lifecycle of @ Retention annotations, mainly including

RetentionPolicy.SOURCE exists only in the source code RetentionPolicy.CLASS default policy, in the class bytecode file, but not available at runtime RetentionPolicy.RUNTIME can be obtained by reflection at run time, is also the most commonly used.

The main targets of @ Target annotations are

ElementType.TYPE action interface, class, enumeration, annotation ElementType.FIELD action attribute field, constant ElementType.METHOD action method of enumeration ElementType.PARAMETER action method parameter ElementType.CONSTRUCTOR action constructor ElementType.LOCAL_VARIABLE action local variable ElementType.ANNOTATION_TYPE acts on annotations (this attribute is used in @ Retention annotations) ElementType.PACKAGE acts on package ElementType.TYPE_PARAMETER acts on type generics That is, generic methods, generic classes, generic interfaces (jdk1.8 added) 3. Annotation examples

In enumerations, the compiler will only limit the names of enumerations can not be repeated, in development will often encounter the problem of enumeration Id duplicate, because everyone in the development of features will only focus on their own functions, finished will only directly submit the code, will not notice the enumeration Id conflicts, this time you can use the following code for enumeration self-check, in the development phase of the project can find code problems in time. The following code can be used in the project with a little modification, taking it away and not writing it.

1. Custom comments

Detect whether there are duplicates in the key of the enumeration

Package org.pdool.anno; import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/*** @ author parsley * / @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface CheckEnum {}

2. Add notes to the corresponding methods.

Package org.pdool.anno;/*** resource enumeration class * @ author parsley * / public enum ResType {GOLD (1), DIAMOND (2), / / Note: repeat SILVER (2); int type; @ CheckEnum public int getType () {return type;} ResType (int type) {this.type = type;}

3. Check the enumeration of comments at the start of the project

Package org.pdool.anno; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashSet;import java.util.Set; / * * @ author parsley * / public class Aain {public static void main (String [] args) throws InvocationTargetException,IllegalAccessException {/ / get the annotated method Method [] declaredMethods = ResType.class.getDeclaredMethods (); Method annoMethod = null For (Method declaredMethod: declaredMethods) {CheckEnum annotation = declaredMethod.getAnnotation (CheckEnum.class); if (annotation! = null) {annoMethod = declaredMethod; break;}} Set set = new HashSet (); / / traverses the id Object [] oo = ResType.class.getEnumConstants () of each enumeration For (Object o: oo) {Object invoke = annoMethod.invoke (o); if (! set.contains (invoke)) {set.add (invoke);} else {System.out.println ("duplicate key" + o + "-" + invoke);}

Note: the above code is only a simple example, just to show the core code, when used in the project, you can modify the access of the class to scan the package under the project, traverse all the enumerations, and then you can use it in the project.

After reading this, the article "how to use common annotations in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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