Introduction to Java annotations
Annotation-based Java development is undoubtedly the latest development trend. [Translator's Note: This is an article from 2005. In 2014, there is no doubt that the use of annotations has become a good solution for multi-person collaborative development. In a cooperative manner, mutual influence and coupling can be very low].
Annotation-based development frees Java developers from cumbersome and cumbersome configuration files. Annotations were introduced for the first time in Java 5.0. Annotations are one of the features of this JDK version, transferring the work of programmers to write Java sample API files to the compiler. The code and documentation are easier to maintain when separate source code and API documentation are no longer maintained. The generated code examples are also less likely to contain bugs.
Java annotations are one of the main features in JDK 5, making development simpler and easier. Annotations are like a kind of meta-information (meta, which can be understood as additional information for special programs to see) and can be added to In code, it can be used in package declarations, type declarations, constructors, methods, fields, parameters and variables. They provide an efficient way to indicate whether a method depends on other methods, is complete, whether the class references other classes, etc.
Quoting from Oracle's official website, "It (annotation-based development) allows us to avoid having to write a separate API documentation in many cases. We only need to enable tools to generate annotations from the source code. This A declarative programming style has been formed. Programmers say, what needs to be done, just let the tool do it according to the code."
Simply put, annotations are a mechanism that associates meta-tags with program elements, allowing the compiler or JVM to extract program behavior from annotated elements and generate interdependent code when necessary.
In the first part of this series of articles, I will introduce some basic Java annotations, their benefits, and some example usages.
Java annotation basics
You need to understand two things. One is the "annotation" (annotation, similar to a new object) itself, and the other is the "annotation type" (annotation type, similar to a class definition). Annotation is a meta tag, used in In your code, it actually has a life cycle and scope of application. The annotation type is used to define annotations. You will use it when you want to create your own annotations. type is the actual constructed type used, and annotation is just a specific usage of that type.
When defining an annotation type, you need to use an "at" (@, some people in China pronounce it as a circle) mark, followed by the keyword interface, plus the name of the annotation. On the other hand, using the form of annotation is also First write the "at" symbol (@), followed by the annotation type. This is the simplest form of annotation. In addition, when using annotations, you can add parentheses after the name to include the parameters that need to be passed. Next You'll see examples of them:
Example of defining annotation type: (Annotation Type, annotation type, similar to defining a class)
Copy the code code as follows:
public @interface MyAnnotation {
String doSomething();
}
Using annotations (Annotation instances) in normal code
Copy the code code as follows:
@MyAnnotation (doSomething="What to do")
public void mymethod() {
....
}
Java annotation types (Annotation Types)
There are three types of annotations:
Marker: Marker type annotations have no elements, only a name.
definition:
Copy the code code as follows:
// This kind of annotation is like a label, without status
// A bit like the interface Serializable without method definition
public @interface AMarkerAnnotation {
}
use:
Copy the code code as follows:
@AMarkerAnnotation
public void mymethod() {
....
}
Single-element annotations: Single-Element or single-value annotations carry only one data. They can be expressed as data=value in parentheses, or only one value can be passed (simple writing method).
definition:
Copy the code code as follows:
public @interface SingleElementAnnotation
{
String doSomething();
}
use:
Copy the code code as follows:
@SingleElementAnnotation ("You can only pass values of the corresponding type")
public void mymethod() {
....
}
Full-value or multiple-value annotations: Full-value type annotations have multiple data members. Therefore, parameters must be passed using the complete data=value syntax format for each member.
definition:
Copy the code code as follows:
public @interface FullValueAnnotation {
String doSomething();
int count;
String date();
}
use:
Copy the code code as follows:
@FullValueAnnotation (doSomething="parameter value", count=1,
date="09-09-2005")
public void mymethod() {
....
}
Considerations for defining Java annotation types
Things to note when defining annotation types:
1. The annotation declaration should start with an at symbol (@), followed by an interface keyword, and the name of the annotation.
2. The method declaration in the annotation does not accept any parameters (it just looks like a method, but is essentially an attribute domain).
3. The method declaration in the annotation cannot have a throws clause.
4. The method return types in the annotations can only be the following:
@primitives (6 primitive data types, int, byte, etc.)
@String(string)
@Class(class, such as String.class)
@enum(enumeration)
@array of the above types (array, array elements can only be one of the above types)
Java annotation types
There are two types of annotations in JDK5:
1.Simple annotations (simple annotation types): These are the basic types provided by Tiger (Tiger is the code name of JDK1.5?) and can only be used to annotate ordinary code; they cannot be used to create another custom annotation type.
2.Meta annotations: Specifically designed to annotate declarations of other annotation-types. Simply put, they are called annotations-of-annotations.