Java Custom Annotations

Source: Internet
Author: User

One: Define annotation Entities

ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/*** Class Description: Pagination annotations * @version1.0 createdate:2015-2-10 * * @history: * @updateDate @updatePerson @declare **/@Retention (retentionpolicy.runtime) @Target ({elementtype.parameter,elementtype.type,elementtype.method, Elementtype.field}) Public@InterfacePageranno {String value ();}

1: meta-annotations refer to annotations. including @Retention @Target @Document @Inherited four kinds
2: @Retention of meta-annotations (reserved)
@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file

@Retention (retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but are not available at run time .@Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time3: @Target of meta-annotations
@Target (Elementtype.type)//interfaces, classes, enumerations, annotations@Target (Elementtype.field)//fields, constants for enumerations@Target (Elementtype.method)//Method@Target (Elementtype.parameter)//Method parameters@Target (Elementtype.constructor)//constructor@Target (elementtype.local_variable)//local variable@Target (Elementtype.annotation_type)//annotations@Target (elementtype.package)///package
4: @document of meta-annotations
@Document: Note that the note will be included in the Javadoc
5: @inherited of meta-annotations
@Inherited: The description subclass can inherit the annotation from the parent class Two: Using annotations
 PackageCommons.anno;ImportOrg.apache.ibatis.annotations.Param;/*** Class Description: Annotated host * @version1.0 createdate:2015-2-11 * * @history: * @updateDate @updatePerson @declare **/@PagerAnno ("Comment to class Appltest") Public classappltest {@PagerAnno ("Comment to class member variable ...")    PrivateString name; @PagerAnno ("Comment to Method Syshi")     Public voidSyshi () {System.out.println ("Hi ..."); } @PagerAnno ("Comment to Method Syshello")     Public voidSyshello (@Param ("MyBatis Annotated-@param") @PagerAnno ("comment to method parameter string name") (String name) {System.out.println ("Hello"); }}

Three: Get annotations
 PackageCommons.anno;Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportOrg.apache.ibatis.annotations.Param;/*** Class Description: annotation Test portal * @version1.0 createdate:2015-2-11 * * @history: * @updateDate @updatePerson @declare **/ Public classAcce { Public Static voidMain (string[] args) {//printing annotations on a classannotation[] Annoarry = appltest.class. Getannotations ();  for(Annotation anno:annoarry) {if(annoinstanceofPageranno)            {System.out.println (((Pageranno) anno). Value ()); }} method[] Methodarry= Appltest.class. GetMethods ();  for(Method method:methodarry) {//annotations on the printing method             for(Annotation anno:method.getAnnotations ()) {if(annoinstanceofPageranno)                {System.out.println (((Pageranno) anno). Value ()); }            }            //to print annotations on a method parameter             for(annotation[] annos:method.getParameterAnnotations ()) { for(Annotation anno:annos) {if(annoinstanceofPageranno)                    {System.out.println (((Pageranno) anno). Value ()); }Else if(annoinstanceofParam)                    {System.out.println (((Param) anno). Value ()); }                }            }        }                //to print annotations on a member variablefield[] Fieldarry = appltest.class. Getdeclaredfields ();  for(Field field:fieldarry) { for(Annotation anno:field.getAnnotations ()) {if(annoinstanceofPageranno)                {System.out.println (((Pageranno) anno). Value ()); }            }        }    }}



Java Custom 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.