Java Annotations (1)-Annotation Basics

Source: Internet
Author: User
Tags deprecated

Annotations (Annotation) are introduced in JAVA5, which provides a new way to add information to your code. Annotations combine metadata with source code files to some extent, as many mature frameworks (Spring) do. So, what exactly can annotations do?

1, the role of annotations.
    • Provides information needed to fully describe a program, such as compile-time validator information.
    • Generates a descriptor file, or generates a definition for a new class.
    • Reduce the burden of writing "boilerplate" code (configuration files), which can be generated automatically using annotations.
    • More clean and readable code.
    • Compile-time type checking.
2. Annotations provided by Java

Java5 contains some native annotations that are next to the Java.lang package (more than this):

    • @Override that indicates that the current method definition overrides a method in the superclass.
    • @Deprecated, the identity element is deprecated, and if the programmer uses annotations as its elements, the compiler issues a warning message.
    • @SuppressWarnings, close the incorrect compiler warning message.

The above annotations are source-level annotations (that is, for retentionpolicy.source labeling, it is important to note that although @ Deprecated is labeled by retentionpolicy.class, but the role is mainly in the compilation period, only the compiler can read, compiled into class will be discarded, of course, the runtime will not exist.

Java also provides meta annotations for custom annotations (meta-annotations), such as @target, @Retention, @SupportedSourceVersion, and so on. Meta-annotations in the Java.lang.annotation package:

@Retention Specifies how annotations for the specified identity are saved.

    • RetentionPolicy.SOURCE– Annotations will be saved only at the source level and will be discarded by the compiler.
    • RetentionPolicy.CLASS– will be used at compile time and saved in class, but the JVM will not recognize this.
    • RetentionPolicy.RUNTIME– this annotation will be recognized by the JVM and will theoretically be present at any time.

@Documented indicates that the annotation used by the element identified by the note should appear in Javadoc.

@Target Specifies which Java element can use the currently defined annotation, specifying the type (ElementType) as follows:

    • ElementType.ANNOTATION_TYPEannotation type declaration.
    • ElementType.CONSTRUCTORConstructs a method declaration.
    • ElementType.FIELDA field declaration (including enumeration constants).
    • ElementType.LOCAL_VARIABLElocal variable declaration.
    • ElementType.METHODMethod declaration.
    • ElementType.PACKAGEPackage declaration.
    • ElementType.PARAMETERThe parameter declaration.
    • ElementType.TYPEclasses, interfaces (including annotation types), or enumeration declarations.

@Inherited indicates that the annotation type is automatically inherited. If a inherited meta comment exists in the annotation type declaration, and the user queries the annotation type in a class of declaration, and there is no comment of that type in that declaration, the annotation type is automatically queried in the superclass of the class. This process repeats until a comment of this type is found or the top level (Object) of the class hierarchy is reached. If no superclass has a comment of that type, the query indicates that the current class does not have such a comment.

Note that if you use anything other than the comment type comment class, the meta-comment type is not valid. Also note that this meta-comment only facilitates the inheritance of comments from the superclass, and the annotations to the implemented interfaces are not valid.

@Repeatable (added in Java8) annotations that use this annotation annotation are reusable when used. Note that annotating the same annotation before Java8 is not allowed to be used multiple times on the same element.

3. Define Annotations

Most of the time, developers need to define annotations themselves to meet different needs.

Define annotations, which typically include the definition of annotations, annotation elements, and meta annotations, as shown in the following example:

package Import java.lang.annotation.ElementType;   Import java.lang.annotation.Retention; Import Java.lang.annotation.RetentionPolicy; Import Java.lang.annotation.Target, @Target (elementtype.type) @Retention (retentionpolicy.class)  public @Interface  ClassInfo {    default "Default";}

3.1. Definition of annotations

Use the keyword @interface to define, such as on public @interface ClassInfo, the permission qualifier for annotations only supports public, default (package access permission).

3.2, Yuan annotation

As above @target (Elementtype.type), the annotation ClassInfo can be used for classes, interfaces, or enumeration types.

3.3. Annotation elements

The annotation element uses a definition format similar to the normal method, such as value () in the above example, using default to specify the defaults. The annotation element must have a value when it is used, either the default value, or the value of the provided element when the annotation is used. And the value of the annotation element cannot be a null value.

The available types of annotation elements are: All basic types, String, Class, enum, Annotation, and arrays of the above types. If you are using annotations of complex types, you can use the nesting of annotations to implement them. if an element named value is defined in the programmer's annotations, and when the annotation is applied, it is not necessary to use the syntax of the name-value pair If the element is the only one that needs to be assigned. Instead, just give the value that you want in parentheses.

3.4. Annotations do not support inheritance

You cannot use the extends keyword to inherit a @interface. You can use nesting to achieve similar functionality.

4. Use of annotations

If you want to use the above annotations, you can use this method as follows:

package Import com.zenfery.example.annotation.ClassInfo; @ClassInfo ("  This is the Helloannotation class. ")publicclass  helloannotation {}

Original link https://blog.zenfery.cc/archives/70.html

Java Annotations (1)-Annotation Basics

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.