Java annotations are described in detail

  • 2020-04-01 03:28:45
  • OfStack

Introduction to Java annotations

Annotation-based Java development is certainly the latest development trend.

Based on the annotation of the development will be a Java developer from tedious heavy configuration file. First introduced in Java 5.0 annotations, annotation is one of the features of the JDK version, the programmers write Java example API file work over to a compiler. When no longer maintain separate source code and API documentation, code and documentation will be easier to maintain. The generated code examples are unlikely to contain errors.

Java annotations is one of the main features of the JDK 5, make development more simple and easy. Comments as a meta information (meta, can be understood as additional information, to show special program), you can add to the code, can be used package (package) statement type (type) statement, the constructor (constructors), method (the methods), fields (fields), parameters (parameters) and variables (variables). They provide an effective way to indicate whether a method depends on another method, whether it is complete, whether a class references another class, and so on.

Reference (link: http://download.oracle.com/javase/6/docs/technotes/guides/language/enhancements.htmlhttp://), "it (development) based on annotations can let us in a lot of the time don't have to write a separate API documentation, only need to enable tools to annotations in the source code can be generated. It generates a declarative programming style, said the programmer, what needs to be done, let tools according to the code to do it."

In simple terms, annotations are a mechanism for associating meta-tags with program elements, allowing the compiler or JVM to extract program behavior from annotated elements and generate codependent code if necessary.

In the first part of this series, I'll introduce you to some of the basic Java annotations, they use (benefits), and some examples of usage (usages).

Java annotation basics

You need to know the two places. One is the "annotation" (the annotation, similar to a new object) itself, the second is the type of "annotation" (the annotation type, similar to a class definition). The annotation is a meta tag, in your code, it is a life cycle and scope. The annotation type is used to define annotations. When you want to create your own annotations, You will use it. Type is the actual construction type used, and the annotation is just a specific use of that type.

Custom annotation types need to use a "at" (a) @, domestic someone read circle tag, followed by the keyword interface, coupled with the annotation name (name). On the other hand, in the form of using annotations, is to write "at" symbol (@), followed by the annotation type. This is the simplest form of annotation. In addition, when you can use annotations, behind the name with parentheses, enclosed inside the parameters of the need to pass. You will see them behind the sample:

Example of defining Annotation Type: (Annotation Type, similar to defining a class)


public @interface MyAnnotation {
   String doSomething();
}

Using annotations in plain code


@MyAnnotation (doSomething="What to do")
public void mymethod() {
   ....
}

Java Annotation Types

There are three types of annotations:

Marker: an annotation of a Marker type has no element, just a name.

Definition:


//This annotation, like a label, has no state
//It is somewhat like an interface Serializable with no method definition
public @interface AMarkerAnnotation {
}

Use:


@AMarkerAnnotation
public void mymethod() {
   ....
}

Single Element annotation: Single Element or Single value annotation of this type, with only one data. It can be represented in parentheses as data=value, or it can be passed with only one value (easy to write).

Definition:


public @interface SingleElementAnnotation
{
    String doSomething();
}

Use:


@SingleElementAnnotation (" You can pass only values of the corresponding type ")
public void mymethod() {
   ....
}

Annotations for full-value or multiple values: annotations of type full-value have multiple data members; therefore, parameters must be passed for each member using the Full data=value syntax format.

Definition:


public @interface FullValueAnnotation {
   String doSomething();
   int count;
   String date();
}

Use:


@FullValueAnnotation (doSomething=" The parameter value ", count=1,
               date="09-09-2005")
public void mymethod() {
   ....
}

Define considerations for Java annotation types

Note when defining annotation types:

1. The annotation declaration should begin 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 arguments (it just looks like a method, but is essentially a property field).
3. Method declarations in annotations must not have a throws clause.
4. The method return types in the annotations can only be as follows:
@primitives(6 primitive data types,int,byte, etc.)
@string
@Class(Class like string.class)
@ enum (enumeration)
Array of the above types @array of the above types

Java annotation type

There are two types of annotations in JDK5:

1.Simple annotations: these are Tiger(Tiger is JDK1.5 code ?) Provides basic types that can only be used to annotate plain code; Cannot be used to create another custom annotation type.

2.Meta annotations: specifically designed to annotate the declarations of other annotation types. Simply put, they are referred to as annotations of annotations.


Related articles: