注解小用例2

需求:对Calculator的加减乘除方法检查是否正确
如果有问题,将出现问题的方法名称和问题名称、原因、次数记录到文件中

Calculator类
 1 package day2_17;
 2 
 3 /**
 4  * @Author Tianhao
 5  * @create 2021-02-17-15:12
 6  */
 7 public class Calculator {
 8     @Check
 9     public void add() {
10         String str = null;
11         str.toString();
12         System.out.println("1+0=" + (1 + 0));
13     }
14     @Check
15     public void sub() {
16         System.out.println("1-0=" + (1 - 0));
17 
18     }
19     @Check
20     public void mul() {
21         System.out.println("1*0=" + (1 * 0));
22 
23     }
24     @Check
25     public void div() {
26         System.out.println("1/0=" + (1 / 0));
27 
28     }
29     public void show() {
30         System.out.println("永远没有bug");
31     }
32 
33 }

自定义注解  Check

 1 package day2_17;
 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 /**
 9  * @Author Tianhao
10  * @create 2021-02-17-15:16
11  */
12 
13 @Target(ElementType.METHOD)
14 @Retention(RetentionPolicy.RUNTIME)
15 public @interface Check {
16 
17 }
检查功能类 CheckTest
 1 package day2_17;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.lang.reflect.Method;
 7 
 8 /**
 9  * 需求:对Calculator的加减乘除方法检查是否正确
10  *  如果有问题,将出现问题的方法名称和问题名称、原因、次数记录到文件中
11  *
12  *
13  * @Author Tianhao
14  * @create 2021-02-17-15:18
15  */
16 public class CheckTest {
17     public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException {
18         //1.获取需要测试的Calculator类型对象
19         Class<?> cls = Class.forName("day2_17.Calculator");
20         Object obj = cls.newInstance();
21         //2.获取Calculator类的所有方法
22         Method[] methods = cls.getMethods();
23         int count = 0;//记录异常次数
24         BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
25         //3.遍历所有方法,看是否有@Check注解
26         for (Method method : methods) {
27             //4.如果有,就执行方法
28             if (method.isAnnotationPresent(Check.class)) {
29                 try {
30                     method.invoke(obj);
31                 } catch (Exception e) {
32                     //5.捕获异常,并记录到文件中
33                     count++;
34                     bw.write(method.getName() + "方法出现异常");
35                     bw.newLine();
36                     bw.write("异常的名称:" + e.getCause().getClass().getSimpleName());
37                     bw.newLine();
38                     bw.write("异常的原因:" + e.getCause().getMessage());
39                     bw.newLine();
40                     bw.write("----------------------------");
41                     bw.newLine();
42                 }
43             }
44         }
45         bw.write("本次测试共出现 " + count + " 次异常");
46         bw.flush();
47         bw.close();
48     }
49 }

生成的bug.txt 文件

add方法出现异常
异常的名称:NullPointerException
异常的原因:null
----------------------------
div方法出现异常
异常的名称:ArithmeticException
异常的原因:/ by zero
----------------------------
本次测试共出现 2 次异常

 

 

 




posted @ 2021-02-17 15:49  dog_IT  阅读(69)  评论(0编辑  收藏  举报