自定义寻找bug简单框架 (还没完成)
先写一个自定义注解类:
package mytestr;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)//作用阶段:直到加载至内存
@Target(ElementType.METHOD)//作用于方法
public @interface Check {
public String value();
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Clock;
public class test {
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		int numm = 0;
		BufferedWriter bw = new BufferedWriter(new FileWriter("bug4.txt"));
		deom1 d = new deom1();
//获取deom1的字节码文件
		Class cls = d.getClass();
		// 获取方法
		Method[] methods = cls.getMethods();
		for (Method method : methods) {
			// 判断方法是否有错
			if (method.isAnnotationPresent(Check.class)) {
				try {
					method.invoke(d);
				} catch (Exception e) {
					numm++;
					bw.write(method.getName() + "方法出异常了");
					bw.newLine();
					bw.write("异常名称" + e.getCause().getClass().getSimpleName());
					bw.newLine();
					bw.write("异常原因" + e.getCause().getMessage());
					bw.newLine();
					bw.write("==========");
					bw.newLine();
					bw.flush();
// TODO Auto-generated catch block
}
			}
		}
		bw.write("本次测试一共发生了" + numm + "次bug");
		bw.flush();
		bw.close();
	}
}
创建一个带异常的类
package mytestr;
public class deom1 {
//加上自定义注解
	@Check("a")
 public void show() {
	 System.out.println(1/0);
 }
}

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号