In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the notes of Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the notes of Java"?
I. what are annotations
We all know that annotations are used in Java code to explain something to people who read this code later. Annotations are upgraded versions of annotations that can explain things to compilers, virtual machines, and so on. For example, the familiar @ Override is a meta-annotation that tells the compiler that the method it annotated is to override the method of the parent class, so that the compiler will check whether the method exists in the parent class and whether the signature of the method is the same as the parent class.
In other words, annotations are code that describes Java code, which can be parsed by the compiler, and annotation processing tools can parse annotations at run time. We use comments in the Java source file so that when we or others read this code later, we can understand it better. Javadoc tool can parse the description information we add for classes, methods, variables and so on in the source code, and automatically generate a HTML document based on these description information. These automatically generated documents can be used as API help documents. As long as the description information we add to classes, methods, and so on conforms to the syntax required by Javadoc, we can use the Javadoc tool to automatically generate a help document based on our description information. Annotations are much more powerful than java annotations and Javadoc, and the big difference between them is that Java annotations and Javadoc descriptions only stop at compile time, while annotations work until run time.
We know that using the "transient" keyword tells the compiler that the field is not serializable. Annotations provide us with a more general way to add description information to classes / methods / attributes / variables than to modify an attribute with keywords such as "transient", which are meaningful to developers, automation tools, Java compilers, and Java runtimes, that is, they can all "read" annotation information. The "transient" keyword is a modifier, and a comment is also a modifier. In addition to passing information, we can also use annotations to generate code. We can use annotations and then have annotation parsing tools parse them to generate some "templated" code. Frameworks such as Hibernate, Spring, and Axis make heavy use of annotations to avoid some repetitive work.
Second, meta-annotation
Meta-annotations are used to describe annotations. For example, we use "@ Target" meta-annotations in the following code to show that MethodInfo can only be used to annotate methods:
1234@Target (ElementType.METHOD) public @ interface MethodInfo {...}
Let's introduce several meta-annotations in detail.
1. Documented
When an annotation type is described by the @ Documentedmeta-annotation, it is documented by the Javadoc tool wherever the annotation is used. Let's take a look at its definition:
12345@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.ANNOTATION_TYPE) public @ interface Documented {}
We can see from the above code that the @ interface keyword is used to define annotations, just as we use the class keyword when defining classes and the interface keyword when defining interfaces. Annotations are also a type. This meta-annotation is modified by @ Documented to indicate that it itself will be documented. The value RetentionPolicy.RUNTIME of the @ Retention meta-annotation indicates that the @ Documented meta-annotation can be retained to the runtime; the value of the @ Target meta-annotation ElementType.ANNOTATION_TYPE indicates that @ Documented this annotation can only be used to describe the annotation type.
2. Inherited
Indicates that the modified annotation type is automatically inherited. The specific explanation is as follows: if an annotation type is modified by Inherited meta-annotations, when a user queries the annotation type in a class declaration and finds that the annotation type is not included in the class declaration, it will automatically query the corresponding annotation type in the parent class of the class, and this process will be repeated until the annotation type is found or the Object class has not been found. This meta-annotation is defined as follows:
12345@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.ANNOTATION_TYPE) public @ interface Inherited {}
We can see that this meta-annotation type is annotated by @ Documented, can be retained until runtime, and can only be used to describe the annotation type.
3. Retention
We have seen this meta-annotation above, which indicates how long an annotation type will be retained, such as the following code indicating that Developer annotations will be retained to the runtime:
1234@Retention (RetentionPolicy.RUNTIME) public @ interface Developer {String value ();}
@ Retention meta-annotations are defined as follows:
123456@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.ANNOTATION_TYPE) public @ interface Retention {RetentionPolicy value ();}
When we use @ Retention, the content in parentheses indicates its value. From the above definition, we can see that the type of value is RetentionPolicy, which is an enumerated type. It can take the following values:
SOURCE: indicates that this comment will be removed at compile time and will not be included in the compiled class file
CLASS: indicates that this comment will be included in the class file, but will be removed at run time
RUNTIME: indicates that this comment will be reserved for the runtime, which can be accessed by JVM at run time, and we can parse it through reflection at run time.
4. Target
This meta-annotation illustrates the scope of application of the modified annotation, that is, which program elements the modified annotation can be used to annotate, and is defined as follows:
123456@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.ANNOTATION_TYPE) public @ interface Target {ElementType [] value ();}
From the above definition, we can see that it is also retained to run time, and its value is ElementType [] type (an array, meaning that multiple values can be specified), and ElementType is an enumerated type, which can take the following values:
TYPE: indicates that it can be used to annotate classes, interfaces, annotation types, or enumerated types
PACKAGE: can be used to annotate packages
PARAMETER: can be used to annotate parameters
ANNOTATION_TYPE: can be used to annotate annotation types
METHOD: can be used to annotate methods
FIELD: can be used to annotate attributes (including enumeration constants)
CONSTRUCTOR: can be used to annotate constructors
LOCAL_VARIABLE: can be used to annotate local variables.
Third, common built-in notes
Java itself has some built-in annotations, so let's take a look at some of our common annotations in daily development: @ Override, @ Deprecated, @ SuppressWarnings. I believe all of us have used these three notes more or less, so let's get to know them again.
1. @ Override comment
Let's first take a look at the definition of this annotation type:
1234@Target (ElementType.METHOD) @ Retention (RetentionPolicy.SOURCE) public @ interface Override {}
From its definition, we can see that this annotation can be used to modify the method, and it is only valid at compile time and no longer exists in the compiled class file. The purpose of this annotation is familiar to all of us, which is to tell the compiler that the modified method is the same signature method in the overridden parent class, and the compiler will check it. If it is found that this method does not exist in the parent class or if the method signature is different, an error will be reported.
2. @ Deprecated
This annotation is defined as follows:
12345@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (value= {CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @ interface Deprecated
From its definition, we can know that it will be documented, can be retained until runtime, and can modify constructors, properties, local variables, methods, packages, parameters, and types. The purpose of this annotation is to tell the compiler that the modified program element has been "obsolete" and is no longer recommended to users.
3. @ SuppressWarnings
This note is also commonly used, so let's take a look at its definition:
12345@Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE) @ Retention (RetentionPolicy.SOURCE) public @ interface SuppressWarnings {String [] value ();}
It can modify the program elements including types, attributes, methods, parameters, constructors, local variables, can only survive in the source code, the value is String []. Its purpose is to tell the compiler to ignore the specified warning message, and it can take the following values:
Deprecation: ignore warnings when obsolete classes or methods are used
Unchecked: unchecked conversion performed
In the fallthrough:swich sentence, case forgot to add break and directly "fell" into the next case.
Path: classpath or original file path does not exist
Serial: serializable classes are missing serialVersionUID
Finally: there is a finally clause that does not execute properly
All: warnings from all of the above situations are ignored.
Examples of the use of this annotation are as follows:
12@SuppressWarning (value= {"deprecation", "unchecked"}) public void myMethos () {
By using the above annotations, we tell the compiler to ignore warnings in the myMethod method due to the use of obsolete classes or methods or unchecked conversions.
IV. Custom annotations
We can create our own annotation type and use it. Take a look at the following example:
123456789@Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ Inheritedpublic @ interface MethodInfo {String author () default "absfree"; String date (); int version () default 1;}
When customizing annotations, there are a few points we need to know:
Annotation types are defined by the "@ interface" keyword
In "annotated body", all methods have no method body and only allow two modifiers, public and abstract (the default is public without modifiers), and annotated methods do not allow throws clauses.
The return values of annotation methods can only be the following: raw data types), String, Class, enumerated types, annotations and their one-dimensional arrays. You can specify default return values for the method.
Let's take a look at the definition of @ SuppressWarnings annotation type mentioned above. This annotation type is defined for us by the system, and its definition is as follows:
12345@Target ({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE) @ Retention (RetentionPolicy.SOURCE) public @ interface SuppressWarnings {String [] value ();}
As we can see, it defines only one annotation method, value (), whose return type is String [] and does not specify a default return value. The syntax we use for the @ SuppressWarnings annotation is as follows:
1@SuppressWarnings (value= {"value1", "value2",...})
This annotation can be used by specifying a return value for each annotation method in parentheses after the annotation type name. Let's take a look at how to use our custom annotation type @ MethodInfo:
123456public class AnnotationTest {@ MethodInfo (author= "absfree", date= "20160410") public static void main (String [] args) {System.out.println ("Using custom annotation...");}}
So now the question is, does the custom annotation we use make sense to the compiler or virtual machine (can the compiler or virtual machine read it)? Obviously, if we don't do anything, the compiler or virtual machine won't understand our custom comments. Let's introduce the parsing of the following comments so that the compiler or virtual machine can read our custom comments.
Fifth, the analysis of notes 1. Compile-time parsing
Compile-time annotations refer to annotations whose value of @ Retention is CLASS. For parsing such annotations, we only need to do the following two things:
The custom class inherits the AbstractProcessor class
Override the process function in it.
In fact, at compile time, the compiler automatically finds all classes that inherit from AbstractProcessor and then calls their process methods. So as long as we do the above two things well, the compiler will take the initiative to parse our compile-time comments. Now that we change the Retention of the MethodInfo defined above to CLASS, we can parse it according to the following code:
1234567891011121314@SupportedAnnotationTypes ({"com.custom.customannotation.MethodInfo"}) public class MethodInfoProcessor extends AbstractProcessor {@ Override public boolean process (Set
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.