2

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)      
vjy
  • 1,184
  • 1
  • 10
  • 24
  • You can find further comments on the same type of question here: [link](http://stackoverflow.com/questions/2065937/how-to-supply-value-to-an-annotation-from-a-constant-java) – Lasse Samson Sep 19 '14 at 06:17

3 Answers3

6

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)
Rib47
  • 2,336
  • 2
  • 14
  • 15
BlackJoker
  • 3,099
  • 2
  • 20
  • 27
1

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.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
  • Thanks a lot Plonus. its clear now. any work around to overcome this limitation as in my case all the variables are stored in a xml file and I want to pass them to this annotation ? – Sudhanva Patil Sep 19 '14 at 07:32
0

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.

vargapeti
  • 301
  • 2
  • 7