1. 程式人生 > 其它 >JavaWeb1.3.4【基礎加強:註解案例-簡單的測試框架】

JavaWeb1.3.4【基礎加強:註解案例-簡單的測試框架】

 1 package com.yubaby.annotation.p3;
 2 
 3 /*
 4 * 案例:簡單的測試框架
 5 
 6 
 7 * 小結:
 8     1. 以後大多數時候,我們會使用註解,而不是自定義註解
 9     2. 註解給誰用?
10         1. 編譯器
11         2. 給解析程式用
12     3. 註解不是程式的一部分,可以理解為註解就是一個標籤
13  */
14 public class Calculator {
15 
16     //加法
17     @Check
18     public void add(){
19         String str = null
; 20 str.toString(); 21 System.out.println("1 + 0 =" + (1 + 0)); 22 } 23 24 //減法 25 @Check 26 public void sub(){ 27 System.out.println("1 - 0 =" + (1 - 0)); 28 } 29 30 //乘法 31 @Check 32 public void mul(){ 33 System.out.println("1 * 0 =" + (1 * 0));
34 } 35 36 //除法 37 @Check 38 public void div(){ 39 System.out.println("1 / 0 =" + (1 / 0)); 40 } 41 42 public void show(){ 43 System.out.println("永無bug..."); 44 } 45 }
 1 package com.yubaby.annotation.p3;
 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 @Retention(RetentionPolicy.RUNTIME) 9 @Target(ElementType.METHOD) 10 public @interface Check { 11 }
 1 package com.yubaby.annotation.p3;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.lang.reflect.InvocationTargetException;
 7 import java.lang.reflect.Method;
 8 
 9 /**
10  * 簡單的測試框架
11  *
12  * main()執行,會自動檢測所有帶有Check註解的方法,判斷這些方法是否有異常,記錄異常資訊到檔案中
13  */
14 public class TestCheck {
15     public static void main(String[] args) throws IOException {
16         //1 建立計算機物件
17         Calculator calculator = new Calculator();
18 
19         //2 獲取位元組碼檔案物件
20         Class<? extends Calculator> calculatorClass = calculator.getClass();
21 
22         //3 獲取所有方法
23         Method[] methods = calculatorClass.getDeclaredMethods();
24 
25         int count = 0; //記錄出現異常的次數
26         BufferedWriter bw = new BufferedWriter(new FileWriter("day01\\src\\bug.txt"));
27 
28         for (Method method: methods){
29             //4 判斷方法上是否有Chech註解
30             if (method.isAnnotationPresent(Check.class)){
31                 //method.isAnnotationPresent(Check.class)判斷method是否被Check註解
32                 //5 有Chech註解的方法-->執行檢測
33                 try {
34                     method.invoke(calculator);
35                 } catch (Exception e) {
36                     //6 try捕獲異常,在catch中處理異常資訊
37                     //記錄異常資訊到檔案中
38                     count++;
39                     bw.write(method.getName() + "方法出現異常");
40                     bw.newLine();
41                     bw.write("異常名稱:" + e.getCause().getClass().getSimpleName());
42                     bw.newLine();
43                     bw.write("異常原因:" + e.getCause().getMessage());
44                     bw.newLine();
45                     bw.write("--------------------------------");
46                     bw.newLine();
47                 }
48             }
49         }
50 
51         bw.write("本次一共出現" + count + "次異常");
52         bw.flush();
53         bw.close();
54 
55     }
56 }
add方法出現異常
異常名稱:NullPointerException
異常原因:null
--------------------------------
div方法出現異常
異常名稱:ArithmeticException
異常原因:/ by zero
--------------------------------
本次一共出現2次異常