日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/6/17 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【错误记录】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 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。