JAVA custom annotations and parameter acquisition __java

Source: Internet
Author: User
Package com.java.annotation;

Import java.lang.annotation.*;

/**
 * Created by LW on 14-5-30.
 * Custom Annotation *
 /
@Documented
@Target (elementtype.method)
@Retention (retentionpolicy.runtime)
Public @interface MethodInfo {public
    String Value () default "no description";
}
/* Four defined Data

@Target (elementtype.type)           : Interface, class, enumeration, annotation
@Target (Elementtype.field)          : Field , enumerated constants
@Target (Elementtype.method)         : Method
@Target (Elementtype.parameter)      : Method parameter
@Target ( Elementtype.constructor)    : Constructor
@Target (elementtype.local_variable): local variable
@Target ( Elementtype.annotation_type): note
@Target (elementtype.package)        : Package

@Retention ( Retentionpolicy.source)  : Valid in the source file (that is, source file retention)
@Retention (retentionpolicy.class)   : Valid in class file (that is, class reservation)
@Retention (retentionpolicy.runtime): Valid at run time (that is, run-time reservation)

@Inherited: Description The subclass can inherit the annotation from the parent class

@Document: Indicates that the annotation will be included in Javadoc.


Package com.java.annotation;
Import java.lang.annotation.Annotation;

Import Java.lang.reflect.Method;
 /** * Created by LW on 14-5-30. * * public class Test {/** * get annotation parameter * * @param annotationclass Annotation class * @param annotationfield annotation class word
    Segment name * @param aclass using the annotation's class name * @param methodname the method name using the annotation * @throws Exception * * public static void Getannotationpar (class Annotationclass, String Annotationfield, Class AClass, String methodname) thro
        WS Exception {method Aclassmethod = Aclass.getmethod (methodname);
        Annotation Annotation = aclassmethod.getannotation (Annotationclass);
        Method method = Annotation.getclass (). Getdeclaredmethod (Annotationfield);
    System.out.println (Method.invoke (annotation)); The public static void main (string[] args) throws Exception {Test.getannotationpar (Methodinfo.class, "Value",
    Test2annotation.class, "testmyannotation");
   } class Test2annotation { @MethodInfo (Value = "Custom Annotation @methodinfo") public void Testmyannotation () {}}
 

Source: Https://github.com/xiaohulu/util_xiaohulu/tree/master/src/com/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.