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);
}
Aggregations