Java Annotations @retention @interface meta-data

Source: Internet
Author: User
Tags deprecated

Original http://www.cnblogs.com/coolgun/p/3802535.html

Java annotations

That is, the comment, Baidu explained: Also called meta-data. A description of the code level. Personal understanding: is a comment that the content can be understood by the code, which is generally a class.

Meta data

Also called meta-annotations, is placed in front of a defined annotation class, is a restriction on annotations. Only two: @Retention and @Target (I only found these two).

@Retention: Used to describe the life cycle of the annotation class. It has the following three parameters:

Retentionpolicy.source: Annotations remain in the source file only

Retentionpolicy.class: Annotations remain in the CLASS file and discarded when loaded into the JVM virtual machine

Retentionpolicy.runtime: Annotations remain during a program run, at which point all annotations defined on a class can be obtained through reflection.

@Target: Used to indicate that the annotation can be declared before those elements.

Elementtype.type: Note that the note can only be declared before a class.

Elementtype.field: Note that the note can only be declared in front of a field in a class.

Elementtype.method: Indicates that the annotation can only be declared before a method of a class.

Elementtype.parameter: Note that the note can only be declared before a method parameter.

Elementtype.constructor: Note that the annotation can only be declared before the constructor of a class.

Elementtype.local_variable: Note that the note can only be declared before a local variable.

Elementtype.annotation_type: Note that the note can only be declared before an annotation type.

Elementtype.package: Note that the note can only be declared before a package name.

Definition of annotations:

The definition of the annotation uses the keyword @interface, and @rentention (ARG) or @target (args) in the line above, as in the following example:

@Rentention (Retentionpolicy.runtime)

Public @interface Annotation01 {

Defining a public final static property

.....

public abstract methods.

......

}

Common basic built-in annotations

@Override When we want to rewrite a method, add @override to the method, and when the name of our method goes wrong, the compiler will error. Defined as follows:

@Retention (Retentionpolicy.source)

Public @interface Override

@Deprecated used to indicate that a property or method of a class is obsolete and is not intended to be used by others, and is decorated with @deprecated on properties and methods. Defined as follows:

@Retention (Retentionpolicy.source)

Public @interface Deprecated

@SuppressWarnings used to suppress warnings from the program. Defined as follows:

@Retention (Retentionpolicy.source)

Public @interface suppresswarnings

Use of annotations

Define annotations--note--get annotations;

The obtained annotations are obtained through the Getannotation or Getannotations method of the modified object.

Java Annotations @retention @interface meta-data

Related Article

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.