Detailed explanation of Android AOP annotation (I)

Android annotation

Related articles:

Detailed explanation of Android AOP annotation (I) detailed explanation of Android AOP annotation processing interpreter (II) detailed explanation and simple use examples of Android AOP annotation (III)

Android AOP is more and more widely used on Android, such as framework butterknife, dagger2, eventbus3, etc. Here I summarize a learning journey.

-Annotation in Java - annotation processing parser apt (annotation processing tool) - Annotation on Android -....

1、 What is annotation

Since jdk5, Java has added annotations, which are special tags in the code. These tags can be read during compilation, class loading and runtime, and perform corresponding processing. By using annotation, developers can embed some supplementary information in the source file without changing the original logic. Code analysis tools, development tools and deployment tools can be verified, processed or deployed through these supplementary information.

Annotation provides a way to set metadata for program elements (packages, classes, constructors, methods, member variables, parameters, local variables). Annotation cannot run. It has only member variables and no methods. Like public and final modifiers, annotation is a part of a program element. Annotation cannot be used as a program element.

Annotation does not directly affect code semantics, but it can be regarded as a program like tool or class library, which in turn will affect the semantics of running programs.

To sum up: annotation is a kind of metadata, which plays the role of "description and configuration".

2、 Custom annotation

2.1 definition notes

Define a new annotation type and use the @ interface keyword (add the @ symbol before the original interface keyword). Defining a new annotation type is similar to defining an interface, for example:

2.2 use notes

After defining the annotation, you can use it in your program. Using annotation is very similar to modifiers such as public and final. Usually, annotation will be placed on another line and before all modifiers. For example:

3、 Some concepts of annotation

3.1 defining attributes

Annotation has only member variables and no methods. The member variable of annotation is declared in the form of "method without formal parameters" in the annotation definition. Its method name defines the name of the member variable, and its return value defines the type of the member variable. In the following example, two member variables are defined in the form of methods:

Once the member variable is defined in the annotation, you should specify a value for the member variable of the annotation when using the annotation (if there is a default value, you can not specify it). For example:

Parameter members can only use eight basic data types: byte, short, char, int, long, float, double and Boolean, and data types such as string, enum, class and annotations, as well as arrays of these types.

3.2 classification

Annotations can be divided into the following two categories according to whether they contain member variables:

-Tag annotation: annotations without member variables are called tags. This annotation provides us with information only by its own existence, such as @ override.

-Metadata annotation: annotation containing member variables. Because they can accept more metadata, they are called metadata annotation.

3.3 default values

The annotation element must have a definite value, either specified in the default value of defining the annotation or when using the annotation. The value of the annotation element of non basic type cannot be null. Therefore, it is a common practice to use an empty string or 0 as the default value. Chestnuts:

Define annotation

Using annotations

4、 Meta annotation

The annotation of the annotation is not the metadata annotation mentioned above.

The function of meta annotations is to annotate other annotations. They are used to provide descriptions of other annotation types. The JDK provides the following four meta annotations:

- @Retention - @Target - @Documented - @Inherited

4.1 @Retention

Reserved means to specify how long an annotation can be retained.

@Retention contains a member variable named "value", which is of type retentionpolicy enumeration. When using @ retention, you must specify a value for its value. The value member variable can only have the following three values:

Function: indicates the level at which the annotation information needs to be saved, which is used to describe the annotation life cycle (i.e. to what extent the described annotation is valid)

example:

Define annotation

use

PS: if there is a member variable named "value" in the annotation, you can directly use the form XXX (VAL) to assign value to the value member variable without using the form name = val. see the above example.

4.2 @Target

Target, specifying which program elements annotation is used to decorate@ Target also contains a member variable named "value". The value member variable type is ElementType [], and ElementType is enumeration type. The values are as follows:

Function: specify the range of objects modified by annotation.

4.2.1 chestnuts 1

Annotation definition

use

4.2.2 chestnuts 2

Annotation definition

use

4.3 @Documented

If @ documented is used to modify the definition when defining annotation a, all program elements modified with annotation a will contain the description of annotation a after generating API documents with Javadoc command. Documented is a tag annotation with no members. Chestnuts:

Define annotation

Using annotations

4.4 @Inherited

Inheritance is a mark annotation that describes that a marked type is inherited. If an annotation type decorated with @ inherited is used for a class, the annotation will be used for subclasses of the class. Chestnuts:

Definition note:

A parent class uses annotations:

A subclass inherits the parent class:

5、 Basic annotation

The JDK provides the following basic annotations by default:

- @Override - @SuppressWarning - @Deprecated - @SafeVarargs

5.1 @Override

Qualified override parent method. For the method modified by @ override in the subclass, it is correct if there is a corresponding overridden parent method; If it does not exist, an error is reported@ Override can only act on methods, not on other program elements.

Parent class:

Subclass:

5.2 @SuppressWarning

Suppress compiler warnings. Instructs a program element decorated with @ suppresswarning (and all child elements in the program element, such as a class and methods in the class......) to suppress the specified compiler warning. For example, the common @ suppresswarning ("unchecked")

5.3 @Deprecated

Used to indicate that a program element (class, method, etc.) is obsolete. If you use a class or method modified by @ deprecated, the compiler displays in red, indicating that the method has been deprecated.

example:

5.4 @SafeVarargs

For security parameters, the @ safevarargs annotation can only be used on methods or construction methods with variable parameter length, and the methods must be declared as static or final, otherwise compilation errors will occur. JDK7 is specially provided to suppress the "reactor pollution" warning. For example:

If I pass a list to the following method, the compiler will alarm

Of course, using @ suppresswarnings ("unchecked") can not display warnings, but this is not good. At this time, use @ sagevarargs. If developers are sure that a method using variable length parameters will not have type safety problems when used with generic classes, they can declare it with this annotation. After using this annotation, the compiler will not give relevant warning information in case of similar situations:

6、 The essence of annotation

After the annotation is compiled, a class file is generated. For example, targettest in 4.2.1 uses the following command to generate the generated class bytecode

The generated bytecode contents are:

It can be seen that:

-The annotation is compiled into an interface, inheriting java.lang.annotation.annotation - the member variables in the annotation are compiled into a construction method with the same name. The annotation default defines the default value - the annotation of the annotation uses a runtimevisibleannotations ID.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>