简体   繁体   English

为什么以下注释在Java中合法但在groovy中不合法?

[英]Why is the following annotation legal in Java but not in groovy?

I wanted to add encryption to my application using the Jasypt library. 我想使用Jasypt库为我的应用程序添加加密。 Their integration page says to add a @TypeDef annotation: 他们的集成页面说要添加一个@TypeDef注释:

" Define the encryption type with a @TypeDef annotation, which could be either inside the persisted entity class or inside a @TypeDefs declaration in a separate package-info.java file ": 使用@TypeDef注释定义加密类型,该注释可以位于持久化实体类内部,也可以位于单独的package-info.java文件中的@TypeDefs声明内 ”:

@TypeDef(
    name="encryptedString", 
    typeClass=EncryptedStringType.class, 
    parameters= {
        @Parameter(name="encryptorRegisteredName", value="myHibernateStringEncryptor")
    }
)

However, I noticed that when I tried this on a groovy file, I received a syntax error. 但是,我注意到当我在groovy文件上尝试这个时,我收到了语法错误。

" Groovy:unexpected token: } @ line 12, column 3. " Groovy:意外的令牌:} @第12行,第3列。

When I copy and pasted the exact code into a java file it works fine. 当我将确切的代码复制并粘贴到java文件中时,它可以正常工作。 If I remove the parameters argument it works, I'm thinking that the parameters { } argument is being interpreted as a closure by groovy. 如果我删除它有效的参数参数,我认为参数{}参数被groovy解释为闭包。

EDIT: I ended up moving the annotation to package-info.java but I still want to know why this doesn't work in groovy. 编辑:我最终将注释移动到package-info.java但我仍然想知道为什么这在groovy中不起作用。

The problem probably resides in the parameters block: 问题可能在于参数块:

parameters= {
    @Parameter(name="encryptorRegisteredName", value="myHibernateStringEncryptor")
}

Whereas curly-braces can be used in java to specify a static-initialization block for arrays, in groovy the curly-brace is the grammar token for a closure, which isn't what you want here. 虽然可以在java中使用花括号来为数组指定静态初始化块,但在groovy中,花括号是闭包的语法标记,这不是你想要的。 I imagine the following might work: 我想以下可能会奏效:

parameters= [
    @Parameter(name="encryptorRegisteredName", value="myHibernateStringEncryptor")
]

Note the hard-braces, which is groovy's token for anonymously-created lists/maps. 注意硬括号,这是匿名创建的列表/地图的groovy标记。

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

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