groovy.lang.Script.evaluate()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中groovy.lang.Script.evaluate()方法的一些代码示例,展示了Script.evaluate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Script.evaluate()方法的具体详情如下:
包路径:groovy.lang.Script
类名称:Script
方法名:evaluate

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);
  }
}

相关文章