String expression = "f >= 10"
JexlEngine jexlEngine = new JexlBuilder().create();
// 获取表达式中的变量名称
Set<List<String>> variables = jexlEngine.createScript(expression).getVariables();
JexlContext jexlContext = new MapContext();
try {
for (List<String> variable : variables) {
String fieldName = variable.get(0) ;
Object fieldValue = null != fieldName ? 反射获取值 : null;
if (null != fieldValue) {
jexlContext.set(fieldName, fieldValue);
}
}
Object result = jexlEngine.createExpression(expression).evaluate(jexlContext);
if (result instanceof Boolean) {
return (Boolean) result;
} else {
throw new IllegalArgumentException("Expression must evaluate to a boolean value");
}
} catch (Exception e) {
throw new Exception(e);
}
Jexl解析表达式
最新推荐文章于 2024-05-24 17:41:11 发布