Basic knowledge of Java annotations

Source: Internet
Author: User
Tags deprecated

1: Note: annotation is a special modifier applied to classes, methods, parameters, variables, constructors, and package life, and is a metadata that is chosen by the JSR-175 standard to describe the code.

The following 4 annotations in Java are specifically responsible for the creation of new annotations:

@Target-Where the annotation is applied, if it is not explicitly stated, the annotation can be put anywhere.

@Inherited-whether subclasses are allowed to inherit the annotation.

@Retention-When to use this annotation to define the life cycle of the annotation; Retentionpolicy.class indicates that it is discarded when the class is loaded and is useful in the processing of bytecode files, which are used by default for annotations. Expression Retentionpolicy.runtime means that it is always not discarded and the runtime retains the comment, so you can use the reflection mechanism to read information about the annotation. Our custom annotations are often used in this way, such as some of the annotations in spring.

@Documented-whether the annotations are included in a simple markup annotation in Javadoc to identify whether to add annotation information to a Java document

Common annotations built into Java:

@Override that indicates that the current method definition overrides a method in the superclass.

@Deprecated, the element compiler that uses annotations for it will issue a warning because the annotation @deprecated is deprecated code, which is not approved for use.

@SuppressWarnings, turn off improper compiler warning messages.

Custom Annotations:

1 @Target (Elementtype.method) 2 @Retention (retentionpolicy.runtime) 3 public @interface Test {4  5}

In addition to the @ symbol, annotations are much like an interface. When defining annotations, you need to use meta annotations, which use @target and @retentionpolicy.

There are generally some elements in the annotations that represent certain values. The annotated element looks like the method of the interface, the only difference being that it can be set to a default value. Annotations without elements are called tag annotations, and the @test above is a markup annotation.

There are generally some elements in the annotations that represent certain values. The annotated element looks like the method of the interface, the only difference being that it can be set to a default value. annotations without elements are called tag annotations , and the @test above is a markup annotation.

  The types of annotations available include the following: All basic types, String, Class, enum, Annotation, and array forms of the above types. An element cannot have an indeterminate value, that is, it has either a default value or a value for the element when the annotation is used. And the element cannot use NULL as the default value. Note In the case where there is only one element and the name of the element is value, you can omit "value=" when using annotations and write the required values directly.

The most important part of using annotations is the processing of annotations, then the annotation processor is involved.

In principle, the annotation processor obtains the annotation information on the inspected method through the reflection mechanism, and then carries out the specific processing according to the value of the annotation element.

First and foremost: annotation is a passive metadata that never has an active behavior.

Whenever annotation works, there is an execution mechanism/caller who obtains the metadata through reflection and then acts on it. In your case, this execution mechanism is the test framework junit. You go slowly through the JUnit code to find it to query the called class there is no such annotation, and then if there is how the code


Basic knowledge of Java annotations

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.