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