如何创建单个注释接受 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
how to create a single annotation accept multiple values in Java
提问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 String
then 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"})