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

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

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

Script.setBinding介绍

暂无

代码示例

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

public static Script newScript(Class<?> scriptClass, Binding context) throws InstantiationException, IllegalAccessException, InvocationTargetException {
  Script script;
  try {
    Constructor constructor = scriptClass.getConstructor(Binding.class);
    script = (Script) constructor.newInstance(context);
  } catch (NoSuchMethodException e) {
    // Fallback for non-standard "Script" classes.
    script = (Script) scriptClass.newInstance();
    script.setBinding(context);
  }
  return script;
}

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

public void setProperty(String property, Object newValue) {
  if ("binding".equals(property))
    setBinding((Binding) newValue);
  else if("metaClass".equals(property))
    setMetaClass((MetaClass)newValue);
  else
    binding.setVariable(property, newValue);
}

代码示例来源: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: twosigma/beakerx

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

代码示例来源: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: spring-projects/spring-integration

@Override
public void customize(GroovyObject goo) {
  Assert.state(goo instanceof Script, "Expected a Script");
  ((Script) goo).setBinding(this.binding);
  super.customize(goo);
}

代码示例来源:origin: org.fujion/fujion-script-groovy

@Override
public Object run(Map<String, Object> variables) {
  script.setBinding(variables == null ? null : new Binding(variables));
  return script.run();
}

代码示例来源:origin: freeplane/freeplane

@Override
public void setBinding(Binding binding) {
  super.setBinding(binding);
  updateBoundVariables();
}

代码示例来源:origin: org.carewebframework/org.carewebframework.web.script.groovy

@Override
public Object run(Map<String, Object> variables) {
  script.setBinding(variables == null ? null : new Binding(variables));
  return script.run();
}

代码示例来源:origin: org.elasticsearch.module/lang-groovy

/**
 * Return a script object with the given vars from the compiled script object
 */
@SuppressWarnings("unchecked")
private Script createScript(Object compiledScript, Map<String, Object> vars) throws InstantiationException, IllegalAccessException {
  Class scriptClass = (Class) compiledScript;
  Script scriptObject = (Script) scriptClass.newInstance();
  Binding binding = new Binding();
  binding.getVariables().putAll(vars);
  scriptObject.setBinding(binding);
  return scriptObject;
}

代码示例来源:origin: de.dfki.cos.basys.common/de.dfki.cos.basys.common.scxml

@Override
public void setBinding(final Binding binding) {
  super.setBinding(binding);
  this.context = ((GroovyContextBinding) binding).getContext();
}

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

public void setProperty(String property, Object newValue) {
  if ("binding".equals(property))
    setBinding((Binding) newValue);
  else if("metaClass".equals(property))
    setMetaClass((MetaClass)newValue);
  else
    binding.setVariable(property, newValue);
}

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

public void setProperty(String property, Object newValue) {
  if ("binding".equals(property))
    setBinding((Binding) newValue);
  else if("metaClass".equals(property))
    setMetaClass((MetaClass)newValue);
  else
    binding.setVariable(property, newValue);
}

代码示例来源:origin: org.elasticsearch/elasticsearch-lang-groovy

@Override
public Object execute(Object compiledScript, Map<String, Object> vars) {
  try {
    Class scriptClass = (Class) compiledScript;
    Script scriptObject = (Script) scriptClass.newInstance();
    Binding binding = new Binding(vars);
    scriptObject.setBinding(binding);
    return scriptObject.run();
  } catch (Exception e) {
    throw new ScriptException("failed to execute script", e);
  }
}

代码示例来源:origin: net.tirasa.connid/connector-framework-internal

@Override
  public Object execute(Map<String, Object> arguments) throws Exception {
    Map<String, Object> args = CollectionUtil.nullAsEmpty(arguments);
    groovyScript.setBinding(new Binding(args));
    return groovyScript.run();
  }
}

代码示例来源:origin: io.ratpack/ratpack-groovy

public RatpackDslClosures apply(Path file, String scriptContent) throws Exception {
 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 ScriptEngine<Script> scriptEngine = new ScriptEngine<>(classLoader, compileStatic, Script.class);
 return RatpackDslClosures.capture(function, file, () -> {
  Script script = scriptEngine.create(file.getFileName().toString(), file, scriptContent);
  script.setBinding(new Binding(args));
  script.run();
 });
}

代码示例来源:origin: org.jenkins-ci.plugins/matrix-project

/**
 * @param context
 *      Variables the script will see.
 */
private boolean evaluate(Binding context) {
  script.setBinding(context);
  try {
    return TRUE.equals(GroovySandbox.run(script, Whitelist.all()));
  } catch (RejectedAccessException x) {
    throw ScriptApproval.get().accessRejected(x, ApprovalContext.create());
  }
}

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

public Object build(Script script) {
  synchronized (script) {
    MetaClass scriptMetaClass = script.getMetaClass();
    try {
      script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this));
      script.setBinding(this);
      return script.run();
    } finally {
      script.setMetaClass(scriptMetaClass);
    }
  }
}

代码示例来源:origin: org.apache.camel/camel-groovy

public <T> T evaluate(Exchange exchange, Class<T> type) {
  Script script = instantiateScript(exchange);
  script.setBinding(createBinding(exchange));
  Object value = script.run();
  return exchange.getContext().getTypeConverter().convertTo(type, value);
}

相关文章