| Constructor: |
public GroovyShell() {
this(null, new Binding());
}
|
public GroovyShell(Binding binding) {
this(null, binding);
}
|
public GroovyShell(CompilerConfiguration config) {
this(new Binding(), config);
}
|
public GroovyShell(ClassLoader parent) {
this(parent, new Binding(), CompilerConfiguration.DEFAULT);
}
|
public GroovyShell(GroovyShell shell) {
this(shell.loader, shell.context);
}
Creates a child shell using a new ClassLoader which uses the parent shell's
class loader as its parent Parameters:
shell - is the parent shell used for the variable bindings and the parent class loader
|
public GroovyShell(Binding binding,
CompilerConfiguration config) {
this(null, binding, config);
}
|
public GroovyShell(ClassLoader parent,
Binding binding) {
this(parent, binding, CompilerConfiguration.DEFAULT);
}
|
public GroovyShell(ClassLoader parent,
Binding binding,
CompilerConfiguration config) {
if (binding == null) {
throw new IllegalArgumentException("Binding must not be null.");
}
if (config == null) {
throw new IllegalArgumentException("Compiler configuration must not be null.");
}
final ClassLoader parentLoader = (parent!=null)?parent:GroovyShell.class.getClassLoader();
this.loader = (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new GroovyClassLoader(parentLoader,config);
}
});
this.context = binding;
this.config = config;
}
|
| Method from groovy.lang.GroovyShell Detail: |
public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException {
Script script = parse(codeSource);
return script.run();
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(File file) throws CompilationFailedException, IOException {
return evaluate(new GroovyCodeSource(file));
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(String scriptText) throws CompilationFailedException {
try {
return evaluate(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), generateScriptName());
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(InputStream in) throws CompilationFailedException {
return evaluate(in, generateScriptName());
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(String scriptText,
String fileName) throws CompilationFailedException {
try {
return evaluate(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), fileName);
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(InputStream in,
String fileName) throws CompilationFailedException {
Script script = null;
try {
script = parse(in, fileName);
return script.run();
} finally {
if (script != null) {
InvokerHelper.removeClass(script.getClass());
}
}
}
Evaluates some script against the current Binding and returns the result |
public Object evaluate(String scriptText,
String fileName,
String codeBase) throws CompilationFailedException {
try {
return evaluate(new GroovyCodeSource(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), fileName, codeBase));
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
Evaluates some script against the current Binding and returns the result.
The .class file created from the script is given the supplied codeBase |
protected synchronized String generateScriptName() {
return "Script" + (++counter) + ".groovy";
}
|
public GroovyClassLoader getClassLoader() {
return loader;
}
|
public Binding getContext() {
return context;
}
|
public Object getProperty(String property) {
Object answer = getVariable(property);
if (answer == null) {
answer = super.getProperty(property);
}
return answer;
}
|
public Object getVariable(String name) {
return context.getVariables().get(name);
}
|
public void initializeBinding() {
Map map = context.getVariables();
if (map.get("shell")==null) map.put("shell",this);
}
|
public static void main(String[] args) {
GroovyMain.main(args);
}
|
public Script parse(GroovyCodeSource codeSource) throws CompilationFailedException {
return InvokerHelper.createScript(parseClass(codeSource), context);
}
Parses the given script and returns it ready to be run. When running in a secure environment
(-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be
given to the script. |
public Script parse(File file) throws CompilationFailedException, IOException {
return parse(new GroovyCodeSource(file));
}
Parses the given script and returns it ready to be run |
public Script parse(String scriptText) throws CompilationFailedException {
try {
return parse(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), generateScriptName());
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
Parses the given script and returns it ready to be run |
public Script parse(InputStream in) throws CompilationFailedException {
return parse(in, generateScriptName());
}
Parses the given script and returns it ready to be run |
public Script parse(InputStream in,
String fileName) throws CompilationFailedException {
GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new GroovyCodeSource(in, fileName, "/groovy/shell");
}
});
return parse(gcs);
}
Parses the given script and returns it ready to be run |
public Script parse(String scriptText,
String fileName) throws CompilationFailedException {
try {
return parse(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), fileName);
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
|
public void resetLoadedClasses() {
loader.clearCache();
}
|
public Object run(File scriptFile,
List list) throws CompilationFailedException, IOException {
String[] args = new String[list.size()];
return run(scriptFile, (String[]) list.toArray(args));
}
A helper method which runs the given script file with the given command line arguments |
public Object run(File scriptFile,
String[] args) throws CompilationFailedException, IOException {
String scriptName = scriptFile.getName();
int p = scriptName.lastIndexOf(".");
if (p++ >= 0) {
if (scriptName.substring(p).equals("java")) {
System.err.println("error: cannot compile file with .java extension: " + scriptName);
throw new CompilationFailedException(0, null);
}
}
// Get the current context classloader and save it on the stack
final Thread thread = Thread.currentThread();
//ClassLoader currentClassLoader = thread.getContextClassLoader();
class DoSetContext implements PrivilegedAction {
ClassLoader classLoader;
public DoSetContext(ClassLoader loader) {
classLoader = loader;
}
public Object run() {
thread.setContextClassLoader(classLoader);
return null;
}
}
AccessController.doPrivileged(new DoSetContext(loader));
// Parse the script, generate the class, and invoke the main method. This is a little looser than
// if you are compiling the script because the JVM isn't executing the main method.
Class scriptClass;
try {
scriptClass = (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws CompilationFailedException, IOException {
return loader.parseClass(scriptFile);
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (e instanceof CompilationFailedException) {
throw (CompilationFailedException) e;
} else if (e instanceof IOException) {
throw (IOException) e;
} else {
throw (RuntimeException) pae.getException();
}
}
return runScriptOrMainOrTestOrRunnable(scriptClass, args);
// Set the context classloader back to what it was.
//AccessController.doPrivileged(new DoSetContext(currentClassLoader));
}
Runs the given script file name with the given command line arguments |
public Object run(String scriptText,
String fileName,
List list) throws CompilationFailedException {
String[] args = new String[list.size()];
list.toArray(args);
return run(scriptText, fileName, args);
}
A helper method which runs the given cl script with the given command line arguments |
public Object run(String scriptText,
String fileName,
String[] args) throws CompilationFailedException {
try {
return run(new ByteArrayInputStream(scriptText.getBytes(config.getSourceEncoding())), fileName, args);
} catch (UnsupportedEncodingException e) {
throw new CompilationFailedException(0, null, e);
}
}
Runs the given script text with command line arguments |
public Object run(InputStream in,
String fileName,
String[] args) throws CompilationFailedException {
GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new GroovyCodeSource(in, fileName, "/groovy/shell");
}
});
Class scriptClass = parseClass(gcs);
return runScriptOrMainOrTestOrRunnable(scriptClass, args);
}
Runs the given script with command line arguments |
public void setProperty(String property,
Object newValue) {
setVariable(property, newValue);
try {
super.setProperty(property, newValue);
} catch (GroovyRuntimeException e) {
// ignore, was probably a dynamic property
}
}
|
public void setVariable(String name,
Object value) {
context.setVariable(name, value);
}
|