zoukankan      html  css  js  c++  java
  • 1.2.4 Java Annotation 提要

    (本文是介绍依赖注入容器Spring和分析JUnit源码的准备知识)

    Java Annotation(标注)

    java.lang.annotation.Annotation是全部Java标注的父接口。它除了override/改写Object的equals(Object)、hashCode()和toString()外,仅有一个方法

    Class<? extends Annotation> annotationType()

    返回本标注的的类型。
    JDK中定义的标注java.lang.Override为:
    @Target(value=METHOD)
    @Retention(value=SOURCE)
    public @interface Override{}
    再如java.lang. FunctionalInterface

    @Documented
    @Retention(value=RUNTIME)
    @Target(value=TYPE)
    public @interface FunctionalInterface
    ......
    这些样例反映标注的一些基本内容:
    ①定义一个标注。如public @interface MyAnnotation。不须要extends Annotation而是使用@ interface。

    有一些用于修饰自己定义标注的标注——称为元标注/meta-annotation,如Override样例中的@Target、@Retention和@Documented。其后是括号,括号里是使用逗号切割的名值对。(name=value)

    @Target说明被修饰的标注的适用场合/目标。

    其值由枚举java.lang.annotation.ElementType限定,包含ANNOTATION_TYPE , CONSTRUCTOR , FIELD , LOCAL_VARIABLE , METHOD ,PACKAGE , PARAMETER , TYPE和TYPE_PARAMETER。

    比如Target自己的适用场合是@Target(value=ANNOTATION_TYPE);假设标注仅仅有单一属性成员(名值对),而并成员名为"value="能够省略为@Target(ANNOTATION_TYPE)。

    @Retention说明被修饰的标注的保留策略,由RetentionPolicy枚举。

    • SOURCE仅仅会保留在程序源代码里。或者说仅对编译器有效,如@Override。编译器检查
    • 默认或指定CLASS时,指定该标注写入class文件,可是不会把这些信息载入到JVM中;
    • RUNTIME表示能够通过反射机制获得该标注。

    @Documented指明该标注被反映在JavaDoc中。
    ②标注用来修饰源码的元素(类,方法。属性,參数。本地变量,包,元标注)。
    ③标注的使用有一个明白的底线它们不可影响程序代码的运行。不管添加或删除标注,代码的运行不会有不论什么影响。

    可是,程序能够解析RUNTIME标注。并决定代码的行为


    样例:JUnit4自己定义的annotation

    org.junit.Test

    org.junit.Ignore @Target({ElementType.METHOD, ElementType.TYPE})

    @Before和@After标示的方法仅仅能各有一个,代替了JUnit曾经版本号中的setUp和tearDown方法

    org.junit.BeforeClass @Target(ElementType.METHOD)

    org.junit.Before @Target(ElementType.METHOD)

    org.junit.AfterClass @Target(ElementType.METHOD)

    org.junit.After @Target(ElementType.METHOD)

    org.junit.runner.RunWith

    org.junit.runners.Suite.SuiteClasses

    org.junit.runners.Parameterized.Parameters


    解析RUNTIME标注

    ①定义一个标注。
    package MyTest;
    import java.lang.annotation.*;
    @Target(ElementType.METHOD)  
    @Retention(RetentionPolicy.RUNTIME)  
    public @interface MyAnnotation{
        String value();
    }
    ②标注的使用类
    package MyTest;
    public class HelloWorld {
        @MyAnnotation("Test")
        public double add(double m,double n){
            return m+n;
        }
        @MyAnnotation("Ignore")
        public double add2(double m,double n){
            return m+n;
        }
    }
    ③解析RetentionPolicy.RUNTIME标注。


    通过c获得全部public方法,对每一个方法获取其标注Annotation[],找到自己定义的MyAnnotation,假设其value()的值为"Test",(Double)method.invoke(h,1,2.0);
    package MyTest;
    import java.lang.annotation.*;
    import java.lang.reflect.*;
    public class TestHelloWorld{    
        public static void main(String[] args)throws Exception{
            //直接使用反射机制
            Class<?> c = MyTest.HelloWorld.class;
            HelloWorld h =(HelloWorld)c.newInstance();
            Method[] methods = c.getMethods();
            for(Method method:methods){
                Annotation[] annotations = method.getDeclaredAnnotations();
                for(Annotation annotation : annotations){
                    if(annotation instanceof MyAnnotation){
                        MyAnnotation myAnnotation = (MyAnnotation) annotation;
                        System.out.println("value: " + myAnnotation.value());
                        if(myAnnotation.value().equals("Test") ){
                            double d = (Double)method.invoke(h,1,2.0);
                            System.out.println(d);
                        }
                    }
                }
            }
        }
    }
    输出:
    value: Test
    3.0
    value: Ignore




  • 相关阅读:
    安装win7和ubuntu双系统
    Jenkins的2个问题
    junit里面Test Case的执行顺序
    使用Array类处理基本数组对象
    Location对象的页面跳转方法介绍
    Javascript几种创建对象的方法
    For循环重复代码的重构
    Sonar在ant工程中读取单元测试和覆盖率报告
    Jenkins无法读取覆盖率报告的解决方法
    python之路-day08-文件操作
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5095128.html
Copyright © 2011-2022 走看看