Java annotation parsing

summary

Java introduced annotation in version 1.5, also known as Java annotation. Annotation is a kind of syntax metadata that can be directly used in source code. Classes / methods / variables / parameters / package names can be annotated. Unlike the Javadoc tag, the compiler can retain the annotation code when generating the class file. At the same time, the Java virtual opportunity may retain the annotation in order to use the annotation during the program running (run-time), so that the relevant information of the annotation can be obtained through reflection.

Built in annotation

In fact, we often encounter annotations, such as @ override, @ deprecated, etc. These are built-in annotations in JDK. Let's take a look at the main built-in annotations in Java. • Annotation @ override applied to Java code checks whether a method is a duplicated method. If the method is not found in the parent class or the implemented interface, the compilation will make an error. ◦ @ deprecated marks that a method or class is discarded. If this class or method is used, the compilation process will alarm ◦ @ suppresswarnings to inform the compiler to ignore the warning about the marked parameters ◦ @ safevarargs to ignore the warning about calling the method or constructor with generic parameters, 1.7 new annotation ◦ @ functional interface indicates that a declared interface will be used as a functional interface, and 1.8 new annotation

• notes for other notes, Called meta annotation ◦ @ retention indicates when the annotated annotation is used (that is, when the annotation will be retained) ■ it is only retained in the source code and discarded during compilation (retentionpolicy. Runtime) ■ the annotation is saved to the class file during compilation and ignored when the class file is loaded (retentionpolicy. Class) ■ the annotation is read when the class file is loaded, that is, the annotation is available during operation. The annotation information can be obtained through reflection (retentionpolicy. Runtime)

◦ @ documented indicates that the annotated annotation will be written into the Javadoc document when the Javadoc is generated. ◦ @ target indicates the scope of action of the annotated annotation ■ ElementType Type: used to describe class, interface (including annotation type) or enum declaration ■ ElementType Field: used to describe the field ■ ElementType Method: used to describe method ■ ElementType Parameter: used to describe the parameter ■ ElementType Constructor: used to describe constructor ■ ElementType LOCAL_ Variable: used to describe the local variable ■ ElementType ANNOTATION_ Type: used to describe annotation ■ ElementType Package: used to describe a package

◦ @ inherited indicates that the annotated annotation is inherited, that is, if an annotation type modified by @ inherited is used in a class, the annotation will also act on the subclass of the modified class. ◦ @ repeatable indicates that the annotated annotation can act on the same object multiple times. 1.9 new annotation

Custom annotation

With so many annotations mentioned above, we focus on meta annotations. When we customize annotations, we usually use meta annotations to help us. The custom annotation format is public @ interface annotation name {definition body}. When using @ interface custom annotation, it automatically inherits Java lang.annotation. Annotation interface. When customizing annotations, you cannot inherit other annotations or interfaces. The method declared in the annotation actually declares an annotation parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter. You can declare the default value of the parameter through default.

Custom annotation is very simple. Use @ interface to define an annotation, as shown below.

An annotation named classInfo is customized. According to @ retention, it can be known that this annotation will always exist, that is, it is still valid when the program is running@ Target (ElementType. Type) indicates that classInfo annotation is applied to class, interface or enum declaration@ Documented description classInfo information can be written into Javadoc documents.

Let's take another look at some annotation parameters in the user-defined annotation. There are three annotation parameters. The annotation parameters can be set to default values. For example, the author annotation parameter has a default value of Wang, and the other two parameters have no default values.

Let's look at another custom annotation.

The user-defined annotation methodinfo is used for methods. This annotation will also exist when the program is running; There are two annotation parameters.

Annotation parameter definitions (method definitions) can only use public or default access modifiers. Parameter types support the following types. • eight basic data types (byte, int, short, long, float, double, char, Boolean) • string type • class type • enum type • annotation type • arrays of all types above

Use of annotations

In addition to the above two annotations, a field scope annotation is added.

If the annotation parameters in the user-defined annotation do not declare default values, these parameters must be assigned values when using the user-defined annotation, otherwise the compiler will report an error.

Take a look at the code used by the annotation:

Get annotation information

To obtain annotation information, we must first ensure that annotations will exist when the program is running. Therefore, we generally add @ retention (retentionpolicy. Runtime) meta annotation to custom annotations, so that we can obtain some annotation information through reflection during the program running. For the description of reflection, you can see this article.

Get the field / method in the class through reflection, and get the relevant annotations through getannotation () or getannotations (). Get the specific annotations and you can get the specific information.

The operation result output is as follows:

Figure-1 operation results

summary

For java beginners and even Java developers with some experience, they may have little contact with Java annotations. In practice, they rarely use annotations, but they are often seen in the code. This article is a simple introduction to annotations. At least at the code level, there is no pressure to read them.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>