根据 "compile" 条款验证 Jexl 脚本?
validate a Jexl script under "compile" terms?
看这个例子:
public class JexlStuff {
private static final JexlEngine jexl = new JexlBuilder().cache(512).strict(true).silent(false)
.safe(true).create();
public static void main(String[] args) {
JexlScript script = jexl.createScript("var x ='6'; ");
// populate the context
JexlContext context = new MapContext();
Object result = script.execute(context);
System.out.println(result);
if (result != null)
System.out.println(result.getClass());
}
}
此示例打印“6”(为什么?)。但是没有return
声明。是否是某种配置会使上面的示例抛出错误,因为提供的脚本永远无法“编译”?
对我来说最好的情况,这一行会引发错误:
JexlScript script = jexl.createScript("var x ='6'; ");
有没有办法验证 - 检查给定脚本的语法?
仅供参考:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.2.1</version>
</dependency>
Jexl 是一种脚本 (JSR223) 语言,
var x ='6';
是有效的 script,因此不应抛出无效编译
当在脚本的最后一行设置值时,结果就是输出,例如在 Jexl (github) test
final JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create();
final JexlScript set = sjexl.createScript("foo[x] = y", "foo", "x", "y");
result = set.execute(null, foo, 0, "42");
Assert.assertEquals("42", result);
看这个例子:
public class JexlStuff {
private static final JexlEngine jexl = new JexlBuilder().cache(512).strict(true).silent(false)
.safe(true).create();
public static void main(String[] args) {
JexlScript script = jexl.createScript("var x ='6'; ");
// populate the context
JexlContext context = new MapContext();
Object result = script.execute(context);
System.out.println(result);
if (result != null)
System.out.println(result.getClass());
}
}
此示例打印“6”(为什么?)。但是没有return
声明。是否是某种配置会使上面的示例抛出错误,因为提供的脚本永远无法“编译”?
对我来说最好的情况,这一行会引发错误:
JexlScript script = jexl.createScript("var x ='6'; ");
有没有办法验证 - 检查给定脚本的语法?
仅供参考:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.2.1</version>
</dependency>
Jexl 是一种脚本 (JSR223) 语言,
var x ='6';
是有效的 script,因此不应抛出无效编译
当在脚本的最后一行设置值时,结果就是输出,例如在 Jexl (github) test
final JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); final JexlScript set = sjexl.createScript("foo[x] = y", "foo", "x", "y"); result = set.execute(null, foo, 0, "42"); Assert.assertEquals("42", result);