0

此代码用于在 jexl2 下工作

public enum Gender { MALE, FEMALE};

public static void main( String[] args ) throws ClassNotFoundException {
    JexlEngine jexl = new JexlEngine();
    Record r = new Record();
    r.setHeader(new RecordHeader());
    JexlContext jc = new MapContext();
    jc.set("r", r );
    jc.set("com.sytrue.objects.Gender", Gender.class );
    Expression e = jexl.createExpression( "r.header.gender=com.sytrue.objects.Gender.FEMALE" );
    System.out.println( e.evaluate(jc) );
}

但在 jexl3 下它会引发异常:

public static void main( String[] args ) throws ClassNotFoundException {
    JexlEngine jexl = new JexlBuilder().create();
    Record r = new Record();
    r.setHeader(new RecordHeader());
    JexlContext jc = new MapContext();
    jc.set("r", r );
    jc.set("com.sytrue.objects.Gender", Gender.class );
    JexlExpression e = jexl.createExpression( "r.header.gender=com.sytrue.objects.Gender.FEMALE" );
    System.out.println( e.evaluate(jc) );
}

例外是:

Exception in thread "main" org.apache.commons.jexl3.JexlException$Property: com.sytrue.objects.record.app.JexlTestApp.main@1:42 unsolvable property 'FEMALE'

我使用 valueOf 方法使它工作:

JexlExpression e = jexl.createExpression( "r.header.gender=com.sytrue.objects.Gender.valueOf('FEMALE')" );

看来我不再可以访问 jexl3 中的枚举类型了。是否有任何原因这不再起作用或者它是一个错误

4

1 回答 1

0

原来这是一个错误:我在 JIRA 上发布了这个问题: https ://issues.apache.org/jira/browse/JEXL-191 它将在 3.1 版中修复

于 2016-03-21T06:25:40.443 回答