Java Magic Hall: Customizing and parsing annotations

Source: Internet
Author: User

First, preface

Annotations (Annotation) as the carrier of the metadata, for the program code itself to provide additional information, the use of MyBatis and other ORM framework Friends of the @Insert should be familiar with the annotations, this is MyBatis custom annotations, Obviously we can also customize some annotations on demand, then parse them to get the metadata, and then implement the code generation through code.

Ii. Custom Annotations

Customize annotations with just the keyword @interface

//identifying annotations (that is, annotations without attributes) Public@interface annotationwithoutproperty{}//annotations with the value attribute Public@interface annotationwithval{String value ();}//annotations with the Myval attribute Public@interface annotationwithmyval{string[] myvalue ();}//annotations with the value and Myval attributes Public@interface annotationwith2val{String value (); String[] MyValue ();}//annotations for the Myval property with default values Public@interface annotationwithdefaultval{String myval ()default "Hello world!";}

Use the following methods:

@AnnotationWithoutProperty @annotationwithval ("Hello World")//The Value property is assigned without explicitly writing out the property name@AnnotationWithMyVal (myvalue={"Hello"," World"})//When you assign a value to another property, you must display the property name@AnnotationWith2Val (value="Hello World", myval={"Hello"," World"}) @AnnotationWithDefaultVal//property has a default value, you do not have to display the Set property value@AnnotationWithDefaultVal ("New Value") Public voidTest () {}

Iii. Annotations of annotations

Annotations are the ability to constrain or enhance annotations by providing additional information to the annotations themselves. There are 4 kinds of annotations including @Documented , @Inherited , @Target , Retention .

@Target Annotations : Used to constrain the use of the described annotations, and compilation fails when the described annotations go beyond the scope of use.

// the scope of constraint @myannotation is function and constructor @Target (Elementtype.method, Elementtype.constructor)  public @interface myannotation{}

@Retention Note : Used to constrain the scope of the described annotation, the annotation has a range of three, respectively

1. retentionpolicy.source , the scope of the source code, is only in the Java file, when the execution of the javac command will be stripped of the note.

2. retentionpolicy.class , the scope of the binary code, is in the CLASS file, when executing the java command will be stripped of the note.

3. retentionpolicy.runtime , the scope of the operation is that we can obtain the annotation dynamically through reflection.

@Retention (retentionpolicy.runtime)  public @interface myannotation{}

@Documented Note : This annotation information is displayed when you specify Javadoc to generate an API document

@Inherited Annotations : Used to specify that the annotations described can be inherited by subclasses of the classes they describe. Default Condition

// default annotations do not inherit from the quilt class @MyAnnotation  Public class parent{} // son does not inherit annotations myannotation  Public class Son extends parent{}

by @Inherited subclasses will inherit the @MyAnnoation annotations of the parent class.

Iv. reading annotations

By reflection we can get annotations on classes, functions, and so on.

@Retention (retentionpolicy.runtime) @Target (elementtype.class) @Documented Public@interface myannotaion{String value ()default "Hello World";} @MyAnnotation Public classtest{ Public Static voidMain (string[] args) {myannotation ma= Test.class. getannotation (myannotation.class); System. out. println (Ma.value ()); //getting annotations that inherit from and from the parent classannotation[] Annotations = Test.class. Getannotations (); //get annotations for themselves onlyannotation[] Annotations = Test.class. Getdeclaredannotations ();  }}    

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/4040929.html ^_^ Fat Boy John

V. References

Http://www.cnblogs.com/liubiqu/archive/2008/06/01/1211503.html

Java Magic Hall: Customizing and parsing 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.