annotation annotations (active code)

Source: Internet
Author: User

Overview of annotations (annotation):

• Starting with JDK5.0, Java adds support for metadata (MetaData), which is annotation (annotations)

· Annotation is actually a special tag in the code that can be read at compile, class load, run time, and perform the appropriate processing. By using annotation, programmers can embed some supplemental information in the source file without changing the original logic.

· Annotation can be used like modifiers, which can be used to modify the declarations of packages, classes, constructors, methods, member variables, parameters, and local variables, which are stored in the annotation "Name=value" pair.

· Annotation can be used to set metadata for program elements (classes, methods, member variables, etc.)

The basic annotation:

• Use annotation to add the @ symbol in front of it and use the annotation as a modifier. Used to decorate the program elements it supports

• Three basic annotation:

[email protected] Override: Qualifies the overriding parent class method, which can only be used for methods

[email protected] Deprecated: Used to indicate that a program element (class, method, and so on) is obsolete

[email protected] Suppresswarnings: Suppressing compiler warnings

Custom annotation:

• Define a new annotation type using the @interface keyword

· The member variables of the annotation are declared in the annotation definition in the form of a parameterless method. Its method name and return value define the name and type of the member.

• You can set an initial value for an annotation member variable when you define it, and specify the initial value of the member variable to use the default keyword

Cases:

Public @interface myannotation{

String name () default "Liang"

}

• Annotation, which does not have a member definition, is called a tag; annotation that contain member variables is called metadata annotation

Extract annotation Information:

· JDK5.0 adds a annotationelement interface under the Java.lang.reflect package, which represents the program element in the program that can accept annotations

• When a annotation type is defined as visible at run time, the annotation stored in the class file will be read by the virtual machine when the class file is loaded

• The program can call the Annotationelement object as follows to access the annotation information

JDK Meta-annotation:

· The meta-annotation of the JDK is used to decorate other annotation definitions

· JDK5.0 provides annotation types specifically on annotations, namely:

1.Retention

2.Target

3.Documented

4.Inherited

• @Retention: Used only to decorate a annotation definition that specifies how long the annotation can be retained @Retention contains a member variable of type Retentionpolicy. You must specify a value for the value member variable when you use @retention:

1.retentionpolicy.source: The compiler directly discards comments for this policy

2.retentionpolicy.class (default): The compiler will record comments in the CLASS file. The JVM does not preserve annotations when running Java programs. This is the default value

3.retentionpolicy.runtime: The compiler will record the comments in the class file. When you run a Java program, the JVM preserves the comments. The program can get the comment through reflection.

• @Target: Used to modify the annotation definition to specify which program elements the modified annotation can use. @Target also contains a member variable named value.

@Documented: Used to specify the annotation class Jiangbei Javadoc tool that is modified by the meta annotation to be extracted into a document.

1. Annotations defined as documented must have the retention value set to runtime.

• @Inherited: The annotation that it modifies will have inheritance. If a class uses a annotation that is @inherited decorated, its subclasses will automatically have that annotation (less used in the actual application).

1  PackageCom.demo;2 /*3  *4 * How to customize an annotation5 * Meta Annotations6  */7  Public classtestannotation {8      Public Static voidMain (string[] args) {9Person p =NewStudent ();Ten P.walk (); One     } A } -  - classStudentextendsperson{ the  - @Override -      Public voidWalk () { -SYSTEM.OUT.PRINTLN ("Student Walk"); +     } - @Override +      Public voideat () { A         Super. Eat (); at     } - } - classperson{ -@MyAnnotation (value= "Liang") - String name; -     intAge ; in      -      PublicPerson () { to     } +      PublicPerson (String name,intAge ) { -         Super(); the          This. Name =name; *          This. Age =Age ; $     }Panax Notoginseng      Public voidWalk () { -System.out.println ("Walking"); the     } + @Deprecated A      Public voideat () { theSystem.out.println ("Eat Something"); +     } - @Override $      PublicString toString () { $         return Super. toString (); -     } -      the}
1  PackageCom.demo;2 3 Importjava.lang.annotation.Retention;4 ImportJava.lang.annotation.RetentionPolicy;5 ImportJava.lang.annotation.Target;6 ImportJava.lang.reflect.Field;7 ImportJava.lang.annotation.ElementType;8 //Custom Annotations9 @Target ({Elementtype.field})Ten @Retention (retentionpolicy.runtime) One  Public@Interfacemyannotation { AString value ()default"Hello"; -}

annotation annotations (active code)

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.