如何将多个注释合并为一个注释? [英] How to combine multiple annotations to single one?

查看:704
本文介绍了如何将多个注释合并为一个注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个框架中有两个注释.我经常在同一字段上使用这两个注释.因此,我试图创建一个包含两者的组合"注释.

I have two annotations from a framework. Often I use those two annotations both on the same field. Thus I'm trying to create a "combined" annotation that contains that both two.

但我完全不知道是否可能:

But I don't know if it is possible at all:

现有注释(我无法控制)

@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiParam {
    String name() default "";
}

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiModelProperty {
    String name() default "";
}

我要创建的

我的自定义批注:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@ApiParam
@ApiModelProperty
public @interface SwaggerParam {
     String name() default "";
}

结果:注释不适用于注释类型.

问题:有机会吗?

推荐答案

很遗憾,您无法执行此操作,因为无法扩展注释.

Unfortunately you can't do this since it is not possible to extend annotations.

在Java中是否有诸如注释继承之类的东西?

当我第一次回答这个问题时,我最初对这个缺点感到困惑,因为Spring框架方法使用元级别注释(例如@Component作为@ Controller/@ Configuration等的元注释)作为一种解决方法.

When I first answered this I was initially confused by the Spring framework approach to this shortcoming whereby they use meta level annotations (such as @Component as a meta annotation for @Controller/@Configuration etc.) as a sort of workaround.

请参阅: 查看全文

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