Summary of Java annotations

Source: Internet
Author: User

Role
    • tag that tells the compiler some information. such as @override, @Deprecated, @SuppressWarnings and so on.
    • Run-time dynamic processing. The annotation information can be obtained by the corresponding function.

      • Getannotation (Annotationname.class) represents the information for a Annotation of the target, because a target can be modified by multiple Annotation
      • Getannotations () indicates that the Target has all Annotation
      • Isannotationpresent (Annotationname.class) indicates whether the Target is modified by a Annotation
    • Dynamic processing at compile time. Generate code dynamically at compile time.

Classification
    • Standard annotation, such as frequently used @override, @Deprecated, @SuppressWarnings, the so-called standard annotation refers to the Java comes with a few annotation
    • Meta-annotation, such as @retention, @Target, @Inherited, @Documented, the so-called Meta annotation refers to the annnotation used to define annotation

      • @Retention
        • Used to specify the retention time for annotations
        • Optional value: Retentionpolicy.source (source time), Retentionpolicy.class (compile time), Retentionpolicy.runtime (runtime)
      • @ Target
        • Used to specify which program elements the annotations can decorate
        • Selectable values: Elementtype.type, Elementtype.method, Elementtype.constructor, Elementtype.field, Elementtype.parameter, etc.
      • @ Inherited
        • Used to specify whether it can be inherited by default to False
      • @ Documented
        • Used to specify whether to save to the Javadoc document
    • custom annotation, sometimes there is a need for their own custom annotation, the process of defining the need to use the meta-annotation

Standard annotation Use

Java comes with a part of the annotations, usually in the casual use, the most used is @override, used to indicate rewriting a function, and then there are @deprecated, indicating that the function has been discarded, no longer recommended to use. @SuppressWarnings used to indicate that an error was ignored.

Run-time annotation parsing

When we add runtime annotations to a class, field, or function, we can dynamically get the information of the annotations at run time.

Class<?> clazz=SomeClass.getClass();  Annotation[] annotations = clazz.getAnnotations();for (Annotation annotation : annotations) {    System.out.println(annotation.annotationType());}
Compile-time annotation parsing

This type of annotation is used to generate code dynamically at compile time.

Writing annotations
@Target({ElementType.FIELD,ElementType.METHOD})@Retention(RetentionPolicy.CLASS)public @interface TestAnnotation {    String name();    intdefault1;}
    • Use @interface to define an annotation, which is a custom annotation name
    • All methods have no method body, parameter, modifier
    • Method return value can only be a basic data type
    • Use default to add defaults
Using annotations
publicclass Person {    @TestAnnotation(name="name")    private String name;    @TestAnnotation(name="walk")    publicvoidwalk(){        System.out.println("walk");    }}
    • Use @ Note name on corresponding target, attribute value is used by property = ""
Parsing annotations
@SupportedAnnotationTypes("Cn.edu.zafu.TestAnnotation") Public  class testprocessor extends abstractprocessor {    @Override     Public Boolean Process(set<? extends typeelement> annotations, roundenvironment roundenv) { for(Typeelement te:annotations) { for(Element element:roundEnv.getElementsAnnotatedWith (TE)) {Testannotation testannotation = element. Getannotation (Testannotation.class);//testannotation.name ()                //testannotation.version ()}        }return true; }}
    • Use the @supportedannotationtypes annotation to represent the full classpath of the annotations supported by this class, supported by wildcard characters
    • Inherit the Abstractprocessor class, and all processing is performed in the process
    • The return value of the Process function indicates whether this set of annotations is accepted by this Processor, and if subsequent Processor are accepted, the annotations will not be processed
Reference articles
    1. Java Annotations Annotation of public technical points

Summary 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.