【错误记录】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 元对象协议与元编