[英]I have a java annotation which takes a string constant as a argument , how can I Pass a string variable to the same?
For Eg @FindBy(how = How.XPATH, using="//input[@name='Username']")
here I want to replace the value of the string constant "//input[@name='Username']"
with a string variable , even though I declare the variable as final I am unable to pass the variable as a parameter. 例如,对于@FindBy(how = How.XPATH, using="//input[@name='Username']")
,我想替换字符串常量"//input[@name='Username']"
,即使我将变量声明为final,也无法将变量作为参数传递。 I want to write like this 我想这样写
final static String Username_xpath="//input[@name='Username']";
@FindBy(how = How.XPATH, using=Username_xpath)
No,you can not use variable in Annotation,only constant is permitted. 不可以,您不能在Annotation中使用变量,只能使用常量。
the following will work,because here Username_xpath
is a constant: 以下将起作用,因为这里Username_xpath
是一个常量:
final static String Username_xpath="//input[@name='Username']";
@FindBy(how = How.XPATH, using=Username_xpath)
You cannot use a variable as an annotation value because the annotation is already evaluated at compile time. 您不能将变量用作注释值,因为注释已在编译时求值。
Therefore only compile time constants ( static final
) can be used as values in annotations. 因此,仅编译时间常数( static final
)可以用作注释中的值。
Annotations are designed for compile-time or deployment-time processing. 批注设计用于编译时或部署时处理。 At this point you do not have any runtime variables. 此时,您没有任何运行时变量。 Therefore it is not possible to use variables in association with annotations. 因此,不可能将变量与注释关联使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.