本文整理了Java中groovy.lang.Script.evaluate()
方法的一些代码示例,展示了Script.evaluate()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Script.evaluate()
方法的具体详情如下:
包路径:groovy.lang.Script
类名称:Script
方法名:evaluate
[英]A helper method to allow the dynamic evaluation of groovy expressions using this scripts binding as the variable scope
[中]一种助手方法,允许使用此脚本绑定作为变量范围动态计算groovy表达式
代码示例来源:origin: bonitasoft/bonita-engine
@Override
public Object evaluate(final SExpression expression, final Map<String, Object> context, final Map<Integer, Object> resolvedExpressions,
final ContainerState containerState) throws SExpressionEvaluationException {
final String expressionContent = expression.getContent();
final ClassLoader scriptClassLoader = Thread.currentThread().getContextClassLoader();
final String expressionName = expression.getName();
try {
final GroovyShell shell = new GroovyShell(scriptClassLoader);
// can put the name here
final Script script = shell.parse(expressionContent);
final Binding binding = new Binding(context);
script.setBinding(binding);
return script.evaluate(expressionContent);
} catch (final MissingPropertyException e) {
final String property = e.getProperty();
final StringBuilder builder = new StringBuilder("Expression ");
builder.append(expressionName).append(" with content: ").append(expressionContent).append(" depends on ").append(property)
.append(" is neither defined in the script nor in dependencies");
throw new SExpressionEvaluationException(builder.toString(), e, expressionName);
} catch (final GroovyRuntimeException e) {
throw new SExpressionEvaluationException(e, expressionName);
} catch (final Exception e) {
throw new SExpressionEvaluationException("Script throws an exception" + expression, e, expressionName);
}
}
代码示例来源:origin: bonitasoft/bonita-engine
@Override
public Object evaluate(final SExpression expression, final Map<String, Object> context, final Map<Integer, Object> resolvedExpressions,
final ContainerState containerState) throws SExpressionEvaluationException {
final String expressionContent = expression.getContent();
final ClassLoader scriptClassLoader = Thread.currentThread().getContextClassLoader();
final String expressionName = expression.getName();
try {
final GroovyShell shell = new GroovyShell(scriptClassLoader);
// can put the name here
final Script script = shell.parse(expressionContent);
final Binding binding = new Binding(context);
script.setBinding(binding);
return script.evaluate(expressionContent);
} catch (final MissingPropertyException e) {
final String property = e.getProperty();
final StringBuilder builder = new StringBuilder("Expression ");
builder.append(expressionName).append(" with content: ").append(expressionContent).append(" depends on ").append(property)
.append(" is neither defined in the script nor in dependencies");
throw new SExpressionEvaluationException(builder.toString(), e, expressionName);
} catch (final GroovyRuntimeException e) {
throw new SExpressionEvaluationException(e, expressionName);
} catch (final Exception e) {
throw new SExpressionEvaluationException("Script throws an exception" + expression, e, expressionName);
}
}
内容来源于网络,如有侵权,请联系作者删除!