反射取得Annotation

来源:互联网 发布:淘宝返利源码 编辑:程序博客网 时间:2024/06/16 09:32

1,颠覆性的开发技术
2,自定义Annotation需要代码容器
3,取得Annotation信息

@Deprecatedclass Memebr{}public class AnnotationTest {    public static void main(String[] args) {        Class clazz = Memebr.class;        Annotation[] ano = clazz.getAnnotations();        for (Annotation annotation : ano) {            System.out.println(annotation);        }    }}运行结果@java.lang.Deprecated()

4,自定义Annotation

//源代码中生效,比如重写方法的注解@Override@Retention(RetentionPolicy.SOURCE)//类定义的时候生效(我的理解是编译时有效)@Retention(RetentionPolicy.CLASS)//表示运行时生效@Retention(RetentionPolicy.RUNTIME)@interface MyAnnotation{    public String name() default "张三";}@MyAnnotation(name="李四")class Memebr{}public class AnnotationTest {    public static void main(String[] args) {        Class clazz = Memebr.class;        Annotation[] ano = clazz.getAnnotations();        for (Annotation annotation : ano) {            System.out.println(annotation);        }    }}运行结果:@test.MyAnnotation(name=李四)