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的更多相关文章

  1. java.lang.IllegalArgumentException: object is not an instance of declaring class

    今天在使用反射的时候,出现了java.lang.IllegalArgumentException: object is not an instance of declaring class错误-具体是 ...

  2. 报错: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 ...

  3. java.lang.IllegalArgumentException: object is not an instance of declaring class新发现

    文章目录 背景 报错 解决 引申 背景 因为要将方法缓存起来提高性能 报错 java.lang.IllegalArgumentException: object is not an instance ...

  4. 由Resin引发的java.lang.IllegalArgumentException: object is not an instance of declaring class(反射中使用)思考

    文章目录 背景 原因 解决办法 背景 在java agent中抓取Resin的 某些方法,在invoke的时候出现错误 java.lang.IllegalArgumentException: obje ...

  5. 【转】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 ...

  6. 前端开发者进阶之ECMAScript新特性【一】--Object.create

    Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需.  要用作原型的对象. 可以为 nul ...

  7. 【转】object标签和embed标签

    我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...

  8. 【JavaScript】Object.observe()带来的数据绑定变革

    Object.observe()带来的数据绑定变革 引言 一场变革即将到来.一项Javascript中的新特性将会改变你对于数据绑定的所有认识.它也将改变你所使用的MVC库观察模型中发生的修改以及更新 ...

  9. 【原】Object 异常静态

    //所有的类都继承Object类: Object a=; Object b="ddfasfda"; //正常情况下,都会省略掉:Object:但实际上是存在的: class tes ...

随机推荐

  1. HAProxy之三----keepalived配合脚本对HAProxy、ping网关实现高可用检测

    调用脚本参数含义 vrrp_script<SCRIPT_NAME> { #定义一个检测脚本,在global_defs之外配置 script <STRING>|<QUOTE ...

  2. PHP使用mail函数发送邮件

    yum install sendmail #需要使用到sendmail hostname jb51.net #建议修改主机名为域名格式,否则启动sendmail会很慢 systemctl start ...

  3. nohup 命令 2>&1 |tee lmbench.log & 只适用没有需要敲y或x的

    nohup make results 2>&1 |tee lmbench.log & nohup 命令 2>&1 |tee lmbench.log & 只适 ...

  4. 那些天,shell脚本中曾经踩过的坑

    前些天,需要实现一个需求,用脚本轮流kill服务器上的进程,观察内存变化情况,并写日志.脚本逻辑不难,但shell脚本好久不用,看过书里的语法都忘得差不多了,中间踩了不少的坑,特此记录一下,留作后续参 ...

  5. CAD中解决打印图纸模糊而且有的字体深浅不一的方法

    按圈圈中选择打印样式

  6. RAM与FLASH

    以前一直使用STM32但是对 变量 或 函数 的存储域没做任何了解:只知道你需要存储的东西就放在Flash的后面几页就好了:这次接触到STM8发现编译器里面有特别的存储查看器就打算看看到底是怎么存储的 ...

  7. Docker系列——Grafana+Prometheus+Node-exporter服务器告警中心(二)

    在前一篇博文中介绍,服务器监控已经部署成功.如果每天都需要人去盯着服务情况,那也不太现实.既然监控平台已经部署好了,是不是可以自动触发报警呢? 在上一篇Prometheus架构中有讲到,核心组件之一: ...

  8. 使用 “恢复模式” 或 “DFU 模式” 来更新和恢复 iOS 固件

    请访问原文链接:https://sysin.org/article/apple-ios-dfu/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:www.sysin ...

  9. TensorFlow编程结构

    TensorFlow编程结构 TensorFlow 与其他编程语言非常不同. 首先通过将程序分为两个独立的部分,构建任何拟创建神经网络的蓝图,包括计算图的定义及其执行.起初这对于传统程序员来说看起来很 ...

  10. 使用cookies,免密登录禅道(一)

    导言:在做自动化的过程中,很多时候都需要绕过登录验证码来进行测试,可使用cookie 绕过验证码进行登录. 以下以自己搭建的禅道环境登录为例(其他网站也可以同样道理): #coding=gbkimpo ...