Search in sources :

Example 1 with ICompilerFactory

use of org.codehaus.commons.compiler.ICompilerFactory 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)

Example 2 with ICompilerFactory

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

the class EnumerableInterpretable method getBindable.

static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount) throws CompileException, IOException {
    ICompilerFactory compilerFactory;
    try {
        compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(expr.name);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(fieldCount == 1 ? new Class[] { Bindable.class, Typed.class } : new Class[] { ArrayBindable.class });
    cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
    if (CalcitePrepareImpl.DEBUG) {
        // Add line numbers to the generated janino class
        cbe.setDebuggingInformation(true, true, true);
    }
    return (Bindable) cbe.createInstance(new StringReader(s));
}
Also used : Typed(org.apache.calcite.runtime.Typed) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) Bindable(org.apache.calcite.runtime.Bindable) StringReader(java.io.StringReader) IClassBodyEvaluator(org.codehaus.commons.compiler.IClassBodyEvaluator) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException)

Example 3 with ICompilerFactory

use of org.codehaus.commons.compiler.ICompilerFactory in project samza by apache.

the class RexToJavaCompiler method getExpression.

/**
 * Creates the instance of the class defined in {@link ClassDeclaration}
 * @param expr Interface whose instance needs to be created.
 * @param s The java code that implements the interface which should be used to create the instance.
 * @return The object of the class which implements the interface {@link Expression} with the code that is passed as input.
 * @throws CompileException
 * @throws IOException
 */
static Expression getExpression(ClassDeclaration expr, String s) throws CompileException, IOException {
    ICompilerFactory compilerFactory;
    try {
        compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(expr.name);
    cbe.setImplementedInterfaces(expr.implemented.toArray(new Class[expr.implemented.size()]));
    cbe.setParentClassLoader(RexToJavaCompiler.class.getClassLoader());
    cbe.setDebuggingInformation(true, true, true);
    return (org.apache.samza.sql.data.Expression) cbe.createInstance(new StringReader(s));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) StringReader(java.io.StringReader) IClassBodyEvaluator(org.codehaus.commons.compiler.IClassBodyEvaluator) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException)

Example 4 with ICompilerFactory

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

the class JaninoRexCompiler method getScalar.

static Scalar getScalar(ClassDeclaration expr, String s) throws CompileException, IOException {
    ICompilerFactory compilerFactory;
    try {
        compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(expr.name);
    cbe.setImplementedInterfaces(new Class[] { Scalar.class });
    cbe.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
    if (CalcitePrepareImpl.DEBUG) {
        // Add line numbers to the generated janino class
        cbe.setDebuggingInformation(true, true, true);
    }
    return (Scalar) cbe.createInstance(new StringReader(s));
}
Also used : ICompilerFactory(org.codehaus.commons.compiler.ICompilerFactory) StringReader(java.io.StringReader) IClassBodyEvaluator(org.codehaus.commons.compiler.IClassBodyEvaluator) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)4 CompileException (org.codehaus.commons.compiler.CompileException)4 ICompilerFactory (org.codehaus.commons.compiler.ICompilerFactory)4 StringReader (java.io.StringReader)3 IClassBodyEvaluator (org.codehaus.commons.compiler.IClassBodyEvaluator)3 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)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 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)1 ArrayBindable (org.apache.calcite.runtime.ArrayBindable)1 Bindable (org.apache.calcite.runtime.Bindable)1 Typed (org.apache.calcite.runtime.Typed)1 ControlFlowException (org.apache.calcite.util.ControlFlowException)1 SamzaException (org.apache.samza.SamzaException)1 ISimpleCompiler (org.codehaus.commons.compiler.ISimpleCompiler)1