【笔记】使用注释,反射,类加载器以及IO流 实现异常日志的记录

测试类代码

package test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


public class Test {
    public static void main(String[] args) throws Exception {
//        用类加载器,获取一个IO流,这个IO流直接怼到配置文件上
        InputStream srsa = ClassLoader.getSystemResourceAsStream("test/bean.properties");
//          创建一个Properties
        Properties prop = new Properties();
//          用Properties去加载这个IO流,获取这个文件
        prop.load(srsa);
//          关闭IO流
        srsa.close();
//        通过键获取值(通过className名字获取对应的test.UseTest字符串)
        String className1 = prop.getProperty("className");
//          使用反射,通过test.UseTest,获取这个Class文件的对象
        Class<?> className = Class.forName(className1);
//          通过反射,获取类的构造方法
        Constructor<?> declConstructor = className.getDeclaredConstructor();
//            去除权限
        declConstructor.setAccessible(true);
//              使用空参构造,创建类的对象
        Object o = declConstructor.newInstance();
//          通过方式获取类中所有方法,返回一个集合
        Method[] declMethods = className.getDeclaredMethods();
//          使用字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Develop\\IdeaProjects\\xml\\src\\test\\bug.txt"));
//        创建一个计数器,用来收集异常的次数
        int count = 0;
//        遍历类中所有方法的集合
        for (Method declMethod : declMethods) {
//          抓异常
            try {
//                关闭权限
                declMethod.setAccessible(true);
//           调用方法
                declMethod.invoke(o);
            } catch (Exception e) {
//                获取方法名
                String methodName = declMethod.getName();
//                  将方法名写入记录文件当中
                bw.write(methodName+"方法出现了异常");
//                换行
                bw.newLine();
//                获得当前时间
                Date date = new Date();
//                  定义时间格式
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:S");
//                  将时间进行格式化,转成对应的格式
                String format = sdf.format(date);
//                  写入记录文件
                bw.write("异常出现的时间是"+format);
//                换行
                bw.newLine();
//                  获取异常对象(Throwable:异常的超级父类)
                Throwable cause = e.getCause();
//                  通过超级父类获得对应的Class对象,因为在这个对应的Class对象中包含了异常的类名等信息
                String bugName = cause.getClass().getSimpleName();
//                  写入记录文件
                bw.write("异常名字是"+bugName);
//                换行
                bw.newLine();
//                调用方法获取异常的信息
                String bugMessage = cause.getMessage();
//                  写入文件
                bw.write("异常的信息"+bugMessage);
                bw.newLine();
//                  catch里面的代码执行一次,计数器++
                count++;

                bw.write("------------------");
                bw.newLine();
            }
        }
//        写入记录的异常次数
        bw.write("本次测试一共产生了"+count+"次异常");
        bw.flush();
        bw.close();
    }
}

自定义注释代码

package test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * @author df
 * @date 2022/1/9 16:28
 * 自定义的注解
 * 注解的意思就是对我们的程序进行标注和解释
 * 一定要加元注解
 *      1.要说明该注解到什么时候有效
 *          @Retention(value = RetentionPolicy.RUNTIME) : 说明该注解能够保留到运行阶段
 *      2.要说明该注解能够使用在哪些位置
 *          @Target(value = {ElementType.METHOD}) : 说明该注解只能应用于方法上
 */
@Target(value = {ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyTest {
}

被测试的类里面的方法

package test;

public class UseTest {
//    每个方法加入注解
    @MyTest
    public void method1() {
        int[] arr = null;
        System.out.println(arr[0]);
    }
    @MyTest
    public void method2() {
        int a = 20 / 0;
        System.out.println(a);
    }
    @MyTest
    public void method3(){
        int[] arr = {1,2};
        System.out.println(arr[10]);
    }
    @MyTest
    public void method4(){
        String s = "abc";
        int i = Integer.parseInt(s);
        System.out.println(i);
    }
    @MyTest
    public void method5(){
        System.out.println("我执行了,没有异常");
    }
}

配置文件

className=test.UseTest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值