编程语言
首页 > 编程语言> > java 注解练习2

java 注解练习2

作者:互联网

package Reflects;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

public class Demo {
    public static void main(String[] args) throws IOException {
        Calculator cl= new Calculator();
        Class<? extends Calculator> aClass = cl.getClass();
        System.out.println(aClass);
        Method[] methods = aClass.getMethods();
        int number = 0;
        BufferedWriter bf = new BufferedWriter(new FileWriter("./debug.txt"));
        for (Method m : methods){
            //需要测试的方法
            if (m.isAnnotationPresent(Check.class)){
                try {
                    m.invoke(cl);
                }catch (Exception e){
                    number++;
                    bf.write(m.getName()+"异常了");
                    bf.newLine();
                    bf.write("异常名称:"+e.getCause().getClass().getSimpleName());
                    bf.newLine();
                    bf.write("异常原因:"+e.getCause().getMessage());
                    bf.newLine();
                    bf.write("==============================");
                    bf.newLine();
                }
            }
        }
        bf.write("本此共"+number+"个错误");
        bf.close();

    }
}
package Reflects;

public class Calculator {
    @Check
    public void add(){
        System.out.println("2+1="+(2+1));
    }
    @Check
    public void sub(){
        System.out.println("2-1="+(2-1));
    }
    @Check
    public void take(){
        System.out.println("2*0="+(2*0));
    }
    @Check
    public void div(){
        System.out.println("2/0="+(2/0));
    }
    @Check
    public void test(){
        System.out.println("2/0="+(2/0));
    }
    public void adapt(){
        System.out.println("=====");
    }
}

 

标签:bf,java,void,练习,System,println,注解,public,out
来源: https://www.cnblogs.com/phpwyl/p/16216399.html