JDK动态代理-报com.sun.proxy.$Proxy0 cannot be cast to ** 的错误

项目场景:

在学习mybatis源码过程中,用到了jdk动态代理,仿照写了一个小demo,发现如下问题

问题描述:

如下代码抛出类型转换异常:
com.sun.proxy. P r o x y 0 c a n n o t b e c a s t t o c o m . d j . d e m o . s p r i n g b o o t d e m o . H a n d l e r M a p p e r Proxy0 cannot be cast to com.dj.demo.springbootdemo.HandlerMapper Proxy0cannotbecasttocom.dj.demo.springbootdemo.HandlerMapperProxyMapper
在这里插入图片描述

public class HandlerMapper {

    interface ProxyMapper {

        @Select(value = "select * from rs_user where id = #{id}")
        List<User> getUsers();
    }

    public static void main(String[] args) {

        InvocationHandler handler = (proxy, method, args1) -> {

            Select annotation = method.getAnnotation(Select.class);
            
            System.out.println(annotation.value());;
            // 该方法要返回的结果类型
            method.getGenericReturnType();
            
            // todo 解析sql
            
            // todo 执行sql
            
            // todo 拼装成要返回的结果类型
            
            return null;
        };

//        ProxyMapper o = (ProxyMapper)Proxy.newProxyInstance(ProxyMapper.class.getClassLoader(), new Class[]{ProxyMapper.class}, handler);
        ProxyMapper o = (ProxyMapper)Proxy.newProxyInstance(ProxyMapper.class.getClassLoader(), ProxyMapper.class.getInterfaces(), handler);
        o.getUsers();

    }
}

原因分析:

问题处在这里: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210219085324441.png)
ProxyMapper o = (ProxyMapper)Proxy.newProxyInstance(
ProxyMapper.class.getClassLoader(), 
ProxyMapper.class.getInterfaces(), 
handler);

ProxyMapper本来已经是最上层的的接口了,如果再用ProxyMapper.class.getInterfaces() 去获取其继承的接口,已经没有更上层的接口了,获取的数组长度为0
在这里插入图片描述


解决方案:

将上述代码改写成如下形式
        ProxyMapper o = (ProxyMapper)Proxy.newProxyInstance(
                ProxyMapper.class.getClassLoader(), 
                new Class[]{ProxyMapper.class}, 
                handler);

或者

        ProxyMapper o = (ProxyMapper)Proxy.newProxyInstance(
                ProxyMapper.class.getClassLoader(),
                ((ProxyMapper) () -> null).getClass().getInterfaces()
                handler);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值