Java 在Groovy注释中传递枚举数组

Java 在Groovy注释中传递枚举数组,java,arrays,groovy,annotations,Java,Arrays,Groovy,Annotations,我不知道为什么,但我不能将数组传递给声明为分离变量的注释 @Target([ ElementType.METHOD, ElementType.TYPE ]) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @interface SomeCustomAnnotation { SomeEnum[] someValue() } _ _ 有什么想法吗?注释需要内联常量。因此,即使变量被声明为静态和最终变量,您试图实现的目标也无

我不知道为什么,但我不能将数组传递给声明为分离变量的注释

@Target([ ElementType.METHOD, ElementType.TYPE ])
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface SomeCustomAnnotation {

  SomeEnum[] someValue()
}
_

_


有什么想法吗?

注释需要内联常量。因此,即使变量被声明为静态和最终变量,您试图实现的目标也无法实现

假设您已经在使用一个单独的类来提供数组,您可以用它的“provider”替换
SomeAnnotation

有很多方法可以做到这一点,但下面是一个使用不同枚举的示例:

enum SomeDataProvider {

    SOME_ENUM_ARRAY_PROVIDER([SOME_ENUM_1, SOME_ENUM_2]);

    private List<SomeAnnotation> array

    SomeDataProvider(def array) {
         this.array = array
    }

    public List<SomeAnnotation> getSomeEnumArray() {
       return array;
    }
}
当然,您需要更改
SomeCustomAnnotation
所需的类型:

@interface SomeCustomAnnotation {

  SomeDataProvider[] someValue()
}
以及通过调用来获取
SomeEnum[]
的处理:

methodg.getDeclaredAnnotation(SomeAnnotation.class)
       .someValue()
       .getSomeEnumArray();

您还可以使用接口替换
SomeDataProvider
,并使
SomeAnnotation
获取
java.lang.Class
对象。

我会尝试将
def
关键字添加到您的
静态最终SomeEnum\u数组中,而不是使用
toArray()
将其强制转换为
SomeEnum[]
。好的,或者只是不使用def并将其定义为
SomeEnum[]
,应该是有效的,sameIntelliJ也给出了这个提示。有什么解决办法吗?@KamilW。Workearounds取决于注释的处理方式。你自己在处理注释吗?@KamilW。添加了一种解决方法:它假设您自己处理注释。非常感谢您的解释,我一开始没有领会。
@SomeAnnotation(someValue = SOME_ENUM_ARRAY_PROVIDER)
def someMethod2(){}
@interface SomeCustomAnnotation {

  SomeDataProvider[] someValue()
}
methodg.getDeclaredAnnotation(SomeAnnotation.class)
       .someValue()
       .getSomeEnumArray();