Happybks teaches you to write Java annotations (1)--classification, operation mechanism, scope and concept summary of annotations

Source: Internet
Author: User
Tags deprecated

This is something that has become commonplace in our programming life. A method that overrides a parent class, applying the programming routines in spring, MyBatis, writing JUnit test functions, and so on. You will find that as a Java coder, you are always in touch with them.

But how to write a note of your own, or want to be able to understand the NB framework of the source code, it is necessary for me to master the method of writing custom annotations.

At the beginning of this series, I will study the development of Java annotations with my blog audience.


Classification of annotations (by source)

Annotations in the JDK:

@Override Override the method of the parent class. Compile an error if the parent class does not have the method

@Overridepublic String toString () {return "Member [id=" + ID + ", familyname=" + familyname+ ", givenname=" + GivenName + ", salary=" + salary + "]";}

@Deprecated previously defined classes or methods, you are using high, but you feel as if the design is not good, and then added new classes or methods to replace them. However, in doing so, the former partners have to complain, "you this version is incompatible" and so on, so we need to use an annotation to indicate "existing but outdated, usable but not recommended", this is @Deprecated.

For example, because now the cat is not catching mice, so, the following method, I decided to discard. But there have been too many methods called around the cat, so I added the annotation @Deprecated. Strikethrough in the IDE is an eye-catching indicator.

Oops, it seems that after calling the annotation @Deprecated method, there is a warning!

Happybks I'm a code freak, be sure to get rid of this warning.

Similar to the way we write code, it's often inevitable that there will be some caveats, some of which we can modify to eliminate warnings based on good paradigms, good habits, and good experience. But sometimes some scenes seem to ask us to tolerate them. What do we do? At this time, you can use @suppresswarnings annotations to eliminate the corresponding warning type.

For example, the above call to discard the warnings generated by the method, we can change to this:

Now the warning is gone! : )


Third-party annotations:

If you are a Java framework talent, you must know Spring @autowired auto-creation, as well as @service and @repository, will also be used MyBatis @insertprovider, @UpdateProvider, @Option.

As a framework person, we have to deal with these third-party annotations. The framework is not covered here.


Custom annotations:

If we want to read someone else's code, try to write a frame, and install a B, it is necessary to write an annotation yourself. This is also the goal of this series of articles!!


In fact, there should be a kind of more special annotations, do not know which category to classify it, that is the meta-annotation.

Meta-annotations: refers to annotations. Just as the metadata is the data that describes the data. Specific I will be in a separate article to introduce.


annotation operating mechanism, scope, inheritance, etc.:

I classify the annotations and classify them according to the operating mechanism.

The operation mechanism of annotations:

Depending on the operating mechanism, we can divide the annotations into three categories:

Source Annotations : annotations exist only in the source code and are compiled into a. class file that does not exist.

compile-time annotations : Annotations exist inside the. class file. Of course there are in the. class file, which of course exists in the source code. The 3 JDK @Deprecated,@SuppressWarnings, and @Override that were just mentionedare compile-time annotations. Because of this, when you @Override a method that the parent class does not exist, the compiler will make an error at compile time, and the compiler will warn you to call the @Deprecated method. The IDE can prompt for errors in real-time when editing code.

Run-time annotations : It also works in the run phase and even affects the annotations that run the logic.

How to specify the operating mechanism of an annotation: (that is, the point in time of impact)

When defining an annotation, we also need to indicate whether it is a source annotation, a compile-time annotation, or a run-time annotation. An enumeration is also needed here:

/* * copyright  (c)  2003, 2004, oracle and/or its affiliates.  All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is  Subject to license terms. */package java.lang.annotation;/** * annotation  retention policy.  the constants of this enumerated type *  describe the various policies for retaining annotations.  they  are used * in conjunction with the {@link  Retention}  Meta-annotation type to specify * how long annotations are to  be retained. * *  @author   Joshua Bloch *  @since  1.5  */public enum retentionpolicy {    /**     *  annotations are to be discarded by the compiler.     */     source,    /**     * annotations are to  be recorded in the class file by the compiler      * but need not be retained by the vm at run  time.  This is the default     * behavior.      */    CLASS,    /**      * annotations are to be recorded in the class file by  the compiler and     * retained by the vm at  run time, so they may be read reflectively.      *     *  @see  java.lang.reflect.AnnotatedElement     */     runtime}


Scope of annotations:

The scope of the annotation refers to what syntax unit a callout modifies. Annotations identify a class, a method, a domain, a constructor, and so on. Here, when specifying the scope of the annotation, an enumeration is used to specify the type of scope, where I list the source code for the JDK:

package java.lang.annotation;/** * a program element type.  the  constants of this enumerated type * provide a simple  Classification of the declared elements in a * java program.  * * <p>these constants are used with the {@link   target} meta-annotation type * to specify where it is legal  to use an annotation type. * *  @author   joshua bloch *   @since  1.5 */public enum elementtype {    /** class,  interface  (Including annotation type), or enum declaration */     TYPE,    /** Field declaration  (includes enum  Constants) &NBSP;*/&NBSP;&NBSP;&NBSP;&NBSp field,    /** method declaration */    method,     /** Parameter declaration */    PARAMETER,     /** Constructor declaration */    CONSTRUCTOR,     /** Local variable declaration */    LOCAL_VARIABLE,     /** annotation type declaration */    annotation_ Type,    /** package declaration */    package}



The inheritance of annotations:

Specifies whether an annotation will take effect in the subclass of the class after labeling a class, interface, or method, and so on. I'll write an introduction to this in a separate article in the later series.


Annotation output to document:

This is the @docmented annotation that specifies whether the annotations are exported to the Java document.

Documented annotations indicate that this annotation should be recorded by the Javadoc tool. By default, Javadoc is not included in annotations. However, if a @Documented is specified when the annotation is declared, it is processed by a tool such as Javadoc, so the annotation type information is also included in the generated document.


Specifically how to write a note of your own. Put it in the next article.











Happybks teaches you to write Java annotations (1)--classification, operation mechanism, scope and concept summary of 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.