首页 > 代码库 > 利用反射调用注解

利用反射调用注解

 利用反射调用注解  

package net.jeesite.java;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(value = RetentionPolicy.RUNTIME)
@interface Meta {

    String length();

    String name();

    int age();

}

public class MetaTest {

    @Meta(age = 0, length = "2", name = "测试注解")
    public static void myMetch() throws NoSuchMethodException, SecurityException {
        MetaTest obj = new MetaTest();

        Class<?> c = obj.getClass();
        
        System.out.println("c:"+c.getConstructors().length);

        Method m = c.getMethod("myMetch");
        System.out.println("m:"+m.getName());
        Meta meta = m.getAnnotation(Meta.class);

        System.out.println("meta.length():" + meta.length());
        System.out.println("meta.name():" + meta.name());
        System.out.println("meta.age():" + meta.age());

    }

    public static void main(String[] args) {

        try {
            myMetch();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

}

 运行结果: 

meta.length():2
meta.name():测试注解
meta.age():0

利用反射调用注解