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 understand annotations

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

Share

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

This article mainly explains "how to understand notes". The content of the explanation is simple and clear, and it is easy to learn and understand. now please follow the editor's train of thought to study and learn "how to understand notes".

The main contents of this paper are as follows:

Background

Now that annotations are in vogue, the annotation @ Override is all too familiar, as well as @ Controller, @ RequestMapping, @ Service.

Annotations have become an essential skill in development.

If you are asked for a note during the interview and you can't say 123, you have to go back and wait for the notice.

What are annotations?

Annotated annotation is a new feature in JavaSE5.0. It can be understood that an annotation is a tag that can be read at compile, class load, run time, and perform processing accordingly.

It can be added to any element of the program: package declaration, type declaration, constructor, normal method, member variable, parameter.

The boss of the notes:

Package java.lang.annotation; / / is an interface public interface Annotation {boolean equals (Object obj); int hashCode (); String toString (); / / gets the annotation type Class clazz = interfaceDemo.getClass (); Method [] methods = clazz.getMethods () For (Method method: methods) {if ("test" .contentEquals (method.getName () {System.out.println ("method name =" >

If the value of value is not specified, the default value and value are used to assign the value:

@ MyInterface ("Lao Tian Custom comments") @ MyInterface (value = "Lao Tian Custom comments")

Then we run the above code and the output is as follows:

We have done the above to customize the annotation and use it to get the attribute value.

Is the annotation a normal class or an interface?

Use javap to view the contents of our custom annotation .class file.

Here Annotation is the interface:

Public interface Annotation {}

So it proves that our custom annotation is the extend interface Annotation, so we can see that the annotation is the interface.

Custom annotation attention points

Supported data types for solution parameters:

All basic data types (int,float,boolean,byte,double,char,long,short)

String Typ

Class Typ

Enum Typ

Annotation Typ

Arrays of all the above types

How to set the parameters in the Annotation type:

It can only be decorated with public or default (default) access. For example, String value (); here set the method to the defaul default type.

Parameter members can only use basic types byte,short,char,int,long,float,double, eight basic Boolean data types and String,Enum,Class,annotations data types, as well as arrays of these types. For example, String value (); here the parameter member is String.

If there is only one parameter member, it is best to set the parameter name to value, followed by parentheses. Example: the following example FruitName annotation has only one parameter member.

How do I get comments?

There are five ways:

Get comments on the class: getAnnotation () of the Class class

Get comments on the method: getAnnotation () of the Method class

Get the comment on the field: getAnnotation () of the Field class

Get a comment on the constructor: getAnnotation () of the Constructor class

Get the comments on the package: getAnnotation () of the Package class

If the specified comment type exists on this element, this method returns the comment for that element, otherwise it returns null. From the above centralized methods, we found that they are all obtained using the getAnnotation () method, and I believe most people can guess why they are all the same method name.

Let's talk about the ancestors of annotations in Java:

Java.lang.reflect.AnnotatedElement

All methods of this interface

Look at the AnnotatedElement class diagram:

It is found that all the previously mentioned classes that get annotations implement the AnnotatedElement interface.

So after the program gets the AnnotatedElement object of a class through reflection, the program can call the method of that object.

There are four ways to access Annotation information:

"getAnnotation"

Returns a comment of the specified type that exists on the program element, or null if the type annotation does not exist.

"getAnnotations"

Returns all comments that exist on the program element.

"isAnnotationPresent"

Determines whether comments of the specified type are packaged on the program element, and returns true if it exists, false otherwise.

"getDeclaredAnnotations"

Returns all comments that exist directly on this element. Unlike other methods in this interface, this method ignores inherited comments. (if no comment exists directly on this element, an array of zero length is returned. Callers of this method are free to modify the returned array; this does not have any effect on the array returned by other callers

Thank you for your reading, the above is the content of "how to understand notes". After the study of this article, I believe you have a deeper understanding of how to understand notes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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