第17天注解的补充

通过注解和反射配置文件

  • getAnnotation(MyAnno.class) 获取到MyAnno的实现类,返回一个MyAnno类型的子类。
  • Class clazz = TestPerson.class; 获取那个类的字节码文件,类就是字节码的泛型。
    	@Retention(RetentionPolicy.RUNTIME)
    	@Target(ElementType.TYPE)
    	public @interface MyAnno {
    	  String className();
    	  String methodName();
    	}
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    @MyAnno(className = "test3.Person",methodName = "speak")
    public class TestPerson {
        public static void main(String[] args) throws Exception{
            Class<TestPerson> clazz = TestPerson.class;//获取测试类的字节码文件
            MyAnno anno1 = clazz.getAnnotation(MyAnno.class);//获取MyAnno的实现子类
            String className = anno1.className();
            String methodName = anno1.methodName();
    
            Class clazz1 = Person.class;
            Object o = clazz1.newInstance();
            Method method = clazz1.getDeclaredMethod("speak");
            method.setAccessible(true);
            method.invoke(o);
        }
    }
    

通过注解和反射做一个简单的框架

  • e.getCause() 返回的是Throw类型的子类
  • e.getCause().getClass().getName()获取的是这个异常的名字
  • e.getCause().getMessage() 异常的描述信息
public class Calculator {
    @Check
    public int add(){
        return 1+0;
    }
    @Check
    public int sub(){
        return 1-0;
    }
    @Check
    public int div(){
        return 1/0;
    }
    @Check
    public int mul(){
        return 1*0;
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Check {
}

public class TestCheck {
    public static void main(String[] args) throws Exception{
        int number = 0;
        BufferedWriter bw = new BufferedWriter(new FileWriter("day16/bug.txt"));
        Calculator calculator = new Calculator();
        Class clazz = Calculator.class;
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            if(method.isAnnotationPresent(Check.class)){
                try{
                    method.invoke(calculator);
                }catch (Exception e){
                    number++;
                    bw.write(method.getName()+"方法出错了");
                    bw.newLine();
                    bw.write("异常的名称:"+e.getCause().getClass().getName());
                    bw.newLine();
                    bw.write("异常的原因:"+e.getCause().getMessage());
                    bw.newLine();
                    bw.write("------------------------------------------");
                    bw.newLine();
	                }
	            }
	        }
        bw.write("出错的异常有"+number+"个");
        bw.close();

    	}
	}

反射中泛型的擦除

public class Test {
    public static void main(String[] args) throws Exception{
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        Class clazz = ArrayList.class;
        Method method = clazz.getDeclaredMethod("add",Object.class);
        method.setAccessible(true);
        method.invoke(list,"你好");
        System.out.println(list);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值