groovy.lang.Binding.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(288)

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

Binding.<init>介绍

[英]A helper constructor used in main(String[]) method calls
[中]在main(String[])方法调用中使用的帮助器构造函数

代码示例

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

/**
 * Parses the bean definition groovy script.
 */
public void parse(InputStream script) {
  parse(script,new Binding());
}

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

public Writable make(Map map) {
    if (map == null) {
      throw new IllegalArgumentException("map must not be null");
    }
    return new XmlWritable(script, new Binding(map));
  }
}

代码示例来源:origin: spockframework/spock

private GroovyShell createShell() {
  CompilerConfiguration compilerSettings = new CompilerConfiguration();
  compilerSettings.setScriptBaseClass(DelegatingScript.class.getName());
  return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings);
 }
}

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

/**
 * Filter to run for the LegacySecurityRealm is the
 * ChainServletFilter legacy from /WEB-INF/security/SecurityFilters.groovy.
 */
@Override
public Filter createFilter(FilterConfig filterConfig) {
  Binding binding = new Binding();
  SecurityComponents sc = this.createSecurityComponents();
  binding.setVariable("securityComponents", sc);
  binding.setVariable("securityRealm",this);
  BeanBuilder builder = new BeanBuilder();
  builder.parse(filterConfig.getServletContext().getResourceAsStream("/WEB-INF/security/SecurityFilters.groovy"),binding);
  
  WebApplicationContext context = builder.createApplicationContext();
  
  return (Filter) context.getBean("legacy");
}

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

/**
 * Creates {@link Filter} that all the incoming HTTP requests will go through
 * for authentication.
 *
 * <p>
 * The default implementation uses {@link #getSecurityComponents()} and builds
 * a standard filter chain from /WEB-INF/security/SecurityFilters.groovy.
 * But subclasses can override this to completely change the filter sequence.
 *
 * <p>
 * For other plugins that want to contribute {@link Filter}, see
 * {@link PluginServletFilter}.
 *
 * @since 1.271
 */
public Filter createFilter(FilterConfig filterConfig) {
  LOGGER.entering(SecurityRealm.class.getName(), "createFilter");
  
  Binding binding = new Binding();
  SecurityComponents sc = getSecurityComponents();
  binding.setVariable("securityComponents", sc);
  binding.setVariable("securityRealm",this);
  BeanBuilder builder = new BeanBuilder();
  builder.parse(filterConfig.getServletContext().getResourceAsStream("/WEB-INF/security/SecurityFilters.groovy"),binding);
  WebApplicationContext context = builder.createApplicationContext();
  return (Filter) context.getBean("filter");
}

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

public Writable make(final Map map) {
    final Closure template = ((Closure) this.template.clone()).asWritable();
    Binding binding = new Binding(map);
    template.setDelegate(binding);
    return (Writable) template;
  }
}

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

@Override
public void run() {
  try {
    Binding b = new Binding();
    b.setVariable("number", count);
    result = (String) this.gse.run(script, b);
  } catch (Throwable t) {
    throw new RuntimeException("problem running script", t);
  }
}

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

@Override
public SecurityComponents createSecurityComponents() {
  Binding binding = new Binding();
  binding.setVariable("authenticator", new Authenticator());
  BeanBuilder builder = new BeanBuilder();
  builder.parse(Jenkins.getInstance().servletContext.getResourceAsStream("/WEB-INF/security/AbstractPasswordBasedSecurityRealm.groovy"),binding);
  WebApplicationContext context = builder.createApplicationContext();
  return new SecurityComponents(
      findBean(AuthenticationManager.class, context),
      new ImpersonatingUserDetailsService(this));
}

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

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: jenkinsci/jenkins

protected int run() throws Exception {
  // this allows the caller to manipulate the JVM state, so require the execute script privilege.
  Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS);
  Binding binding = new Binding();
  binding.setProperty("out",new PrintWriter(stdout,true));
  binding.setProperty("stdin",stdin);
  binding.setProperty("stdout",stdout);
  binding.setProperty("stderr",stderr);
  binding.setProperty("channel",channel);
  if (channel != null) {
    String j = getClientEnvironmentVariable("JOB_NAME");
    if (j != null) {
      Item job = Jenkins.getActiveInstance().getItemByFullName(j);
      binding.setProperty("currentJob", job);
      String b = getClientEnvironmentVariable("BUILD_NUMBER");
      if (b != null && job instanceof AbstractProject) {
        Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
        binding.setProperty("currentBuild", r);
      }
    }
  }
  GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding);
  groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()]));
  return 0;
}

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

private void reloadClassloader() {
 this.beakerxUrlClassLoader = newParentClassLoader(getClasspath());
 this.icz = new ImportCustomizer();
 this.groovyClassLoader = newEvaluator(getImports(), getClasspath(), getOutDir(), icz, beakerxUrlClassLoader);
 this.scriptBinding = new Binding();
}

代码示例来源: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"));
}

相关文章