Groovy的字符文字用于注释元数据? [英] Groovy character literal for annotation metadata?

查看:229
本文介绍了Groovy的字符文字用于注释元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的的Java 在Groovy定制标注,但没有管理它,因为焦炭的问题。

I wanted to test my Java custom annotation in Groovy but did not manage it because of the char issue.

groovyc的:预期'一'是char类型的内联恒不@MyAnnotation场前pression

Groovyc: Expected 'a' to be an inline constant of type char not a field expression in @MyAnnotation

我知道的字符在Groovy中指定的如

'a' as char

我的的Java 自定义注释

@Target({FIELD})
@Retention(RUNTIME)
@Documented
public @interface MyAnnotation {

    char someChar() default '#';

}

不工作的 Groovy的 code

class Foo {

    @MyAnnotation(someChar = 'a' as char)
    Object hoo

}

如果您解炭为常数,它不工作的。

If you extract the char as a constant, it does not work either.

推荐答案

这是一个棘手的......我发现了什么工作是:

This is a tricky one... what I found to work was:

import java.lang.annotation.Documented
import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import groovy.transform.CompileStatic

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {

    char someChar() default ('#' as char);

}

@CompileStatic
class Foo {

    @MyAnnotation(someChar =( (char)'a'))
    Object hoo

}

其中importart部分是作为注释定义字符(除非这是Java的不Groovy中),在字符投在注解的使用,而不是为char 不过硬铸铁与制作该类沿 @CompileStatic 。这似乎为我工作,但如果你有机会到注释code,你也只是将其更改为一个字符串,因为我认为我有它的工作与简单的字符串。

where the importart parts are the as char in the annotation definition (unless that is Java not Groovy), the char cast in the annotation usage, not as char but a hard cast along with making that class @CompileStatic. This seems to work for me, but if you have access to the annotation code you could also just change it to a String as I think I had it working simpler with a String.

这篇关于Groovy的字符文字用于注释元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆