Java註解@interface (入門)

來源:互聯網
上載者:User

標籤:ext   main   utils   height   declare   collect   stat   img   on()   

 註解(也被稱為中繼資料)為我們在代碼中添加資訊提供了一種形式化的方法,使我們可以在稍後某個時刻非常方便地使用這些資料。

--------------------------------------------------------------------------------------------------------------------------------------------

1.定義註解:

 

[java] view plain copy  
  1. package com.test;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Target(ElementType.METHOD)  
  9. @Retention(RetentionPolicy.RUNTIME)  
  10. public @interface UseCase {  
  11.     public int id();  
  12.     public String description() default "no description";  
  13. }  

 

2.在下面的類中,有三個方法被註解為用例:

[java] view plain copy  
  1. package com.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class PasswordUtils {  
  6.     @UseCase(id = 47, description = "Passwords must contain at lease one numeric")  
  7.     public boolean validatePassword(String password) {  
  8.         return password.matches("\\w*\\d\\w*");  
  9.     }  
  10.   
  11.     @UseCase(id = 48)  
  12.     public String encryptPassword(String password) {  
  13.         return new StringBuilder(password).reverse().toString();  
  14.     }  
  15.   
  16.     @UseCase(id = 49, description = "New password can‘t equal previously used ones")  
  17.     public boolean checkForNewPassword(List<String> prevPasswords, String password) {  
  18.         return !prevPasswords.contains(password);  
  19.     }  
  20. }  
3.編寫註解處理器:
[java] view plain copy  
  1. package com.test;  
  2.   
  3. import java.lang.reflect.*;  
  4. import java.util.*;  
  5.   
  6. public class UseCaseTracker {  
  7.     public static void trackUseCases(List<Integer> useCases, Class<?> cl) {  
  8.         for (Method m : cl.getDeclaredMethods()) {  
  9.             UseCase uc = m.getAnnotation(UseCase.class);  
  10.             if (uc != null) {  
  11.                 System.out.println("Found Use Case:" + uc.id() + " " + uc.description());  
  12.                 useCases.remove(new Integer(uc.id()));  
  13.             }  
  14.         }  
  15.         for (int i : useCases) {  
  16.             System.out.println("Warning: Missing use case-" + i);  
  17.         }  
  18.     }  
  19.   
  20.     public static void main(String[] args) {  
  21.         List<Integer> useCases = new ArrayList<Integer>();  
  22.         Collections.addAll(useCases, 47, 48, 49, 50);  
  23.         trackUseCases(useCases, PasswordUtils.class);  
  24.     }  
  25. }  

輸出:

Found Use Case:49 New password can‘t equal previously used ones
Found Use Case:47 Passwords must contain at lease one numeric
Found Use Case:48 no description
Warning: Missing use case-50

 
頂
1

Java註解@interface (入門)(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.