1

My problem is different of what has been posted:

  1. value for the annotation attribute must be constant expression OR
  2. The value for annotation attribute Min.value must be a constant expression

I have already made it final and static still it is giving me that error on hovering over it.
Here is my code:
Login.java

@FindBy(xpath = Constants.user_email)
public static WebElement user_email;

Constants.java

public static final String user_email= CONFIG.getProperty("user_email");

What I tried to fix this: I changed this public static Properties CONFIG = new Properties(); to this public static final Properties CONFIG = new Properties();

Community
  • 1
  • 1
paul
  • 4,333
  • 16
  • 71
  • 144

1 Answers1

3

Annotation element values must be resolvable by the compiler at compile time. It is not enough that user_email is static final, its value must be a compile time constant, i.e. a string literal or a concatenation of constant expressions. The expression CONFIG.getProperty("user_email") is not resolvable until run time, so you can't use it for an annotation value.

The exact definition of "constant expression" is given in the Java Language Specification:

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

[...]

  • Qualified names (§6.5.6.2) of the form TypeName . Identifier that refer to constant variables (§4.12.4).

Where a "constant variable" is a "variable of primitive type or type String, that is final and initialized with a compile-time constant expression"

(yes, these definitions are circular, and in the case of strings you eventually have to bottom out at a quoted string literal or an expression that concatenates a series of other constant expressions).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • SO I can't use `public static final String user_email = CONFIG.getProperty("user_email");` I have to pass value directly like this `public static final String user_email = "//*[@id='user_email']"`. **Or** is there any workaround so that I can pass value using **CONFIG properties file** – paul Nov 24 '14 at 11:43
  • 1
    @paul Not if it has to be an annotation. If you want to use XPaths that are not compile-time constants then you'll have to call the `WebDriver` methods yourself rather than using `PageFactory` annotations. – Ian Roberts Nov 24 '14 at 13:37