如何创建单个注释接受 Java 中的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12637649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:39:40  来源:igfitidea点击:

how to create a single annotation accept multiple values in Java

javaannotationsjirasonarqube

提问by Junchen Liu

I have a Annotation called

我有一个名为的注释

@Retention( RetentionPolicy.SOURCE )
@Target( ElementType.METHOD )
public @interface JIRA
{
    /**
     * The 'Key' (Bug number / JIRA reference) attribute of the JIRA issue.
     */
    String key();
}

which allows to add annotation like this

它允许添加这样的注释

@JIRA( key = "JIRA1" )

is there any way to allow this to happen

有没有办法让这种情况发生

@JIRA( key = "JIRA1", "JIRA2", .....  )

the reason is, we currently annotate the test against a Jira task or bug fix, but sometimes, then the value will get parsed by sonar. problem is a single test covers more then 1 bug.

原因是,我们目前针对 Jira 任务或错误修复对测试进行注释,但有时,该值会被声纳解析。问题是单个测试涵盖了 1 个以上的错误。

回答by Amit Deshpande

Change your key()function to return String[]rather than Stringthen you can pass various values using String[]

更改您的 key()函数以返回String[]而不是String然后您可以使用传递各种值String[]

public @interface JIRA {
/**
 * The 'Key' (Bug number / JIRA reference) attribute of the JIRA issue.
 */
String[] key();
}

Use it like below

像下面一样使用它

@JIRA(key = {"JIRA1", "JIRA2"})