前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )

【错误记录】Groovy 函数拦截调用 invokeMethod 导致栈溢出 ( java.lang.StackOverflowError )

作者头像
韩曙亮
发布2023-03-30 10:38:00
4940
发布2023-03-30 10:38:00
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、报错信息


使用 Groovy 函数拦截功能 , 定义 Groovy 类 , 实现 GroovyInterceptable 接口 , 并重写 invokeMethod 方法 , 在该方法中使用

代码语言:javascript
复制
println "invokeMethod"

代码 , 打印日志 ;

完整代码如下 :

代码语言:javascript
复制
class Student implements GroovyInterceptable{
    def name;

    def hello() {
        println "Hello ${name}"
    }

    @Override
    Object invokeMethod(String name, Object args) {
        println "invokeMethod"
        //System.out.println "invokeMethod"
    }
}

def student = new Student(name: "Tom")

// 直接调用 hello 方法
student.hello()

报错信息 :

代码语言:javascript
复制
Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
	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)
	at Student.invokeMethod(Groovy.groovy:10)
在这里插入图片描述
在这里插入图片描述

二、解决方案


调用 实现了 GroovyInterceptable 接口的 Student 类的 hello 方法 , 会调用 invokeMethod 方法 ,

在 invokeMethod 方法中 , 又调用了 println 方法 ,

代码语言:javascript
复制
    @Override
    Object invokeMethod(String name, Object args) {
        println "invokeMethod"
        //System.out.println "invokeMethod"
    }

println 方法是 Groovy 注入到 Object 对象中的 , 在 Student 对象中 , 调用 println 也会回调 invokeMethod 方法 , 而在 invokeMethod 方法中又调用了 println 方法 , 这样循环调用 , 最终导致栈溢出 ;

在 invokeMethod 中 , 不调用 println 方法 , 调用 System.out.println 进行日志打印 , 这样就可以避免栈溢出 ;

代码语言:javascript
复制
class Student implements GroovyInterceptable{
    def name;

    def hello() {
        println "Hello ${name}"
    }

    @Override
    Object invokeMethod(String name, Object args) {
        System.out.println "invokeMethod"
    }
}

def student = new Student(name: "Tom")

// 直接调用 hello 方法
student.hello()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、报错信息
  • 二、解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档