閱讀390 返回首頁    go 阿裏雲 go 技術社區[雲棲]


關於@interface 自定義注解


自定義注解的話主要就3件事
1.定義注解
  
Java code?
1
2
3
4
  @Retention(RetentionPolicy.RUNTIME) 
  @Target(ElementType.METHOD) 
  public @interface Test { } 
   



2.加注解到方法上,也可能是類上,變量上
  

Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  public class Foo { 
    @Test public static void m1() { } 
    public static void m2() { } 
    
    @Test public static void m3() { 
       throw new RuntimeException("Boom"); 
    
 
    public static void m4() { } 
 
    @Test public static void m5() { } 
 
   public static void m6() { } 
 
    @Test public static void m7() { 
       throw new RuntimeException("Crash"); 
    
 
    public static void m8() { } 
  
   

3.使用注解
  這就涉及到反射的東西,lz去查java反射機製的東西吧
  
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  import java.lang.reflect.*; 
  public class RunTests { 
    public static void main(String[] args) throws Exception { 
      int passed = 0, failed = 0
        for (Method m : Class.forName(args[0]).getMethods()) { 
          if (m.isAnnotationPresent(Test.class)) { //if(方法有@Test注解)
            try 
              m.invoke(null); 
              passed++; 
            catch (Throwable ex) { //方法裏拋出異常
              System.out.printf("Test %s failed: %s %n", m, ex.getCause()); 
            failed++; 
            
          
     } 
     System.out.printf("Passed: %d, Failed %d%n", passed, failed); 
   } 
  } 
   



1 Target
指定所定義的annotation可以用在哪些程序單元上
如果Target沒有指定,則表示該annotation可以使用在任意程序單元上
代碼
    @Target({ElementType.ANNOTATION_TYPE,
             ElementType.CONSTRUCTOR,
             ElementType.FIELD,
            ElementType.LOCAL_VARIABLE,
             ElementType.METHOD,
             ElementType.PACKAGE,
             ElementType.PARAMETER,
             ElementType.TYPE})
    public @interface TODO {}


2  Retention
指出Java編譯期如何對待annotation
annotation可以被編譯期丟掉,或者保留在編譯過的class文件中
在annotation被保留時,它也指定是否會在JVM加載class時讀取該annotation
代碼
   @Retention(RetentionPolicy.SOURCE)  // Annotation會被編譯期丟棄
   public @interface TODO1 {}
   @Retention(RetentionPolicy.CLASS)   // Annotation保留在class文件中,但會被JVM忽略
   public @interface TODO2 {}
   @Retention(RetentionPolicy.RUNTIME) // Annotation保留在class文件中且會被JVM讀取
   public @interface TODO3 {}


3  Documented
指出被定義的annotation被視為所熟悉的程序單元的公開API之一
被@Documented標注的annotation會在javadoc中顯示,這在annotation對它標注的元素被客戶端使用時有影響時起作用
d, Inherited
該meta-annotation應用於目標為class的annotation類型上,被此annotattion標注的class會自動繼承父類的annotation

4  Annotation的反射
我們發現java.lang.Class有許多與Annotation的反射相關的方法,如getAnnotations、isAnnotationpresent
我們可以利用Annotation反射來做許多事情,比如自定義Annotation來做Model對象驗證
代碼
    @Retention(RetentionPolicy.RUNTIME)
   @Target({ ElementType.FIELD, ElementType.METHOD })
   public @interface RejectEmpty {
        /** hint title used in error message */
        String value() default "";
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target( { ElementType.FIELD, ElementType.METHOD })
   public @interface AcceptInt {
       int min() default Integer.MIN_VALUE;
       int max() default Integer.MAX_VALUE;
       String hint() default "";
   }
使用@RejectEmpty和@AcceptInt標注我們的Model的field,然後利用反射來做Model驗證

最後更新:2017-04-03 12:55:24

  上一篇:go SQL UNION 和 UNION ALL 操作符
  下一篇:go C# 自定義數字格式字符串 ToString ###,###,###,##0