注释属性FindBy.xpath的值必须是常量表达式

时间:2014-11-24 11:22:00

标签: java final properties-file

我的问题与发布的内容不同:

  1. value for the annotation attribute must be constant expression
  2. The value for annotation attribute Min.value must be a constant expression
  3. 我已经让它finalstatic仍然在将鼠标悬停在它上面时给出了错误。
    这是我的代码:
    Login.java

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

    Constants.java

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

    我试图解决这个问题: 我将此public static Properties CONFIG = new Properties();更改为此 public static final Properties CONFIG = new Properties();

1 个答案:

答案 0 :(得分:0)

编译时编译器必须可以解析注释元素值。 user_email是静态final是不够的,它的值必须是编译时常量,即字符串文字或常量表达式的连接。表达式CONFIG.getProperty("user_email")在运行时无法解析,因此您无法将其用作注释值。

“常量表达式”的确切定义在Java Language Specification

中给出
  

编译时常量表达式是表示基本类型值的表达式或不突然完成的字符串,仅使用以下内容组成:

     

[...]

     
      
  • TypeName形式的合格名称(第6.5.6.2节)。引用常量变量的标识符(§4.12.4)。
  •   

“常量变量”是“基本类型或类型String的变量,是最终的并使用编译时常量表达式初始化”

(是的,这些定义是循环的,对于字符串,你最终必须以引用的字符串文字或连接一系列其他常量表达式的表达式为最低点。)

相关问题