Java 保存其他批注的批注成员?
我想创建一个自定义注释(使用Java),它将接受其他注释作为参数,例如:Java 保存其他批注的批注成员?,java,annotations,Java,Annotations,我想创建一个自定义注释(使用Java),它将接受其他注释作为参数,例如: public @interface ExclusiveOr { Annotation[] value(); } 但这会导致编译器错误“注释成员的类型无效” 对象[]也不起作用 有什么方法可以满足我的要求吗?您可以: Class<? extends Annotation>[] value(); Class产生错误是因为您不能将接口用作注释值(将其更改为Comparable,您将得到相同的错误)。从:
public @interface ExclusiveOr {
Annotation[] value();
}
但这会导致编译器错误“注释成员的类型无效”
对象[]也不起作用
有什么方法可以满足我的要求吗?您可以:
Class<? extends Annotation>[] value();
Class产生错误是因为您不能将接口用作注释值(将其更改为Comparable
,您将得到相同的错误)。从:
如果在注释类型中声明的方法的返回类型不是以下类型之一,则为编译时错误:基元类型之一、String
、Class
、调用Class
、枚举类型、注释类型或前面类型之一的数组。如果在注释类型中声明的任何方法的签名重写等效于类对象
或接口注释
中声明的任何公共
或受保护
方法的签名重写,则这也是编译时错误
恐怕我不知道有什么好的解决方法,但现在至少你知道了为什么会出现错误。我本人在此为给定的问题提出一个解决方法:
我想做的是这样的:
@Contract({
@ExclusiveOr({
@IsType(IAtomicType.class),
@Or({
@IsType(IListType.class),
@IsType(ISetType.class)
})
})
})
拟议的解决办法:
使用无参数构造函数(稍后将由您自己的注释处理器调用)定义一个类,方法如下:
final class MyContract extends Contract{
// parameter-less ctor will be handeled by annotation processor
public MyContract(){
super(
new ExclusiveOr(
new IsType(IAtomicType.class),
new Or(
new IsType(IListType.class),
new IsType(ISetType.class)
)
)
);
}
}
用法:
@Contract(MyContract.class)
class MyClass{
// ...
}
根据您希望指定其他注释的原因,有多种解决方案:
单个注释类型的实例数组
可能不是您在问题中的意思,但是如果您想指定单个注释类型的多个实例,那么当然可以:
public @interface Test {
SomeAnnotation[] value();
}
注释类型数组,而不是实例数组
如果不需要在单个注释上指定任何参数,则可以使用它们的类对象而不是实例
public @interface Test {
Class<? extends Annotation>[] value();
}
给每个成员一个默认值,就可以只为您需要的类型指定数组。我刚刚遇到了这个问题,但是(受@ivan_ivanovich_ivanoff的启发),我发现了一种方法,可以将任何注释组合指定为注释成员:使用原型/模板类
在本例中,我定义了一个WhereOr(即模型注释的“where子句”),需要包含任意Spring元注释(如@Qualifier元注释)
其中的一个小缺陷是强制取消引用,它将where子句的实现与其描述的具体类型分开
@Target({})
@Retention(RetentionPolicy.RUNTIME)
public @interface WhereOr {
Class<?>[] value() default {};
}
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonModel {
Class<?> value();
WhereOr where() default @WhereOr;
}
public class Prototypes {
@Qualifier("myContext")
@PreAuthorize("hasRole('ROLE_ADMINISTRATOR')")
public static class ExampleAnd {
}
}
@JsonModel(
value = MusicLibrary.class,
where = @WhereOr(Prototypes.ExampleAnd.class)
)
public interface JsonMusicLibrary {
@JsonIgnore
int getMajorVersion();
// ...
}
@Target({})
@保留(RetentionPolicy.RUNTIME)
公共@接口{
类[]值()默认值{};
}
@目标({ElementType.TYPE,ElementType.ANNOTATION\u TYPE})
@保留(RetentionPolicy.RUNTIME)
public@interface JsonModel{
类值();
WhereOr where()默认为@WhereOr;
}
公共类原型{
@限定词(“myContext”)
@预授权(“hasRole('ROLE\u ADMINISTRATOR'))
公共静态类示例和{
}
}
@JsonModel(
值=MusicLibrary.class,
where=@WhereOr(prototype.ExampleAnd.class)
)
公共接口JsonMusicLibrary{
@杰索尼奥雷
int getMajorVersion();
// ...
}
我将以编程方式从“where子句”注释中提取可能的有效配置。在本例中,我还使用Prototype类作为逻辑AND分组,使用类数组作为逻辑OR。抱歉,它没有。我需要注释的实例,而不是类型。因为注释不接受任何类型的参数。Java只允许指定一种特定类型注释的数组(不是我需要的任何类型)。
@Target({})
@Retention(RetentionPolicy.RUNTIME)
public @interface WhereOr {
Class<?>[] value() default {};
}
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonModel {
Class<?> value();
WhereOr where() default @WhereOr;
}
public class Prototypes {
@Qualifier("myContext")
@PreAuthorize("hasRole('ROLE_ADMINISTRATOR')")
public static class ExampleAnd {
}
}
@JsonModel(
value = MusicLibrary.class,
where = @WhereOr(Prototypes.ExampleAnd.class)
)
public interface JsonMusicLibrary {
@JsonIgnore
int getMajorVersion();
// ...
}