【发布时间】:2018-11-27 07:48:33
【问题描述】:
你如何逃脱。 (点)评估 GroovyShell 中的绑定? 看来评估员无法处理。
import groovy.lang.GroovyShell;
import groovy.lang.Binding;
public class BindingSample {
public static void main(String[] args) {
String expression = "sample.name == ben || sample.name == mark || sample.name == trae";
Binding binding = new Binding();
binding.setVariable("sample.name", "ben");
GroovyShell shell = new GroovyShell(binding);
Object result = shell.evaluate(expression);
System.out.println(result);
}
}
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: sample for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Script1.run(Script1.groovy:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:527)
at templates.postprocess.BindingSample.main(BindingSample.java:23)
【问题讨论】: