home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (Interpreter.groovy)

This example Groovy source code file (Interpreter.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

binding, binding, classloader, evaluation, groovyclassloader, groovyshell, groovyshell, interpreter, interpreter, list, reflection, saving, script_filename, script_filename, string

The Groovy Interpreter.groovy source code

 * 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,
 * 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

 * Helper to interpret a source buffer.
 * @version $Id: Interpreter.groovy 15932 2009-04-08 03:51:03Z user57 $
 * @author <a href="mailto:jason@planet57.com">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.getContext()

    GroovyClassLoader getClassLoader() {
        return shell.getClassLoader()

    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")

                    context["${m.name}"] = new MethodClosure(type.newInstance(), m.name)
        finally {
            def cache = classLoader.classCache

            // Remove the script class generated

            // Remove the inline closures from the cache as well

        return result

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Interpreter.groovy source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.