简体   繁体   English

我有一个Java注释,它以字符串常量作为参数,如何将字符串变量传递给它?

[英]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.

相关问题 如何使用Java Native Interface将字节数组传递给C函数,该函数将char *作为参数? - How can I use Java Native Interface to pass a byte array into a C function which takes a char* as an argument? 如何使用字符串作为获取枚举的方法的参数? - How can I use a string as argument for a method that takes an enum? 如何将Java函数传递给String []参数? - How do I pass a Java function a String[] argument? 我可以在 @Value 注释中传递变量来读取属性文件,其键存储在 String 变量中吗? - Can I pass variable in @Value annotation to read a property file, whose key is stored in a String variable? 我有一个字符串变量和另一个分配给同一个变量的字符串。 那怎么老字符串? - I have a string variable and another string assigned to same variable. Then how to get old string? 我无法解析为变量。我正在编写一个将字符串作为参数反转的 Java 程序 - i cannot be resolved to a variable.i am writing a java program which reverses a string as argument 我如何声明一个大字符串( <Html> 代码)中避免“常量字符串过长”的错误? - How can I declare a Large String(<Html> codes) in Java to avoid “Constant string too long” error? 我可以将哪种类型的参数传递给JAVA 8中的ToIntFunction功能接口 - which type of argument I can pass into ToIntFunction Functional Interface in JAVA 8 如何在Java集合中转换为json响应的字符串? - how can i convert this string which is json response in java collection? 如何为包中的所有类共用一个常量变量? - How can I have a constant variable common to all classes in a package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM