Search in sources :

Example 1 with ISimpleCompiler

use of org.codehaus.commons.compiler.ISimpleCompiler in project calcite by apache.

the class JaninoRelMetadataProvider method compile.

static <M extends Metadata> MetadataHandler<M> compile(String className, String classBody, MetadataDef<M> def, List<Object> argList) throws CompileException, IOException {
    final ICompilerFactory compilerFactory;
    try {
        compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to instantiate java compiler", e);
    }
    final ISimpleCompiler compiler = compilerFactory.newSimpleCompiler();
    compiler.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
    final String s = "public final class " + className + " implements " + def.handlerClass.getCanonicalName() + " {\n" + classBody + "\n" + "}";
    if (CalcitePrepareImpl.DEBUG) {
        // Add line numbers to the generated janino class
        compiler.setDebuggingInformation(true, true, true);
        System.out.println(s);
    }
    compiler.cook(s);
    final Constructor constructor;
    final Object o;
    try {
        constructor = compiler.getClassLoader().loadClass(className).getDeclaredConstructors()[0];
        o = constructor.newInstance(argList.toArray());
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    return def.handlerClass.cast(o);
}
Also used : ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ControlFlowException(org.apache.calcite.util.ControlFlowException) CompileException(org.codehaus.commons.compiler.CompileException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JaninoRexCompiler(org.apache.calcite.interpreter.JaninoRexCompiler) InvocationTargetException(java.lang.reflect.InvocationTargetException) ISimpleCompiler(org.codehaus.commons.compiler.ISimpleCompiler)

Aggregations

UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ExecutionException (java.util.concurrent.ExecutionException)1 JaninoRexCompiler (org.apache.calcite.interpreter.JaninoRexCompiler)1 ControlFlowException (org.apache.calcite.util.ControlFlowException)1 CompileException (org.codehaus.commons.compiler.CompileException)1 ICompilerFactory (org.codehaus.commons.compiler.ICompilerFactory)1 ISimpleCompiler (org.codehaus.commons.compiler.ISimpleCompiler)1