简体   繁体   English

Groovy trait 没有按照文档生成默认的 getter 和 setter

[英]Groovy traits doesn't have generated default getters and setters as documented

According to documentation of Groovy the behavior of getters and setters generating should be similar to common classes.根据 Groovy 的文档,getter 和 setter 生成的行为应该类似于普通类。 But I have an issue ... common Groovy classes do have these getters and setters generated in my code but I need to write explicit setters and getters or my code does not compile.但是我有一个问题……常见的 Groovy 类确实在我的代码中生成了这些 getter 和 setter,但我需要编写显式的 setter 和 getter,否则我的代码无法编译。

@MappedSuperclass
trait Versioned {
    @Version
    @Column(name = "VERSION", nullable = false)
    Long version
}

When another grrovy class implemented the trait the message was:当另一个 grovy 类实现该特性时,消息是:

"error: PersistentCommand is not abstract and does not override abstract method setVersion(Long) in Versioned" “错误:PersistentCommand 不是抽象的,并且不会覆盖 Versioned 中的抽象方法 setVersion(Long)”

My Groovy is older (2.4.15) but the rest is now up to date IntelliJ Idea 2020.2.3, Gradle 6.7.我的 Groovy 较旧(2.4.15),但其余的现在是最新的 IntelliJ Idea 2020.2.3,Gradle 6.7。 By the way I have checked traits documentation of exact Groovy version I use.顺便说一下,我已经检查了我使用的确切 Groovy 版本的特性文档。 Any idea what could be the problem?知道可能是什么问题吗?

Thank you谢谢

this is not an answer.这不是答案。 i just want to show that your code works.我只是想表明您的代码有效。

@Grab(group='javax.persistence', module='javax.persistence-api', version='2.2')
import javax.persistence.MappedSuperclass
import javax.persistence.Version
import javax.persistence.Column

@MappedSuperclass
trait Versioned {
    @Version
    @Column(name = "VERSION", nullable = false)
    Long version
}

@groovy.transform.ToString
class V implements Versioned{
    String name
}

def v=new V(version:123,name:"world")

So after some experimenting I can confirm my suspect.所以经过一些实验,我可以确认我的怀疑。 The problem was groovy version that was too old and probably caused some issues.问题是 groovy 版本太旧了,可能导致了一些问题。 With groovy 2.5.13 it behaves as expected使用 groovy 2.5.13,它的行为符合预期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM