持有其他注释注释成员? [英] Annotation member which holds other annotations?

查看:140
本文介绍了持有其他注释注释成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建(使用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".

对象[]也不起作用。

有没有办法做我想做什么?

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)
        })
    })
})

建议的解决方法:

Proposed workaround:

在定义下列方式与无参数的构造函数的类(将通过自己的注释处理器后来被称为):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆