如何从常量 java 为注释提供值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2065937/
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 supply value to an annotation from a Constant java
提问by Kannan Ekanath
I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time. I have an interface as follows,
我认为这在 Java 中可能是不可能的,因为注释及其参数是在编译时解析的。我有一个界面如下,
public interface FieldValues {
String[] FIELD1 = new String[]{"value1", "value2"};
}
and another class as,
和另一个类,
@SomeAnnotation(locations = {"value1", "value2"})
public class MyClass {
....
}
I mark many classes with the annotation and I would like to know if I can avoid specifying the strings in every annotation I would instead prefer to use
我用注释标记了许多类,我想知道是否可以避免在每个注释中指定字符串,而我更喜欢使用
@SomeAnnotation(locations = FieldValues.FIELD1)
public class MyClass {
....
}
However this gives compilation errors like annotation value should be an array initializer etc. Does someone know how I can use a String constant or String[] constant to supply value to an annotation?
但是,这会导致编译错误,例如注释值应该是数组初始值设定项等。有人知道我如何使用 String 常量或 String[] 常量为注释提供值吗?
采纳答案by irreputable
Compile constants can only be primitives and Strings:
编译常量只能是原语和字符串:
15.28. Constant Expressions
A compile-time constant expressionis an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
- Literals of primitive type and literals of type
String
- Casts to primitive types and casts to type
String
- [...] operators [...]
- Parenthesized expressions whose contained expression is a constant expression.
- Simple names that refer to constant variables.
- Qualified names of the form TypeName. Identifierthat refer to constant variables.
15.28. 常量表达式
编译时常量表达式是表示原始类型值或不会突然完成并且仅使用以下内容组成的字符串的表达式:
- 原始类型字面量和类型字面量
String
- 转换为原始类型并转换为类型
String
- [...] 运营商 [...]
- 包含表达式是常量表达式的括号表达式。
- 引用常量变量的简单名称。
- 形式为TypeName 的限定名称。引用常量变量的标识符。
Actually in java there is no way to protect items in an array. At runtime someone can always do FieldValues.FIELD1[0]="value3"
, therefore the array cannot be really constant if we look deeper.
实际上在java中没有办法保护数组中的项目。在运行时,总有人可以做FieldValues.FIELD1[0]="value3"
,因此如果我们深入研究,数组就不可能是真正的常量。
回答by Kevin
Does someone know how I can use a String constant or String[] constant to supply value to an annotation?
有人知道如何使用 String 常量或 String[] 常量为注释提供值吗?
Unfortunately, you can't do this with arrays. With non-array variables, the value must be final static.
不幸的是,你不能用数组来做到这一点。对于非数组变量,该值必须是最终静态的。
回答by Andrzej Doyle
You can use a constant (i.e. a static, final variable) as the parameter for an annotation. As a quick example, I use something like this fairly often:
您可以使用常量(即静态的最终变量)作为注释的参数。举个简单的例子,我经常使用这样的东西:
import org.junit.Test;
import static org.junit.Assert.*;
public class MyTestClass
{
private static final int TEST_TIMEOUT = 60000; // one minute per test
@Test(timeout=TEST_TIMEOUT)
public void testJDK()
{
assertTrue("Something is very wrong", Boolean.TRUE);
}
}
Note that it's possible to pass the TEST_TIMEOUT
constant straight into the annotation.
请注意,可以将TEST_TIMEOUT
常量直接传递到注释中。
Offhand, I don't recall ever having tried this with an array, so you may be running into some issues with slight differences in how arrays are represented as annotation parameters compared to Java variables? But as for the other part of your question, you could definitely use a constant String without any problems.
顺便说一句,我不记得曾经用数组尝试过这个,所以你可能会遇到一些问题,与 Java 变量相比,数组如何表示为注释参数的方式略有不同?但至于您问题的另一部分,您绝对可以毫无问题地使用常量 String。
EDIT: I've just tried this with a String array, and didn't run into the problem you mentioned - however the compiler didtell me that the "attribute value must be constant" despite the array being defined as public static final String[]
. Perhaps it doesn't like the fact that arrays are mutable? Hmm...
编辑:我刚刚用 String 数组尝试过这个,并没有遇到你提到的问题 - 但是编译器确实告诉我“属性值必须是常量”,尽管数组被定义为public static final String[]
. 也许它不喜欢数组可变的事实?唔...
回答by Steve B.
You're not supplying it with an array in your example. The following compiles fine:
在您的示例中,您没有为它提供数组。以下编译正常:
public @interface SampleAnnotation {
String[] sampleValues();
}
public class Values {
public static final String val0 = "A";
public static final String val1 = "B";
@SampleAnnotation(sampleValues={ val0, val1 })
public void foo() {
}
}
回答by jorgwel
I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time.
我认为这在 Java 中可能是不可能的,因为注释及其参数是在编译时解析的。
With Seam 2 http://seamframework.org/you were able to resolve annotation parameters at runtime, with expression language inside double quotes.
使用 Seam 2 http://seamframework.org/,您可以在运行时解析注释参数,在双引号内使用表达式语言。
In Seam 3 http://seamframework.org/Seam3/Solder, this feature is the module Seam Solder
在 Seam 3 http://seamframework.org/Seam3/Solder 中,此功能是模块 Seam Solder
回答by Kaustubh Thawari
You can use enum and refer that enum in annotation field
您可以使用枚举并在注释字段中引用该枚举