本文整理了Java中groovy.lang.GroovyShell
类的一些代码示例,展示了GroovyShell
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GroovyShell
类的具体详情如下:
包路径:groovy.lang.GroovyShell
类名称:GroovyShell
[英]Represents a groovy shell capable of running arbitrary groovy scripts
[中]表示能够运行任意groovy脚本的groovy shell
代码示例来源: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
/**
* Parses the bean definition groovy script by first exporting the given {@link Binding}.
*/
public void parse(InputStream script, Binding binding) {
if (script==null)
throw new IllegalArgumentException("No script is provided");
setBinding(binding);
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(ClosureScript.class.getName());
GroovyShell shell = new GroovyShell(classLoader,binding,cc);
ClosureScript s = (ClosureScript)shell.parse(new InputStreamReader(script));
s.setDelegate(this);
s.run();
}
代码示例来源: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: org.codehaus.groovy/groovy
/**
* Parses the given script and returns it ready to be run
*
* @param scriptText the text of the script
*/
public Script parse(String scriptText) throws CompilationFailedException {
return parse(scriptText, generateScriptName());
}
代码示例来源: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: spockframework/spock
private GroovyShell createShell() {
CompilerConfiguration compilerSettings = new CompilerConfiguration();
compilerSettings.setScriptBaseClass(DelegatingScript.class.getName());
return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings);
}
}
代码示例来源:origin: org.codehaus.groovy/groovy
public static void processConfigScripts(List<String> scripts, CompilerConfiguration conf) throws IOException {
if (scripts.isEmpty()) return;
Binding binding = new Binding();
binding.setVariable("configuration", conf);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
for (String script : scripts) {
shell.evaluate(new File(script));
}
}
代码示例来源:origin: org.codehaus.groovy/groovy
@Override
public void setup() {
CompilerConfiguration config = new CompilerConfiguration();
config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL");
ImportCustomizer ic = new ImportCustomizer();
ic.addStarImports("org.codehaus.groovy.ast.expr");
ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper");
ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport");
config.addCompilationCustomizers(ic);
final GroovyClassLoader transformLoader = compilationUnit!=null?compilationUnit.getTransformLoader():typeCheckingVisitor.getSourceUnit().getClassLoader();
Class<?> clazz = transformLoader.loadClass(scriptPath, false, true);
if (TypeCheckingDSL.class.isAssignableFrom(clazz)) {
script = (TypeCheckingDSL) clazz.newInstance();
GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config);
script = (TypeCheckingDSL) shell.parse(
new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding())
);
代码示例来源:origin: palantir/atlasdb
private void evalFiles(String[] filepaths, CommandLine cli) throws CompilationFailedException, IOException {
Binding binding = setupBinding(new Binding(), cli.hasOption(MUTATIONS_ENABLED_FLAG_SHORT));
GroovyShell shell = new GroovyShell(binding);
if(cli.hasOption(CLASSPATH_FLAG_SHORT)) {
shell.getClassLoader().addClasspath(cli.getOptionValue(CLASSPATH_FLAG_SHORT));
}
for(String filepath : filepaths) {
File file = new File(filepath);
shell.evaluate(file);
}
}
代码示例来源: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: spring-projects/spring-framework
binding.setVariable("beans", beans);
GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
shell.evaluate(encodedResource.getReader(), "beans");
代码示例来源:origin: crashub/crash
@Override
protected void setUp() throws Exception {
CompilerConfiguration config = new CompilerConfiguration();
config.setScriptBaseClass(GroovyScriptCommand.class.getName());
//
loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
shell = new GroovyShell(loader);
}
代码示例来源: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: apache/nifi
CompilerConfiguration conf = new CompilerConfiguration();
conf.setDebug(true);
shell = new GroovyShell(conf);
if (addClasspath != null && addClasspath.length() > 0) {
for (File fcp : Files.listPathsFiles(addClasspath)) {
throw new ProcessException("Path not found `" + fcp + "` for `" + ADD_CLASSPATH.getDisplayName() + "`");
shell.getClassLoader().addClasspath(fcp.toString());
shell.getClassLoader().addClasspath(groovyClasspath);
scriptText = scriptBody;
script = shell.parse(PRELOADS + scriptText, scriptName);
compiled = (Class<Script>) script.getClass();
script = compiled.newInstance();
Thread.currentThread().setContextClassLoader(shell.getClassLoader());
return script;
代码示例来源:origin: org.integratedmodelling/klab-common
private void compile(String code) {
this.compiler.setScriptBaseClass(getBaseClass());
this.shell = new GroovyShell(this.getClass()
.getClassLoader(), new Binding(), compiler);
this.script = shell.parse(code);
}
代码示例来源:origin: apache/groovy
private void configureCompiler() {
if (scriptBaseClass!=null) {
configuration.setScriptBaseClass(scriptBaseClass);
}
if (indy) {
configuration.getOptimizationOptions().put("indy", Boolean.TRUE);
configuration.getOptimizationOptions().put("int", Boolean.FALSE);
}
if (configscript!=null) {
Binding binding = new Binding();
binding.setVariable("configuration", configuration);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
File confSrc = new File(configscript);
try {
shell.evaluate(confSrc);
} catch (IOException e) {
throw new BuildException("Unable to configure compiler using configuration file: "+confSrc, e);
}
}
}
代码示例来源:origin: apache/groovy
thread.setContextClassLoader(GroovyShell.class.getClassLoader());
@Override
public GroovyClassLoader run() {
return new GroovyClassLoader(baseClassLoader);
final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
try {
parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
} finally {
groovy.resetLoadedClasses();
groovy.getClassLoader().clearCache();
if (contextClassLoader || maven) thread.setContextClassLoader(savedLoader);
代码示例来源:origin: groovy/groovy-core
public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
// @todo refactor this path
File currentDir = new File("./src/test/groovy/bugs");
String file = "bug1567_script.groovy";
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
String[] test = null;
Object result = shell.run(new File(currentDir, file), test);
String[] roots = new String[]{currentDir.getAbsolutePath()};
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
binding = new Binding();
// a MME was ensued here stating no 't.start()' was available
// in the script
gse.run(file, binding);
}
}
代码示例来源:origin: stackoverflow.com
new GroovyShell().parse( new File( "test.groovy" ) ).invokeMethod( "hello_world", null ) ;
Class scriptClass = new GroovyClassLoader().parseClass( new File( "test.groovy" ) ) ;
Object scriptInstance = scriptClass.newInstance() ;
scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
代码示例来源:origin: jenkinsci/jenkins
/**
* Can be used to customize the environment in which the script runs.
*/
protected GroovyShell createShell() {
return new GroovyShell(loader, bindings);
}
内容来源于网络,如有侵权,请联系作者删除!