Search in sources :

Example 1 with JaninoCompiler

use of org.apache.calcite.util.javac.JaninoCompiler in project calcite by apache.

the class SparkHandlerImpl method compile.

public ArrayBindable compile(ClassDeclaration expr, String s) {
    final String className = "CalciteProgram" + classId.getAndIncrement();
    final File file = new File(SRC_DIR, className + ".java");
    try (Writer w = Util.printWriter(file)) {
        String source = "public class " + className + "\n" + "    implements " + ArrayBindable.class.getName() + ", " + Serializable.class.getName() + " {\n" + s + "\n" + "}\n";
        System.out.println("======================");
        System.out.println(source);
        System.out.println("======================");
        w.write(source);
        w.close();
        JaninoCompiler compiler = new JaninoCompiler();
        compiler.getArgs().setDestdir(CLASS_DIR.getAbsolutePath());
        compiler.getArgs().setSource(source, file.getAbsolutePath());
        compiler.getArgs().setFullClassName(className);
        compiler.compile();
        @SuppressWarnings("unchecked") final Class<ArrayBindable> clazz = (Class<ArrayBindable>) Class.forName(className);
        final Constructor<ArrayBindable> constructor = clazz.getConstructor();
        return constructor.newInstance();
    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : Serializable(java.io.Serializable) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JaninoCompiler(org.apache.calcite.util.javac.JaninoCompiler) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) File(java.io.File) Writer(java.io.Writer)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayBindable (org.apache.calcite.runtime.ArrayBindable)1 JaninoCompiler (org.apache.calcite.util.javac.JaninoCompiler)1