java 自定義注解(翻譯)

引用:[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項目的單元測試中編寫,然后運行就好。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,292評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • “你記不記得我們還是網友剛剛認識的時候,你就約我出門?” “什么時候啊? 我怎么不記得了?” “...
    steropeg閱讀 683評論 4 3
  • 那一年的秋天,小武河兩岸開滿了美麗的雛菊。十五歲的我在攔河大壩上騎著自行車去往黃山中學的路上。 途徑一大片野菊花的...
    雙面老孫閱讀 550評論 0 1
  • 今天出點小意外,車壞半路了。心情極不好。37度高溫,汗向下流。 很不巧,工作上經理指出,這不合理那不合理,這沒有上...
    沐子芳菲閱讀 196評論 0 0