【IllegalArgumentException】: object is not an instance of declaring class
java.lang.IllegalArgumentException: object is not an instance of declaring class
日前在调试动态代理的例子中,出现以上报错,关键代码如下:
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("执行" + method.getName());
if (method.getName().equals("writeFile")){
Method method1 = proxy.getClass().getMethod("fetchData");
Method method2 = proxy.getClass().getMethod("makeContent", Object.class);
List<Object> dataList = (List<Object>) method1.invoke(proxy);
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:/Test/test/123.txt"))){
for (Object o : dataList) {
String oneInfo = (String) method2.invoke(obj,o);
bufferedWriter.write(oneInfo);
}
}
return null;
}else {
return method.invoke(obj,args);
}
}
第15行
String oneInfo = (String) method2.invoke(obj,o);
应改为
String oneInfo = (String) method2.invoke(proxy,o);
为何会这样?我们先来了解一下invoke()方法:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {}
官方JDK是这样解释的:
A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler, passing the proxy instance,a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler, passing the proxy instance,a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.
通过一个代理接口对代理实例的方法调用将被分派到实例调用处理程序的调用方法,传递代理实例、标识被调用方法的java.lang.reflect.Method对象和一个包含参数的object类型数组。
- Object proxy:代理实例
- Method method:被调用的方法
- Object[] args:被调用方法的参数
我们可以看到,与22行的区别是代理实例的不同,而代理是实例的不同是因为调用方法的不同,method1方法是通过代理实例proxy获得,所以它必须传入代理实例proxy,而method方法代理的是该类的一个变量obj,更深入的了解后续探讨。
【IllegalArgumentException】: object is not an instance of declaring class的更多相关文章
- java.lang.IllegalArgumentException: object is not an instance of declaring class
今天在使用反射的时候,出现了java.lang.IllegalArgumentException: object is not an instance of declaring class错误-具体是 ...
- 报错:java.lang.IllegalArgumentException: object is not an instance of declaring class
反射的报错信息如下: java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.r ...
- java.lang.IllegalArgumentException: object is not an instance of declaring class新发现
文章目录 背景 报错 解决 引申 背景 因为要将方法缓存起来提高性能 报错 java.lang.IllegalArgumentException: object is not an instance ...
- 由Resin引发的java.lang.IllegalArgumentException: object is not an instance of declaring class(反射中使用)思考
文章目录 背景 原因 解决办法 背景 在java agent中抓取Resin的 某些方法,在invoke的时候出现错误 java.lang.IllegalArgumentException: obje ...
- 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...
- 前端开发者进阶之ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需. 要用作原型的对象. 可以为 nul ...
- 【转】object标签和embed标签
我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...
- 【JavaScript】Object.observe()带来的数据绑定变革
Object.observe()带来的数据绑定变革 引言 一场变革即将到来.一项Javascript中的新特性将会改变你对于数据绑定的所有认识.它也将改变你所使用的MVC库观察模型中发生的修改以及更新 ...
- 【原】Object 异常静态
//所有的类都继承Object类: Object a=; Object b="ddfasfda"; //正常情况下,都会省略掉:Object:但实际上是存在的: class tes ...
随机推荐
- libvirtd 启动成功,但却没有监听
现象: 执行 systemctl start libvirtd在其它机器上进行测试是否监听:virsh -c qemu+tcp://host/system libvirtd启动成功,没有报错,但却没有 ...
- 云计算OpenStack核心组件---nova计算服务(7)
一.nova介绍 Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源.OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是通过 Nova 来实现的. ...
- Linux进阶之补充知识篇
一.Linux系统的主要特点: 开放性:指系统遵循世界标准规范,特别是遵循开放系统互连(OSI)国际标准 多用户:允许多个用户从相同或不同终端上同时使用同一台计算机 多任务:它是指计算机同时执行多个程 ...
- Linux服务之DNS服务篇
一.DNS服务概述 DNS(Domain Name System)域名系统,在TCP/IP 网络中有非常重要的地位,能够提供域名与IP地址的解析服务. DNS 是一个分布式数据库,命名系统采用层次的逻 ...
- IDEA Git 项目实战场景
实战场景一:上班啦,从远程仓库克隆项目到本地仓库(Clone) 打开 IDEA,在 Check out from Version Control 下拉菜单选择 Git,如下: 在弹出窗口的 URL 地 ...
- fcntl详解
功能描述:根据文件描述词来操作文件的特性. #include <unistd.h>#include <fcntl.h> int fcntl(int fd, int cmd); ...
- Centos7 Samba共享服务搭建
Centos7 Samba共享服务搭建 1.安装启动和端口 ---------------------------------------------------------------------- ...
- python 交换变量值为何不需要中间变量
python 元组解包unpacking,同一语句中多重赋值 cpython 认为几种变量的交换是比较常见的,提供了专门的优化指令,像[-5,256]预先放到了整数池中一样,
- 西门子 S7300 以太网模块连接组态王方法
北京华科远创科技有限研发的远创智控ETH-YC模块,以太网通讯模块型号有MPI-ETH-YC01和PPI-ETH-YC01,适用于西门子S7-200/S7-300/S7-400.SMART S7-20 ...
- SpringCloud(八)Sleuth 分布式请求链路跟踪
SpringCloud Sleuth 分布式请求链路跟踪 概述 为什么会出现这个技术?需要解决哪些问题? 在微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后 ...