美文网首页高级Android知识Android开发
java 自定义注解(翻译)

java 自定义注解(翻译)

作者: 666swb | 来源:发表于2017-05-10 17:31 被阅读47次

    引用:[Java Custom Annotations Example]{http://www.mkyong.com/java/java-custom-annotations-example/}

    在android 的开源项目butterknife,使用注解的方式,解放了android中view的注入,下面通过一个列子学习一下注解。

    在这个例子中,向你展示怎样创建两个自定义注解Annotation---@Test和@TestInfo, 进行单元测试

    1:@Test 注解

    这个@interface告诉java这个一个自定义的注解,然后你可以使用这个注解在类的方法上使用,例如:@Test(enable=false).

    package com.mkyong.test.core;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD) //can use in method only.
    public @interface Test {
    
        //should ignore this test?
        public boolean enabled() default true;
    
    }
    

    笔记:

    @Retention,@Target 都是元注解,(用来标注注解的注解叫做元注解)

    @Retention(RetentionPolicy.RUNTIME) 表示运行时也保留该注解

    @Target(ElementType.METHOD) 表示只能在方法的上面使用它,如果是ElementType.TYPE就表示在类的上面使用。

    2.@TesterInfo 注解

    这个@TesterInfo自定义注解应用到类上,这个展示了其中方法返回的不同类型,有枚举,数组,字符串等

    package com.mkyong.test.core;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE) //on class level
    public @interface TesterInfo {
    
        public enum Priority {
           LOW, MEDIUM, HIGH
        }
    
        Priority priority() default Priority.MEDIUM;
    
        String[] tags() default "";
    
        String createdBy() default "Mkyong";
    
        String lastModified() default "03/01/2014";
    
    }
    

    @Target(ElementType.TYPE) 表示只能就表示在类的上面使用。

    3: 单元测试的例子

    使用自定义的@Test,和@TesterInfo,在这个测试例子中。

    package com.mkyong.test;
    
    import com.mkyong.test.core.Test;
    import com.mkyong.test.core.TesterInfo;
    import com.mkyong.test.core.TesterInfo.Priority;
    
    @TesterInfo(
        priority = Priority.HIGH,
        createdBy = "mkyong.com",
        tags = {"sales","test" }
    )
    public class TestExample {
    
        @Test
        void testA() {
          if (true)
            throw new RuntimeException("This test always failed");
        }
    
        @Test(enabled = false)
        void testB() {
          if (false)
            throw new RuntimeException("This test always passed");
        }
    
        @Test(enabled = true)
        void testC() {
          if (10 > 1) {
            // do nothing, this test always passed.
          }
        }
    
    }
    

    上面的例子中,@Testinfo用在类上,@test用在方法上

    4. 反射获取注解

    下面的这个例子,通过反射的api,去读取自定义注解的信息

    package com.mkyong.test;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    import com.mkyong.test.core.Test;
    import com.mkyong.test.core.TesterInfo;
    
    public class RunTest {
    
     public static void main(String[] args) throws Exception {
    
       System.out.println("Testing...");
    
       int passed = 0, failed = 0, count = 0, ignore = 0;
    
       Class<TestExample> obj = TestExample.class;
    
       // Process @TesterInfo
       if (obj.isAnnotationPresent(TesterInfo.class)) {
    
           Annotation annotation = obj.getAnnotation(TesterInfo.class);
           TesterInfo testerInfo = (TesterInfo) annotation;
    
           System.out.printf("%nPriority :%s", testerInfo.priority());
           System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
           System.out.printf("%nTags :");
    
           int tagLength = testerInfo.tags().length;
           for (String tag : testerInfo.tags()) {
               if (tagLength > 1) {
                   System.out.print(tag + ", ");
               } else {
                   System.out.print(tag);
               }
               tagLength--;
           }
    
           System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());
    
       }
    
       // Process @Test
       for (Method method : obj.getDeclaredMethods()) {
    
           // if method is annotated with @Test
           if (method.isAnnotationPresent(Test.class)) {
    
               Annotation annotation = method.getAnnotation(Test.class);
               Test test = (Test) annotation;
    
               // if enabled = true (default)
               if (test.enabled()) {
    
                 try {
                   method.invoke(obj.newInstance());
                   System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
                   passed++;
                 } catch (Throwable ex) {
                   System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
                   failed++;
                 }
    
               } else {
                   System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());
                   ignore++;
               }
    
           }
    
       }
       System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);
    
       }
    }
    

    结果:

    Testing...
    
    Priority :HIGH
    CreatedBy :mkyong.com
    Tags :sales, test
    LastModified :03/01/2014
    
    1 - Test 'testA' - failed: java.lang.RuntimeException: This test always failed
    2 - Test 'testC' - passed
    3 - Test 'testB' - ignored
    
    Result : Total : 3, Passed: 1, Failed 1, Ignore 1
    

    先是获取类上的注解@TesterInfo的信息,然后获取方法上的注解@Test的信息

    其中在android stuido中运行java文件,可以在android项目的单元测试中编写,然后运行就好。

    相关文章

      网友评论

        本文标题:java 自定义注解(翻译)

        本文链接:https://www.haomeiwen.com/subject/wgygtxtx.html