【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )
生活随笔
收集整理的这篇文章主要介绍了
【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- 一、报错信息
- 二、解决方案
一、报错信息
使用 Groovy 函数拦截功能 , 定义 Groovy 类 , 实现 GroovyInterceptable 接口 , 并重写 invokeMethod 方法 , 在该方法中使用
println "invokeMethod"代码 , 打印日志 ;
完整代码如下 :
class Student implements GroovyInterceptable{def name;def hello() {println "Hello ${name}"}@OverrideObject invokeMethod(String name, Object args) {println "invokeMethod"//System.out.println "invokeMethod"} }def student = new Student(name: "Tom")// 直接调用 hello 方法 student.hello()报错信息 :
Caught: java.lang.StackOverflowError java.lang.StackOverflowErrorat Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)at Student.invokeMethod(Groovy.groovy:10)二、解决方案
调用 实现了 GroovyInterceptable 接口的 Student 类的 hello 方法 , 会调用 invokeMethod 方法 ,
在 invokeMethod 方法中 , 又调用了 println 方法 ,
@OverrideObject invokeMethod(String name, Object args) {println "invokeMethod"//System.out.println "invokeMethod"}println 方法是 Groovy 注入到 Object 对象中的 , 在 Student 对象中 , 调用 println 也会回调 invokeMethod 方法 , 而在 invokeMethod 方法中又调用了 println 方法 , 这样循环调用 , 最终导致栈溢出 ;
在 invokeMethod 中 , 不调用 println 方法 , 调用 System.out.println 进行日志打印 , 这样就可以避免栈溢出 ;
class Student implements GroovyInterceptable{def name;def hello() {println "Hello ${name}"}@OverrideObject invokeMethod(String name, Object args) {System.out.println "invokeMethod"} }def student = new Student(name: "Tom")// 直接调用 hello 方法 student.hello()总结
以上是生活随笔为你收集整理的【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【报错信息】Google Play 上架
- 下一篇: 【Groovy】MOP 元对象协议与元编