Java Annotations The Thing

Source: Internet
Author: User

Respect for the work of others, reproduced please indicate the source: http://blog.csdn.net/gengqiquan/article/details/70184450, this article from: "Gengqiquan blog"

There is nothing to do today, at the invitation of the baboon, write a detailed article about the annotations.

Annotations generally have the following functions
    • Generate documentation, which is the most common of all, and is the earliest Java-provided annotation feature. For example, look at the source code when the method comments above the @see @param @return and so on;
    • Reduced configuration, can be run-time dynamic processing, to obtain annotation information, to implement the function of the configuration file, can also be compiled by the plug-in processing, to resolve the interpretation of annotations caused by the reflection performance consumption;
    • Reduce duplication of effort, such as third-party framework Butterknife, and reduce calls to Findviewbyid through annotations @bindview;
    • Scoped scopes, such as a format check at compile time, such as @override before the method, if you do not overwrite the method of the same name with the superclass method, the compilation can be checked out, with annotations instead of enumerations and so on; Android also provides some parameter annotations to limit the resource parameters, which we will talk about later;

Advantages of annotations: Convenient, concise, configuration information and Java code together to help enhance the cohesion of the program.

The disadvantage of annotations: dispersed into the various class files, so it is not easy to maintain, such as some routing annotations, playing on the activity, especially in support of Multipath, find a mistake can make you crazy.

In general, annotations have more advantages over coding than disadvantages, especially when used appropriately, to reduce or avoid the drawbacks of the code.

Once we get to the top of the list, we'll see how to use the good annotation.

Content may be more, depending on the circumstances of the article. Lest you look at the dizziness of the headache from understanding to giving up.

Most Android uses Java, we only talk about Java annotations and how to customize the annotations that are appropriate for you

Meta annotations

The Java language provides several basic meta-annotations that we can use to customize the annotations we need after we understand and master them.

Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types.

    • @Target
    • @Retention
    • @Documented
    • @Inherited

The @Target specifies the scope of the annotation object to use, that is, where the annotations that have been @target modified can be placed, and can be decorated: packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, Enumeration values), method parameters, and local variables (such as loop variables, catch parameters)

The @Target values (ElementType) are:

    • CONSTRUCTOR: Used to describe the constructor
    • Field: Used to describe the domain
    • Local_variable: Used to describe local variables
    • Method: Used to describe methods
    • Package: Used to describe packages
    • PARAMETER: Used to describe parameters
    • Type: Used to describe classes, interfaces (including annotation types), or enum declarations

For example, we see the most system method annotations @override

@Target(ElementType.METHOD)@Retention(RetentionPolicy.SOURCE)public @interface Override {}

It is specified by @target that the annotation acts only on method, that is, class methods.

@Retention Specify the life cycle of the annotation object, for example, some annotations are legalized during the coding phase, just keep it in the source code. No need to compile into the Dex file. Some annotations, for example, need to be kept as they are acquired through reflection at runtime.

The @Retention values (retentionpoicy) are:

    • Source: Valid in the original file
    • Class: Valid in class file
    • Runtime: Valid at run time

The @retention (Retentionpolicy.source) in the @override example above shows that @retention (Retentionpolicy.source) annotations are only valid at the source stage.

@Documented is a markup annotation that represents this annotation being extracted into the document by a document generation tool such as Javadoc. The content in the Doc document differs depending on the content of the information for this annotation. @Documented no attribute members.

@Inherited is also a markup annotation that declares that a type that is being labeled is inherited. If an annotation type that uses the @inherited modifier is applied to a class, then this annotation will be applied to all subclasses of the class.

It is important to note that @inherited annotations are inherited only by subclasses of the class that are labeled. The class does not inherit the annotation from the interface it implements, nor does the method inherit the annotation from the method it overloads.

Also, the reflection API enhances this inheritance when the retention attribute value of the annotation annotated by @inherited is retentionpolicy.runtime. That is, when we use Java.lang.reflect to query a callout annotated with the @inherited modifier, the Reflection code examines the class and its parent class until the specified annotation type is found, or reaches the top level of the class inheritance structure.

The above is the introduction of the meta-annotations provided by Java, and then we look at the custom annotations

Custom annotations

@interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, class, String, enum). You can declare the default value of a parameter through default.
When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited, and the compiler automatically completes other details.
When you define annotations, you cannot inherit other annotations or interfaces.

The Custom annotation format is

  public @interface 注解名 {定义体}

The parameters of the annotations only support the following data types

    • All basic data Types (Int,float,boolean,byte,double,char,long,short)
    • String type
    • Class type
    • Enum type
    • Annotation type
    • Arrays of all types above

Points to be aware of

    • The parameters of the annotations can only be used with the public or the default, the two access modifiers
    • If there is only one parameter member, it is best to set the parameter name to value and then parentheses
    • The annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when the annotation is used, and the value of a non-basic type of annotation element cannot be null. Therefore, using an empty string or 0 as the default value is a common practice. This constraint makes it difficult for the processor to represent the presence or absence of an element, because in each annotation declaration, all elements are present and have corresponding values, in order to circumvent this constraint, we can only define some special values, such as an empty string or a negative number, once to indicate that an element does not exist (this paragraph is copied)

Cases:

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface BindView {    intdefault -1;}

This is the point of custom annotations.

Next: Common annotations provided by Android and custom restriction annotations
Next: How to define and code parse runtime annotations

Do you have any suggestions to leave a message?

If my blog is helpful to you, please leave a message to encourage or click to praise it!

I built a QQ group (Group No.: 121606151), for everyone to discuss the exchange of Android technology issues, interested can add, we progress together.

Java Annotations The Thing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.