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

x33g5p2x  于2022-01-19 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(961)

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

GroovyShell.evaluate介绍

[英]Evaluates some script against the current Binding and returns the result
[中]根据当前绑定计算某些脚本并返回结果

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * A helper method to allow the dynamic evaluation of groovy expressions using this
 * scripts binding as the variable scope
 *
 * @param expression is the Groovy script expression to evaluate
 */
public Object evaluate(String expression) throws CompilationFailedException {
  GroovyShell shell = new GroovyShell(getClass().getClassLoader(), binding);
  return shell.evaluate(expression);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Loads a set of given beans
 * @param resources The resources to load
 * @throws IOException
 */
public void loadBeans(Resource[] resources) throws IOException {
  Closure beans = new Closure(this){
    @Override
    public Object call(Object... args) {
      return beans((Closure)args[0]);
    }
  };
  Binding b = new Binding();
  b.setVariable("beans", beans);
  GroovyShell shell = classLoader != null ? new GroovyShell(classLoader,b) : new GroovyShell(b);
  for (Resource resource : resources) {
    shell.evaluate(new InputStreamReader(resource.getInputStream()));
  }
}

代码示例来源:origin: jenkinsci/jenkins

protected void execute(GroovyCodeSource s) {
  try {
    createShell().evaluate(s);
  } catch (RuntimeException x) {
    LOGGER.log(WARNING, "Failed to run script " + s.getName(), x);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
  GroovyShell groovyShell = new GroovyShell(
      this.classLoader, new Binding(arguments), this.compilerConfiguration);
  try {
    String filename = (script instanceof ResourceScriptSource ?
        ((ResourceScriptSource) script).getResource().getFilename() : null);
    if (filename != null) {
      return groovyShell.evaluate(script.getScriptAsString(), filename);
    }
    else {
      return groovyShell.evaluate(script.getScriptAsString());
    }
  }
  catch (IOException ex) {
    throw new ScriptCompilationException(script, "Cannot access Groovy script", ex);
  }
  catch (GroovyRuntimeException ex) {
    throw new ScriptCompilationException(script, ex);
  }
}

代码示例来源:origin: org.springframework/spring-context

@Override
@Nullable
public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
  GroovyShell groovyShell = new GroovyShell(
      this.classLoader, new Binding(arguments), this.compilerConfiguration);
  try {
    String filename = (script instanceof ResourceScriptSource ?
        ((ResourceScriptSource) script).getResource().getFilename() : null);
    if (filename != null) {
      return groovyShell.evaluate(script.getScriptAsString(), filename);
    }
    else {
      return groovyShell.evaluate(script.getScriptAsString());
    }
  }
  catch (IOException ex) {
    throw new ScriptCompilationException(script, "Cannot access Groovy script", ex);
  }
  catch (GroovyRuntimeException ex) {
    throw new ScriptCompilationException(script, ex);
  }
}

代码示例来源:origin: jenkinsci/jenkins

public String call() throws RuntimeException {
    // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
    if (cl==null)       cl = Thread.currentThread().getContextClassLoader();
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
        "jenkins",
        "jenkins.model",
        "hudson",
        "hudson.model"));
    GroovyShell shell = new GroovyShell(cl,new Binding(),cc);
    StringWriter out = new StringWriter();
    PrintWriter pw = new PrintWriter(out);
    shell.setVariable("out", pw);
    try {
      Object output = shell.evaluate(script);
      if(output!=null)
      pw.println("Result: "+output);
    } catch (Throwable t) {
      Functions.printStackTrace(t, pw);
    }
    return out.toString();
  }
}

代码示例来源:origin: groovy/groovy-core

public void testBug() throws Exception {
  String code = "for (i in 1..10) \n{\n  println(i)\n}";
  GroovyShell shell = new GroovyShell();
  shell.evaluate(code);
}

代码示例来源:origin: groovy/groovy-core

public void testMe () {
    new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", Byte.valueOf((byte)1));");
    new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", (byte)1);");
  }
}

代码示例来源:origin: groovy/groovy-core

protected Object evaluate(String text) {
  String name = "Script" + counter++;
  try {
    return getShell().evaluate(text, name);
  }
  catch (Exception e) {
    handleException(text, e);
    return null;
  }
}

代码示例来源:origin: groovy/groovy-core

/**
 * Execute a script.
 */
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
  try {
    // use evaluate to pass in the BSF variables
    source = convertToValidJavaClassname(source);
    getEvalShell().evaluate(script.toString(), source);
  } catch (Exception e) {
    throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
  }
}

代码示例来源:origin: groovy/groovy-core

/**
 * Evaluate an expression.
 */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
  try {
    source = convertToValidJavaClassname(source);
    return getEvalShell().evaluate(script.toString(), source);
  } catch (Exception e) {
    throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
  }
}

代码示例来源:origin: groovy/groovy-core

public void testScriptNameMangling() {
  String script = "this.getClass().getName()";
  GroovyShell shell = new GroovyShell();
  String name = (String) shell.evaluate(script,"a!b");
  assertEquals("a_b", name);
}

代码示例来源:origin: groovy/groovy-core

public void testClassLoader() {
  Binding context = new Binding();
  CompilerConfiguration config = new CompilerConfiguration();
  config.setScriptBaseClass(DerivedScript.class.getName());
  GroovyShell shell = new GroovyShell(context, config);
  String script = "evaluate '''\n"+
          "class XXXX{}\n"+
          "assert evaluate('XXXX') == XXXX\n"+
          "'''";
  shell.evaluate(script);
 }

代码示例来源:origin: groovy/groovy-core

public void testExecuteScript() {
  GroovyShell shell = new GroovyShell();
  try {
    Object result = shell.evaluate(script1, "Test.groovy");
    assertEquals(new Integer(1), result);
  }
  catch (Exception e) {
    fail(e.toString());
  }
}

代码示例来源:origin: groovy/groovy-core

public void testWithGCSWithURL() throws Exception {
  String scriptFileName = "src/test/groovy/bugs/GROOVY3934Helper.groovy";
  File helperScript = new File(scriptFileName);
  if(!helperScript.exists()) {
    fail("File " + scriptFileName + " does not exist");
  } else {
    URL url = helperScript.toURI().toURL();
    GroovyCodeSource gcs = new GroovyCodeSource(url);
    GroovyShell shell = new GroovyShell();
    Object result = shell.evaluate(gcs);
    assertEquals("GROOVY3934Helper script called", result);
  }
}

代码示例来源:origin: groovy/groovy-core

/**
 * Test for GROOVY-6615
 * @throws Exception
 */
public void testScriptWithCustomBodyMethod() throws Exception {
  Binding context = new Binding();
  CompilerConfiguration config = new CompilerConfiguration();
  config.setScriptBaseClass(BaseScriptCustomBodyMethod.class.getName());
  GroovyShell shell = new GroovyShell(context, config);
  Object result = shell.evaluate("'I like ' + cheese");
  assertEquals("I like Cheddar", result);
}

代码示例来源:origin: spring-projects/spring-framework

try {
  GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
  shell.evaluate(encodedResource.getReader(), "beans");

代码示例来源:origin: groovy/groovy-core

public void testExecuteScriptWithContext() {
  Binding context = new Binding();
  context.setVariable("test", new PropertyHolder());
  GroovyShell shell = new GroovyShell(context);
  try {
    Object result = shell.evaluate(script2, "Test.groovy");
    assertEquals(new Integer(2), result);
  }
  catch (Exception e) {
    fail(e.toString());
  }
}

代码示例来源:origin: groovy/groovy-core

public void testScriptWithDerivedBaseClass() throws Exception {
  Binding context = new Binding();
  CompilerConfiguration config = new CompilerConfiguration();
  config.setScriptBaseClass(DerivedScript.class.getName());
  GroovyShell shell = new GroovyShell(context, config);
  Object result = shell.evaluate("x = 'abc'; doSomething(cheese)");
  assertEquals("I like Cheddar", result);
  assertEquals("abc", context.getVariable("x"));
}

代码示例来源:origin: groovy/groovy-core

public void testClassPropsGroovy() {
  Object testObject = new GroovyShell().evaluate("class Test {def meth1(a,b){}}\nreturn new Test()");
  Inspector insp = new Inspector(testObject);
  String[] classProps = insp.getClassProps();
  assertEquals("package n/a", classProps[Inspector.CLASS_PACKAGE_IDX]);
  assertEquals("public class Test", classProps[Inspector.CLASS_CLASS_IDX]);
  assertEquals("implements GroovyObject ", classProps[Inspector.CLASS_INTERFACE_IDX]);
  assertEquals("extends Object", classProps[Inspector.CLASS_SUPERCLASS_IDX]);
  assertEquals("is Primitive: false, is Array: false, is Groovy: true", classProps[Inspector.CLASS_OTHER_IDX]);
}

相关文章