Search in sources :

Example 1 with ClassFile

use of org.codehaus.janino.util.ClassFile in project drill by axbaretto.

the class JaninoClassCompiler method compile.

@Override
public Map<String, byte[]> compile(final ClassNames className, final String sourceCode) throws CompileException, IOException, ClassNotFoundException {
    ClassFile[] classFiles = doCompile(sourceCode);
    Map<String, byte[]> results = new HashMap<>();
    for (int i = 0; i < classFiles.length; i++) {
        ClassFile classFile = classFiles[i];
        results.put(classFile.getThisClassName(), classFile.toByteArray());
    }
    return results;
}
Also used : ClassFile(org.codehaus.janino.util.ClassFile) HashMap(java.util.HashMap)

Example 2 with ClassFile

use of org.codehaus.janino.util.ClassFile in project narchy by automenta.

the class DiffableFunctionMarshaller method compile.

private DiffableFunction compile(DiffableFunctionSource dfs) {
    SourceEnvironment se1 = new SourceEnvironment();
    String rv1 = dfs.valueToSource(se1);
    SourceEnvironment se2 = new SourceEnvironment();
    String rv2 = dfs.partialDeriveToSource(se2);
    String classPackage = getClass().getPackage().getName() + ".compiled";
    String className = "JaninoCompiledFastexpr" + COMPILED_CLASS_INDEX++;
    StringBuilder sb = new StringBuilder();
    sb.append("package ").append(classPackage).append(";\n");
    sb.append("import ").append(DiffableFunction.class.getCanonicalName()).append(";\n");
    sb.append("public class ").append(className).append(" implements DiffableFunction {\n");
    sb.append("public double value(double[] xValues) {\n");
    sb.append(se1);
    sb.append("return ").append(rv1).append(";\n");
    sb.append("}\n");
    sb.append("public double partialDerive(double[] xValues, int parameterIndex) {\n");
    sb.append(se2);
    sb.append("return ").append(rv2).append(";\n");
    sb.append("}\n");
    sb.append("}\n");
    try {
        Scanner scanner = new Scanner(null, new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), "UTF-8");
        JaninoRestrictedClassLoader cl = new JaninoRestrictedClassLoader();
        UnitCompiler unitCompiler = new UnitCompiler(new Parser(scanner).parseCompilationUnit(), new ClassLoaderIClassLoader(cl));
        ClassFile[] classFiles = unitCompiler.compileUnit(debug, debug, debug);
        Class<?> clazz = cl.defineClass(classPackage + "." + className, classFiles[0].toByteArray());
        return (DiffableFunction) clazz.newInstance();
    } catch (CompileException | IOException | InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(DiffableFunctionMarshaller.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
Also used : Scanner(org.codehaus.janino.Scanner) UnitCompiler(org.codehaus.janino.UnitCompiler) ClassFile(org.codehaus.janino.util.ClassFile) IOException(java.io.IOException) Parser(org.codehaus.janino.Parser) ClassLoaderIClassLoader(org.codehaus.janino.ClassLoaderIClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) CompileException(org.codehaus.commons.compiler.CompileException)

Example 3 with ClassFile

use of org.codehaus.janino.util.ClassFile in project drill by apache.

the class JaninoClassCompiler method compile.

@Override
public Map<String, byte[]> compile(final ClassNames className, final String sourceCode) throws CompileException, IOException, ClassNotFoundException {
    ClassFile[] classFiles = doCompile(sourceCode);
    Map<String, byte[]> results = new HashMap<>();
    for (int i = 0; i < classFiles.length; i++) {
        ClassFile classFile = classFiles[i];
        results.put(classFile.getThisClassName(), classFile.toByteArray());
    }
    return results;
}
Also used : ClassFile(org.codehaus.janino.util.ClassFile) HashMap(java.util.HashMap)

Aggregations

ClassFile (org.codehaus.janino.util.ClassFile)3 HashMap (java.util.HashMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 CompileException (org.codehaus.commons.compiler.CompileException)1 ClassLoaderIClassLoader (org.codehaus.janino.ClassLoaderIClassLoader)1 Parser (org.codehaus.janino.Parser)1 Scanner (org.codehaus.janino.Scanner)1 UnitCompiler (org.codehaus.janino.UnitCompiler)1