【问题标题】:Groovy - Stackoverflow whilst calling super.method()Groovy - 调用 super.method() 时堆栈溢出
【发布时间】:2014-07-09 12:10:56
【问题描述】:

我在更改返回类型(协变)的同时覆盖 Groovy 中的方法时遇到了问题。

我的测试代码是:

class Grandparent {
    public Grandparent doStuff( String s ){
        println "GP $s"
        this
    }
}

class Parent extends Grandparent{
    public Parent doStuff( String s ){
        println "P $s "
        this
    }
}

class Child extends Parent{
    public Child doStuff( String s ){
        println "C $s "
        super.doStuff(s)
        this
    }
}

Child c = new Child()
c.doStuff("Yo")

在上面的脚本中,代码堆栈溢出,只是重复调用 Child 类的 doStuff() 方法:

Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
    at Child.doStuff(GroovyInheritanceTest.groovy:18)
    at Child.doStuff(GroovyInheritanceTest.groovy)
    at Parent.doStuff(GroovyInheritanceTest.groovy)
    at Child.super$3$doStuff(GroovyInheritanceTest.groovy)
    at Child.doStuff(GroovyInheritanceTest.groovy:18)
    at Child.doStuff(GroovyInheritanceTest.groovy)
    at Parent.doStuff(GroovyInheritanceTest.groovy)

如果

,则不会发生此问题

1)我们在层次结构中只有两个类(例如,切出 Child) 2) doStuff 的所有实例都返回相同的类型,例如全部返回 GradParent

我使用的是 Groovy 2.1.5 - 它似乎在 Groovy 2.2 中工作。

有谁知道是否有解决方法可以在 Groovy 2.1.5 中进行这项工作,或者有作为其中一部分修复的错误编号的详细信息?

【问题讨论】:

  • 它也适用于 groovy 2.3.3。
  • 前段时间我被那个咬了,无法升级groovy。幸运的是,我可以更改子类方法名称。

标签: groovy


【解决方案1】:

有谁知道在 Groovy 中是否有解决方法可以使这项工作 2.1.5 或有作为其中一部分修复的错误编号的详细信息?

请参阅 https://issues.apache.org/jira/browse/GROOVY-5810https://issues.apache.org/jira/browse/GROOVY-6818

【讨论】:

  • 谢谢 - 这个特定的代码示例实际上在 2.2 中有效(第一个错误与静态方法有关,第二个错误看起来非常相似,但在 2.2 中被报告为损坏)。例外的答案仍然有用。
猜你喜欢
  • 2010-10-20
  • 2019-05-18
  • 2014-05-25
  • 2011-08-05
  • 2019-08-07
  • 2013-10-30
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多