Java Custom Annotations and reflections

Source: Internet
Author: User

Java Annotations and Reflections
In Java, four kinds of meta-annotations are provided, which are specifically responsible for annotating other annotations, as follows
1, @Retention meta-annotations, indicating the level at which the annotation information (life cycle) needs to be saved. The optional retentionpoicy parameters include:
Retentionpolicy.source: Stuck in Java source file, compiler was discarded
Retentionpolicy.class: Stays in CLASS file, but is discarded by VM (default)
Retentionpolicy.runtime: In-memory bytecode, VMS will also retain annotations at run time, so the annotations can be read through the reflection mechanism
2, @Target yuan annotation, the default value is any element, indicating where the annotation is used. The available ElementType parameters include
Elementtype.constructor: Constructor declaration
Elementtype.field: Member variables, objects, attributes (including enum instances)
Elementtype.local_variable: local variable declaration
Elementtype.method: Method declaration
Elementtype.package: Package Declaration
Elementtype.parameter: Parameter declaration
Elementtype.type: Classes, interfaces (including annotation types) or enum declarations
3. @Documented to include annotations in Javadoc
4. @Inheried allow subclasses to inherit annotations from parent class
Ii. Definition of annotations
1, the format of the annotation definition
Public @interface Fieldmeta {}

 PackageCom.http.model;Importjava.lang.annotation.Documented;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.Target;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.ElementType, @Retention (retentionpolicy.runtime)//annotations exist in the class bytecode file and can be retrieved at run time through reflection@Target ({Elementtype.field})//define the purpose of the annotation * * scope field, enumerated constant/method@Documented//Note that the note will be included in the Javadoc Public@InterfaceFieldmeta {String name ()default""; String description ()default"";} 


2, the application of annotations in the entity class
@FieldMeta (name= "serial number", description= "")
Private Stringid;
@FieldMeta (name= "name", description= "")
Private Stringtitle;

 PackageCom.http.model; Public classmodeltest {@FieldMeta (name= "Serial number", description= "")    PrivateString ID; @FieldMeta (Name= "title", description= "title Information description")      PrivateString title;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetTitle () {returntitle; }     Public voidSettitle (String title) { This. title =title; }}

III. Traversal reflection of annotations

    //reflection Gets object property name and annotation information     Public Static voidGetField () {Try{Class Clazz= Class.forName ("Com.http.model.HttpRequest"); field[] Field=Clazz.getdeclaredfields (); //System.out.println (field.length);             for(Field F:field) {BooleanIsexist=f.isannotationpresent (Fieldmeta.class); if(isexist) {Fieldmeta TT=f.getannotation (Fieldmeta.class); System.out.println ("--Annotations:" +tt.id () + "" +tt.name ());            } System.out.println (F.getname ()); }        } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }     }    

Reflection Get Object Method name
Public Static voidGetMethod () {Try{Class Clazz= Class.forName ("Com.http.model.HttpRequest"); Method[] Method=Clazz.getdeclaredmethods (); System.out.println (method.length); for(Method f:method) {System.out.println (F.getname ()); //Get method Name } } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }

Java Custom Annotations and reflections

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.