org.codehaus.groovy.tools.shell.Interpreter.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all Show documentation
Show all versions of groovy-all Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.tools.shell
import org.codehaus.groovy.tools.shell.util.Logger
import org.codehaus.groovy.runtime.MethodClosure
import java.lang.reflect.Method
/**
* ???
*
* @version $Id: Interpreter.groovy 12266 2008-05-04 10:11:31Z user57 $
* @author Jason Dillon
*/
class Interpreter
{
static final String SCRIPT_FILENAME = 'groovysh_evaluate'
private final Logger log = Logger.create(this.class)
private final GroovyShell shell
Interpreter(final ClassLoader classLoader, final Binding binding) {
assert classLoader
assert binding
shell = new GroovyShell(classLoader, binding)
}
Binding getContext() {
return shell.context
}
GroovyClassLoader getClassLoader() {
return shell.classLoader
}
def evaluate(final List buffer) {
assert buffer
def source = buffer.join(Parser.NEWLINE)
def result
Class type
try {
Script script = shell.parse(source, SCRIPT_FILENAME)
type = script.getClass()
log.debug("Compiled script: $script")
if (type.declaredMethods.any { it.name == 'main' }) {
result = script.run()
}
// Need to use String.valueOf() here to avoid icky exceptions causes by GString coercion
log.debug("Evaluation result: ${String.valueOf(result)} (${result?.getClass()})")
// Keep only the methods that have been defined in the script
type.declaredMethods.each { Method m ->
if (!(m.name in [ 'main', 'run' ] || m.name.startsWith('super$') || m.name.startsWith('class$') || m.name.startsWith('$'))) {
log.debug("Saving method definition: $m")
shell.context["${m.name}"] = new MethodClosure(type.newInstance(), m.name)
}
}
}
finally {
def cache = shell.classLoader.classCache
// Remove the script class generated
cache.remove(type?.name)
// Remove the inline closures from the cache as well
cache.remove('$_run_closure')
}
return result
}
}