我想创建一个自定义注释(使用 Java),它将接受其他注释作为参数,例如:
public @interface ExclusiveOr {
Annotation[] value();
}
但这会导致编译器错误“注解成员的类型无效”。
Object[] 也不起作用。
有没有办法做我想做的事?
最佳答案
错误产生是因为你不能使用接口(interface)作为注解值(把它改成Comparable
,你会得到同样的错误)。来自JLS :
It is a compile-time error if the return type of a method declared in an annotation type is any type other than one of the following: one of the primitive types,
String
,Class
and any invocation ofClass
, an enum type, an annotation type, or an array of one of the preceding types. It is also a compile-time error if any method declared in an annotation type has a signature that is override-equivalent to that of anypublic
orprotected
method declared in classObject
or in the interfaceannotation.Annotation
.
恐怕我不知道一个好的解决方法,但现在至少您知道为什么会收到错误。
关于java - 持有其他注释的注释成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/679177/