使用 JEXL,您可以在包含变量及其值的给定上下文 (JexlContext) 中评估脚本/表达式。JexlContext 公开了 'has' 和 'get' 方法,它们分别检查是否存在并获取变量的值。在您的情况下,您需要找出您的 JexlContext 是(或应该是);从那里,可以直接迭代您的变量(从您的脚本中提取)并检查它们的值(从上下文中)。
请参阅:
http: //commons.apache.org/proper/commons-jexl/apidocs/org/apache/commons/jexl3/JexlContext.html
http://commons.apache.org/proper/commons-jexl/apidocs/org /apache/commons/jexl3/JexlScript.html
例如(使用来自https://github.com/apache/commons-jexl的 JEXL 3.2 主干):
/**
* Collect the global variables of a script and their values from a context.
* @param script the script
* @param context the context
* @return a map keyed by variable name of their contextual values
*/
Map<String, Object> collectVars(JexlScript script, JexlContext context) {
Set<List<String>> sls = script.getVariables();
Map<String, Object> vars = new TreeMap<String, Object>();
for(List<String> ls : sls) {
// build the 'antish' name by concatenating
StringBuilder strb = new StringBuilder();
for(String s : ls) {
if (strb.length() > 0) {
strb.append('.');
}
strb.append(s);
}
String name = strb.toString();
vars.put(name, context.get(name));
}
return vars;
}
@Test
public void testStckOvrflw() throws Exception {
JexlEngine jexl = new JexlBuilder().safe(false).create();
// a context with some variables
JexlContext context = new MapContext();
context.set("low", 15000);
context.set("high", 50000);
context.set("mid", 35000);
context.set("limit.low", 15042);
context.set("limit.high", 35042);
// an expression with 2 variables
JexlScript expr = jexl.createScript("low >= 12345 && high <= 56789");
// collecting the 2 variables, low and high
Map<String, Object> vars = collectVars(expr, context);
Assert.assertEquals(2, vars.size());
Assert.assertEquals(15000, vars.get("low"));
Assert.assertEquals(50000, vars.get("high"));
expr = jexl.createScript("limit.low >= 12345 && limit.high <= 56789");
vars = collectVars(expr, context);
Assert.assertEquals(2, vars.size());
Assert.assertEquals(15042, vars.get("limit.low"));
Assert.assertEquals(35042, vars.get("limit.high"));
}