此代码用于在 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 中的枚举类型了。是否有任何原因这不再起作用或者它是一个错误