Java: Annotations annotation (meta data)

Source: Internet
Author: User

The content of this article:

    • Introduction to annotation annotation
    • Usage of basic annotations

Starting Date: 2018-07-28

Introduction to annotation annotation

    • Annotation is a special tag in the code that can be identified during compilation, class loading, runtime (need to be set), and performs different processing depending on the annotation.
    • Annotation can modify program elements such as packages, classes, constructors, functions, member variables, declarations of local variables, parameters, and so on. Annotation helps these elements to set the metadata, and the program takes information from the metadata to process the elements.

Metadata can describe inter-code relationships or the relationship between code and other resources, such as the need to correspond the request path to the servlet that handles the request in Web. hbm files to describe the mapping between the entity class and the data table, for example, in Hibernate. Metadata can provide some information to assist in the operation of our program.

In Java, annotation can help us set up metadata at the code level without having to use some of the files, such as Web. XML, to describe it.

Usage of basic annotations

How to use annotations to decorate elements:
    • In general, annotations are made on the previous line of the modification target, such as. The same is true for decorated member variables.

Common Basic Annotation:

@Override: Indicates that the function to be decorated below must be a function that overrides the function of the parent class, and if the decorated function does not exist in the parent class, an error is added.

@Deprecated: Indicates that elements such as functions, classes, and so on are obsolete, and the effect is to warn when using obsolete elements.

@SuppressWarnings: Suppress cosmetic element warning (warning), the effect is modified, the compiler will not prompt the warning.

@SafeVarargs "Java7 new": If a collection without a generic definition is assigned to a set with a defined generic, heap pollution will occur and an error will be made. @SafeVarargs can suppress this error.

@FunctionalInterface "Java8 new": The interface used to restrict adornments must be a functional interface, or an error will be made.

Meta Annotaion: Annotations that modify annotations
  • @Retention: Specifies how the annotation is retained, in a different way, and affects where it can be used. For example, if you need to get information through reflection, you should allow the runtime to get it.
      • @Retention (retentionpolicy.class): The default value, which means leaving annotations in the CLASS file, the JVM will not be able to get annotations after the program runs. "Because the member variables inside the name is value, you can save the parameter name =" XXX ", if not value, then you can not save! "
      • @Retention (retentionpolicy.runtime): The delegate leaves annotations in the class file, and after the program runs, the JVM can get annotations by reflection.
      • @Retention (Retentionpolicy.source): Left in source code only, the JVM does not get annotations.
  • @Target: Limiting which elements annotaion can modify
      • @Target (Elemenettype.annotation_type): Specifies that the modified annotaion can only be used to decorate annotaion
      • @Target (Elemenettype.field): Specifies that modified annotaion can only be used to decorate member variables
      • @Target (elemenettype.constructor): Specifies that the modified annotaion can only be used to decorate the constructor
      • @Target (Elemenettype.method): Specifies that modified annotaion can only be used to modify functions
      • @Target (elemenettype.local_variable): Specifies that modified annotaion can only be used to modify local variables
      • @Target (elemenettype.package): Specifies that the modified annotaion can only be used to decorate the package
      • @Target (Elemenettype.parameter): Specifies that modified annotaion can only be used to modify parameters
      • @Target (Elemenettype.type): Specifies that modified annotaion can only be used to decorate classes, interfaces (including annotation types) or enumeration definitions
  • @Documented: Specifies whether Annotaion can be included in the Javadoc generated document.
  • @Inherited: Specifies that the annotaion has inheritance, and the parent class defines the annotation, and the subclass inherits the annotation.

Custom annotations

    • Custom annotations are similar to defining interfaces, and add an @ on the interface basis

    • When it is so empty, do not write things, is a marker-type annotation, marker-type annotation function is relatively simple, usually can only be used to mark special elements for processing. For example, override is a marker-type annotation.

    • Annotations can define member variables that exist in the form of declaring an invisible parameter function, which is the type of the variable, and the function name is the name of the variable. The member variables defined on the annotations can only be string, array, class, Enum class, annotation. Member variables can provide more meta-data information.

Once the member variable is defined, it is necessary to assign a value to the member variable when it is used (the member variable is named value, you can omit value= "xxx", but the name cannot be omitted). In the absence of a member variable, you can write the annotation name, and the parentheses are used to assign the value.

    • If some member variables do not need to be assigned each time, you can set a default value for it. The member variable can have a default value, followed by the default value.

Note Processor:

For Java-brought annotations, Java has its own annotation processor to handle. But custom annotations, you need to define the annotation processor yourself, to call the annotation processor to handle. Therefore, the annotation processor is essentially a normal function, but its work is mainly to deal with annotations.

Learn a few pre-knowledge before learning how to define a note processor:

    • The parent interface for all annotations is annotation.
    • An annotation that only has enough time for us to use it, so if we want to use the annotation processor, the annotations first need to use the @retention (retentionpolicy.runtime) modifier.
    • There are some tool classes in the Java.lang.reflect package that enable reflection functionality.
    • The Java.lang.reflect package has a annotationelement interface that declares a series of functions that can get annotations, and it has several implementation classes, as seen below, so you can invoke declared functions in objects such as classes, constructors, and so on to get annotations.
      • Getannotation (note. Class): Get annotations of the corresponding type
      • Getannotations (): Get all annotations
      • getDeclaredAnnotations():获取直接注解,忽略继承过来的注解。
      • Isannotationpresent (note. Class): Determine if the corresponding type of annotation exists

Get annotations:

.... The other objects are shown.

Operation notes: Usually, the information on the note strip is processed.

Small example

I don't know if you know anything about Org.junit.Test, which allows you to run the functions you want to run directly (what can be called Unit tests?). )。 Instead of needing you to create a main () to call.

Here's an example to implement a similar function:

1. Create Annotations:

2. Use annotations:

3. Call the Note processor: "This is called with org.junit.Test, you can also call it in Main ()!" 】

The result is:

Add:
    • In the small example above, the member variables of the annotations are not used, but in fact, the use of member variables is also just to get the information inside to do more processing, which depends on the individual to apply.

Java: Annotations annotation (meta data)

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.