码迷,mamicode.com
首页 > 编程语言 > 详细

Java注解@interface (入门)(转)

时间:2017-04-13 16:41:51      阅读:235      评论:0      收藏:0      [点我收藏+]

标签: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 (入门)(转)

标签:ext   main   utils   height   declare   collect   stat   img   on()   

原文地址:http://www.cnblogs.com/yuwenlanleng/p/6704518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!