java - 持有其他注释的注释成员?

标签 java annotations

我想创建一个自定义注释(使用 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 of Class, 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 any public or protected method declared in class Object or in the interface annotation.Annotation.

恐怕我不知道一个好的解决方法,但现在至少您知道为什么会收到错误。

关于java - 持有其他注释的注释成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/679177/

相关文章:

java - Java 中如何以及在何处使用注解?

java - 使用注解创建时,默认情况下如何命名Bean?

java - Hibernate - @OneToMany - 调用方法时发生 com.sun.jdi.InvocationException

java - URLClassLoader 将 Annotation 加载为 com.sun.$Proxy$27

java - 为单个单元格设置两种样式

java - Phonegap 与 servlet 和数据库连接 java 文件

java - 应用程序部署在 tomcat 服务器中失败

java - HTTP动词篡改解决方案

annotations - Spring 注解 AOP 调用两次

Java Spring : JDBC Connection getting closed