简体   繁体   中英

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. 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.

the following will work,because here Username_xpath is a constant:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM