0

初めてmvelを使用しています。@includeNamedを使用すると、TemplateRuntime.eval を実行するだけで問題なく動作します。

しかし、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)
4

1 に答える 1

0

まあ、それはバグのようです。variableResolverFactory.someMethod が呼び出されたときに、StackResetResolverFactory のコンストラクターで NPE が発生していることに気付きました。

回避策として、VariableResolverFactory を受け取る execute メソッドを使用します。

Map<String, Object> dummyVariableMap = new HashMap<String, Object>();
VariableResolverFactory dummyResolverFactory = new SimpleVariableResolverFactory(dummyVariableMap);
System.out.println(TemplateRuntime.execute(ct, null, dummyResolverFactory, registry));

奇妙なことに、名前付きテンプレートを使用して CompiledTemplate を実行することは非常に一般的なユースケースだと思いました。

于 2013-05-01T00:01:20.617 に答える