Groovy:意外的标记“:”

Groovy: Unexpected token ":"

我有这个自动生成的代码行:

EPRole validator: { EPRole r, EPUserEPRole ur ->
            if (ur.EPUser == null) return
            boolean existing = false
            EPUserEPRole.withNewSession {
                existing = EPUserEPRole.exists(ur.EPUser.id, r.id)
            }
            if (existing) {
                return 'userRole.exists'
            }
        }

当我尝试编译代码时,我得到 82: unexpected token: validator @ line 82, column 10.

我是 groovy 的新人,非常感谢您的帮助。

您应该将具有正确类型和名称的属性添加到 class。首字母大写用于 classes(或一般类型)。所以你的 EPUserEPRole 中应该有这样一个 属性:

EPRole epRole

然后为 epRole 添加验证器。注意大小写。

上面的代码会使 groovy 解析器混淆定义 属性 validator 类型 EPRole 后跟 :,因此错误(或否则它会根据上下文尝试使用映射调用方法 EPRole。