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

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

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

Script.run介绍

[英]A helper method to allow scripts to be run taking command line arguments
[中]允许使用命令行参数运行脚本的助手方法

代码示例

代码示例来源:origin: apache/incubator-shardingsphere

private Object evaluate(final String expression) {
  Script script;
  if (SCRIPTS.containsKey(expression)) {
    script = SCRIPTS.get(expression);
  } else {
    script = SHELL.parse(expression);
    SCRIPTS.put(expression, script);
  }
  return script.run();
}

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

/**
 * Run a script identified by name with a given binding.
 *
 * @param scriptName name of the script to run
 * @param binding    the binding to pass to the script
 * @return an object
 * @throws ResourceException if there is a problem accessing the script
 * @throws ScriptException   if there is a problem parsing the script
 */
public Object run(String scriptName, Binding binding) throws ResourceException, ScriptException {
  return createScript(scriptName, binding).run();
}

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

/**
 * Evaluates some script against the current Binding and returns the result
 *
 * @param codeSource
 * @throws CompilationFailedException
 */
public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException {
  Script script = parse(codeSource);
  return script.run();
}

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

public Writer writeTo(Writer out) {
  Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
  PrintWriter pw = new PrintWriter(out);
  scriptObject.setProperty("out", pw);
  scriptObject.run();
  pw.flush();
  return out;
}

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

/**
 * Write the template document with the set binding applied to the writer.
 *
 * @see groovy.lang.Writable#writeTo(java.io.Writer)
 */
public Writer writeTo(Writer writer) {
  Binding binding;
  if (map == null)
    binding = new Binding();
  else
    binding = new Binding(map);
  Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
  PrintWriter pw = new PrintWriter(writer);
  scriptObject.setProperty("out", pw);
  scriptObject.run();
  pw.flush();
  return writer;
}

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

return ((Script) goo).run();

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

public void run() {
    try {
      Script script = (Script) script1Class.newInstance();
      script.run();
      completed [0] = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
};

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

/**
 * Evaluate an expression.
 */
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
  try {
    Class scriptClass = evalScripts.get(script);
    if (scriptClass == null) {
      scriptClass = loader.parseClass(script.toString(), source);
      evalScripts.put(script, scriptClass);
    } else {
      LOG.fine("eval() - Using cached script...");
    }
    //can't cache the script because the context may be different.
    //but don't bother loading parsing the class again
    Script s = InvokerHelper.createScript(scriptClass, context);
    return s.run();
  } catch (Exception e) {
    throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
  }
}

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

/**
 * Evaluates some script against the current Binding and returns the result
 *
 * @param in       the stream reading the script
 * @param fileName is the logical file name of the script (which is used to create the class name of the script)
 */
public Object evaluate(Reader in, String fileName) throws CompilationFailedException {
  Script script = null;
  try {
    script = parse(in, fileName);
    return script.run();
  } finally {
    if (script != null) {
      InvokerHelper.removeClass(script.getClass());
    }
  }
}

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

return ((Script) goo).run();

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

public void run() {
    try {
      Class cls = groovyLoader.loadClass("Script2", true, true);
      Script script = (Script) cls.newInstance();
      script.run();
      completed [1] = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
};

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

protected void assertScript(final String text, final String scriptName) throws Exception {
  log.info("About to execute script");
  log.info(text);
  GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
      return new GroovyCodeSource(text, scriptName, "/groovy/testSupport");
    }
  });
  Class groovyClass = loader.parseClass(gcs);
  Script script = InvokerHelper.createScript(groovyClass, new Binding());
  script.run();
}

代码示例来源:origin: twosigma/beakerx

public Object parseClassFromScript(String script) {
  Class<?> parsedClass = groovyClassLoader.parseClass(script);
  Script instance = null;
  try {
   instance = (Script) parsedClass.newInstance();
   instance.setBinding(scriptBinding);
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  }
  return instance.run();
 }
}

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

protected void assertScriptFile(String fileName) throws Exception {
  log.info("About to execute script: " + fileName);
  Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
  Script script = InvokerHelper.createScript(groovyClass, new Binding());
  script.run();
}

代码示例来源:origin: twosigma/beakerx

private Object runScript(Script script) {
 groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, groovyEvaluator.getBeakerX());
 script.setBinding(groovyEvaluator.getScriptBinding());
 return script.run();
}

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

public void run()
  {
    final long id = Thread.currentThread().getId();

    // run the script numIter times
    for (int i = 0; i < numIter; i++)
    {
      Builder builder = new Builder();

      Binding binding = new Binding();
      binding.setVariable("builder", builder);

      script = InvokerHelper.createScript(scriptClass, binding);

      script.run();
    }

    latch.countDown();
  }
}

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

public Object build(Script script) {
  // this used to be synchronized, but we also used to remove the
  // metaclass.  Since adding the metaclass is now a side effect, we
  // don't need to ensure the meta-class won't be observed and don't
  // need to hide the side effect.
  MetaClass scriptMetaClass = script.getMetaClass();
  script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this));
  script.setBinding(this);
  Object oldScriptName = getProxyBuilder().getVariables().get(SCRIPT_CLASS_NAME);
  try {
    getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, script.getClass().getName());
    return script.run();
  } finally {
    if(oldScriptName != null) {
      getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, oldScriptName);
    } else {
      getProxyBuilder().getVariables().remove(SCRIPT_CLASS_NAME);
    }
  }
}

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

/**
 * When a method is not found in the current script, checks that it's possible to call a method closure from the binding.
 *
 * @throws IOException
 * @throws CompilationFailedException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException, CompilationFailedException, IllegalAccessException, InstantiationException {
  String text = "if (method() == 3) { println 'succeeded' }";
  GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script");
  GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
  Class clazz = loader.parseClass(codeSource);
  Script script = ((Script) clazz.newInstance());
  Binding binding = new Binding();
  binding.setVariable("method", new MethodClosure(new Dummy(), "method"));
  script.setBinding(binding);
  script.run();
}

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

protected void executeScript(Class scriptClass, Permission missingPermission) {
  try {
    Script script = InvokerHelper.createScript(scriptClass, new Binding());
    script.run();
    //InvokerHelper.runScript(scriptClass, null);
  } catch (AccessControlException ace) {
    if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
      return;
    } else {
      fail(ace.toString());
    }
  }
  if (missingPermission != null) {
    fail("Should catch an AccessControlException");
  }
}

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

public void testCreateScriptWithScriptClass() {
    GroovyClassLoader classLoader = new GroovyClassLoader();
    String controlProperty = "text";
    String controlValue = "I am a script";
    String code = controlProperty + " = '" + controlValue + "'";
    GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell");
    Class scriptClass = classLoader.parseClass(codeSource, false);
    Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables));
    assertEquals(bindingVariables, script.getBinding().getVariables());
    script.run();
    assertEquals(controlValue, script.getProperty(controlProperty));
  }
}

相关文章