Java custom annotation to get annotation values annotation set in the annotation class

Source: Internet
Author: User

Java annotation mechanism is widely used in various frameworks, and some values can be set in annotations.

To get the annotation value set in the annotation class annotation: traverse the method in the custom annotation, reflect the execution method, and the result is the corresponding annotation value.

Java code example:

Package Com.doctor.spring.core;import Java.lang.annotation.documented;import Java.lang.annotation.ElementType; Import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.target;import java.lang.reflect.method;/** * Custom annotation to get the annotation value set in the annotation class annotation That is: Iterate through the methods in the custom annotation, reflect the execution method, and the result is the corresponding annotation value. * * For complex annotation processing see: {@code annotationutils} * * @author doctor * * @time January 23, 2015 PM 6:15:31 */public Class Annotationpract Ice {public static void main (string[] args) throws Reflectiveoperationexception {myannotation myannotation = Annotationp. Class.getannotation (Myannotation.class); System.out.println (myannotation);//output://@com. Doctor.spring.core.annotationpractice$myannotation (value= annotationp,//num=12, address=[1, 2]) for (Method Method:myAnnotation.annotationType (). Getdeclaredmethods ()) {if (! Method.isaccessible ()) {method.setaccessible (true);} Object invoke = Method.invoke (myannotation); System.out.println ("Invoke METHD" + method.getname ()+ "Result:" + Invoke); if (Invoke.getclass (). IsArray ()) {object[] temp = (object[]) invoke;for (Object o:temp) {SYSTEM.O Ut.println (o);}} Output://Invoke METHD num result:12//invoke METHD value result:annotationp//invoke METHD address Result:[ljava.lang.stri ng;@74a14482//1//2} @Retention (Retentionpolicy.runtime) @Target (elementtype.type) @Documentedprivate @interface myannotation {String value () default ""; int num () default 100; String[] Address () default {};} @MyAnnotation (value = "Annotationp", num =, address = {"1", "2"}) private static class Annotationp {}}


Java custom annotation to get annotation values annotation set in the annotation class

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.