我是第一次使用 mvel。简单地运行 TemplateRuntime.eval 时,使用@includeNamed效果很好。
但是如果我尝试使用 CompiledTemplate,它就会抛出 NPE。难道我做错了什么?或者这是一个错误?我正在使用 mvel 2.1.4.Final
public class App {
public static void main(String[] args) {
TemplateRegistry registry = new SimpleTemplateRegistry();
registry.addNamedTemplate("world", TemplateCompiler.compileTemplate("world!!!"));
System.out.println(TemplateRuntime.eval("Eval Hello: @includeNamed{'world'}", null, registry));
CompiledTemplate ct = TemplateCompiler.compileTemplate("Compile Hello: @includeNamed{'world'}");
System.out.println(TemplateRuntime.execute(ct, null, registry));
}
}
和堆栈跟踪(注意:Eval 打印正常):
Eval Hello: world!!!
Exception in thread "main" java.lang.NullPointerException
at org.mvel2.integration.impl.StackResetResolverFactory.<init>(StackResetResolverFactory.java:15)
at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
at org.mvel2.MVEL.executeExpression(MVEL.java:930)
at org.mvel2.templates.res.CompiledNamedIncludeNode.eval(CompiledNamedIncludeNode.java:56)
at org.mvel2.templates.res.TextNode.eval(TextNode.java:42)
at org.mvel2.templates.res.TextNode.eval(TextNode.java:42)
at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:285)
at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:247)
at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:255)
at org.mvel2.templates.TemplateRuntime.execute(TemplateRuntime.java:187)
at mveltest.App.main(App.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)