包含其他注释的注释成员? [英] Annotation member which holds other annotations?
本文介绍了包含其他注释的注释成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个自定义注释(使用 Java),它会接受其他注释作为参数,例如:
I want to create a custom annotation (using Java) which would accept other annotations as parameter, something like:
public @interface ExclusiveOr {
Annotation[] value();
}
但这会导致编译器错误注释成员的类型无效".
But this causes compiler error "invalid type for annotation member".
Object[] 也不起作用.
Object[] also doesn't work.
有没有办法做我想做的事?
Is there a way to do what I want?
推荐答案
我本人在此提出针对给定问题的解决方法:
I myself hereby propose a workaround for the given problem:
好吧,我想让它成为可能:
Well, what I wanted to make possible was something like that:
@Contract({
@ExclusiveOr({
@IsType(IAtomicType.class),
@Or({
@IsType(IListType.class),
@IsType(ISetType.class)
})
})
})
建议的解决方法:
通过以下方式定义一个带有无参数构造函数的类(稍后将由您自己的注解处理器调用):
Define a class with parameter-less constructor (which will be called by your own annotation processor later) in following way:
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{
// ...
}
这篇关于包含其他注释的注释成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文