Application of Java annotation

Source: Internet
Author: User

One, Annotation example

Override Annotation

1 @Override 2  Public void onCreate (Bundle savedinstancestate);
Second, the concept and role of Annotation

  

1 Concepts

An annotation was a form of metadata, that can being added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations has no direct effect on the operation of the code they annotate.

Syntax metadata that can be added to the Java source code. Classes, methods, variables, parameters, and packages can be annotated, and can be used to correlate information metadata with program elements. Annotation Chinese is often translated as "annotations".

  

1 use

A. Tag that tells the compiler some information
B. Compile-time dynamic processing, such as dynamically generated code
C. Run-time dynamic processing, such as getting annotation information

Iii. Classification of Annotation

1 Standard Annotation

Including the override, Deprecated, suppresswarnings, Standard Annotation refers to the Java comes with a few Annotation, the above three represents the overriding function, is not encouraged to use (better way, use risky or no longer maintenance) , ignoring an item Warning

2 USD Annotation

@Retention, @Target, @Inherited, @Documented, Meta Annotation refers to the Annotation that is used to define Annotation, which is explained in detail later in the Annotation customization section

3 Custom Annotation

Custom Annotation represents the Annotation that you define as needed, which you need to use to define the meta Annotation
This is only a classification, but also according to the scope of the source code, compile time, run-time Annotation, later in the custom Annotation will be described in detail.

Iv. Annotation Custom 1 calls
1  Public classApp {2 3 @MethodInfo (4Author = "Trinea.cn+[email protected] ",5Date = "2014/02/14",6Version = 2)7      PublicString Getappname () {8         return"Trinea";9     }Ten}

Here is an example of invoking a custom annotation--methodinfo, MethodInfo Annotation function to add related information to a method, including author, date, version.

2 Definitions
1 @Documented2 @Retention (retentionpolicy.runtime)3 @Target (Elementtype.method)4 @Inherited5  Public@InterfaceMethodInfo {6 7String author ()default"[Email protected]";8 9 String Date ();Ten  One     intVersion ()default1; A}

This is the implementation part of MethodInfo.
(1). By @interface definition, the annotation name is the custom annotation name
(2). The annotation configuration parameter is named the method name of the annotation class, and:
A. All methods have no method body, no arguments, no modifiers, only public & abstract modifiers are allowed, default is public, no throw exceptions are allowed
B. Method return values can only be basic types, String, Class, annotation, enumeration, or their one-dimensional array
C. If there is only one default property, you can use the value () function directly. An attribute does not indicate that the Annotation is a Mark Annotation
(3). You can add the default value

3 USD Annotation

Whether the @Documented will be saved to the Javadoc document
@Retention retention time, optional value source (source), class (Compile time), runtime (runtime), default to CLASS, the value of most of the source is Mark Annotation, such Annotation are mostly used to verify, such as Override, Deprecated, suppresswarnings
@Target can be used to decorate which program elements, such as TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER, etc., are not labeled to indicate that all
Whether the @Inherited can be inherited by default to False

V. Annotation parsing 1 Runtime Annotation parsing

(1) Run-time Annotation refers to the runtime of the Annotation, you can manually call the following common API parsing @Retention

Method.getannotation (annotationname.  Class); Method.getannotations (); Method.isannotationpresent (annotationname. class);

Other @Target similar to the Field,class method
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

(2) Analytical examples such as the following

1  Public Static voidMain (string[] args) {2     Try {3Class cls = Class.forName ("cn.trinea.java.test.annotation.App");4          for(Method method:cls.getMethods ()) {5MethodInfo MethodInfo =Method.getannotation (6MethodInfo.class);7             if(MethodInfo! =NULL) {8System.out.println ("Method name:" +method.getname ());9System.out.println ("method Author:" +Methodinfo.author ());TenSystem.out.println ("method version:" +methodinfo.version ()); OneSystem.out.println ("Method Date:" +methodinfo.date ()); A             } -         } -}Catch(ClassNotFoundException e) { the e.printstacktrace (); -     } -}

Application of Java annotation

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.