All pastes #399407 Raw Edit

Stuff

public text v1 · immutable
·published 2007-03-17 23:52 UTC
rendered paste body

import java.net.*;
import org.codehaus.janino.*;
import racer2d.core.*;
import java.io.*;
import org.newdawn.slick.util.*;
 
/**
 * An interpreter which loads classes from Java source
 * files.
 *
 * @author davedes
 */
public class JaninoInterpreter implements Interpreter {
    
    private SimpleCompiler compiler = new SimpleCompiler();
    
    /** Creates a new instance of JaninoInterpreter. */
    public JaninoInterpreter() {
    }
    
    /**
     * Compile the simple Java source file into the interpreter.
     * The Java source can only have one package but can contain
     * multiple <tt>public</tt> classes.
     *
     * @param ref the path to the module
     * @throws RuntimeException if the file wasn't found
     * @throws InterpreterException if there was a problem compiling
     */
    public void compile(String ref) throws InterpreterException {
        compile(ResourceLoader.getResourceAsStream(ref));
    }
    
    /**
     * Compile the simple Java source file into the interpreter.
     * The Java source can only have one package but can contain
     * multiple <tt>public</tt> classes.
     *
     * @param in the stream to the module
     * @throws InterpreterException if there was a problem compiling
     */
    public void compile(InputStream in) throws InterpreterException {
        try {
            compiler.cook(in);
        } catch (Exception e) {
            throw new InterpreterException("Error compiling source: "   
                                +e.getMessage(), e);
        }
    }
    
    /**
     * Called to load a new class instance. The source
     * files containing the class must be compiled before
     * the classes can be accessed through this method.
     * 
     * @param type the class type to return
     * @param name the qualified name of the class to load
     *
     * @return a new instance of the interpreted class
     * @throws ClassNotFoundException if the class was not found
     * @throws InterpreterException if there was an error 
     *                  interpreting the class
     * @throws ClassCastException if the class could not be cast 
     *                  to the given type
     */
    public <T> T get(Class<T> type, String name) throws InterpreterException,  
                                                    ClassNotFoundException,
                                                    ClassCastException {
        ClassLoader loader = compiler.getClassLoader();
        Object o = null;
        try {
            Class<?> klass = loader.loadClass(name);
            o = klass.newInstance();
        }
        catch (InstantiationException e) {
            throw new InterpreterException("Instantiation err: "
                            +e.getMessage(), e);
        }
        catch (IllegalAccessException e) {
            throw new InterpreterException("Access err: "
                            +e.getMessage(), e);
        }
        return (T)o;
    }
}