【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 ...
随机推荐
- JFlash ARM对stm32程序的读取和烧录-(转载)
本篇文章主要是记录一下JFlash ARM 的相关使用和操作步骤,读取程序说不上破解,这只是在没有任何加密情况下对Flash的读写罢了!在我们装了JLINK驱动后再根目录下找到JFlash ARM , ...
- 发现新大陆 --21lic
21lic网上发单平台 http://project.21ic.com/p/97250
- 在命令行模式下查看Python帮助文档---dir、help、__doc__
在命令行模式下查看Python帮助文档---dir.help.__doc__ 1.dir函数式可以查看对象的属性,使用方法很简单,举str类型为例,在Python命令窗口输入 dir(str) 即 ...
- GO语言复合类型04---映射
package main import "fmt" /* 映射(map)是键值对(key-value)数据的集合 根据键key可以快速检索值value 键值的类型可以是任意的,ke ...
- Mapper注解与MapperScan注解
1.Mapper注解 在接口类上添加@Mapper,在运行时动态代理生成实现类 @Mapper public interface UserDao { // User getUser(); } 如果想要 ...
- Mobileye高级驾驶辅助系统(ADAS)
Mobileye高级驾驶辅助系统(ADAS) Mobileye is the global leader in the development of vision technology for Adv ...
- 视觉SLAM技术应用
视觉SLAM技术应用 SLAM技术背景 SLAM技术全称Simultaneous localization and mapping,中文为"同时定位与地图构建".SLAM可以在未知 ...
- 地理围栏API服务开发
地理围栏API服务开发 要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中. 指定应用权限 如果需要使用地理围栏服务 ...
- TVM中的调度原语
TVM中的调度原语 TVM是一种用于高效内核构造的领域专用语言. 本文将展示如何通过TVM提供的各种原语来调度计算. from __future__ import absolute_import, p ...
- pytest命令行参数
1.-v:可以输出用例更加详细的执行信息,如下图 C:\Users\cale\checkapi\test_cc>pytest test_m1.py -v ==================== ...