use of org.codehaus.janino.Parser in project drill by apache.
the class FunctionInitializer method convertToCompilationUnit.
/**
* Using class name generates path to class source code (*.java),
* reads its content as string and parses it into {@link org.codehaus.janino.Java.CompilationUnit}.
*
* @param clazz function class
* @return compilation unit
* @throws IOException if did not find class or could not load it
*/
private CompilationUnit convertToCompilationUnit(Class<?> clazz) throws IOException {
String path = clazz.getName();
path = path.replaceFirst("\\$.*", "");
path = path.replace(".", FileUtils.separator);
path = "/" + path + ".java";
logger.trace("Loading function code from the {}", path);
try (InputStream is = clazz.getResourceAsStream(path)) {
if (is == null) {
throw new IOException(String.format("Failure trying to locate source code for class %s, tried to read on classpath location %s", clazz.getName(), path));
}
String body = IO.toString(is);
// TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem...
body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", "");
try {
return new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit();
} catch (CompileException e) {
throw new IOException(String.format("Failure while loading class %s.", clazz.getName()), e);
}
}
}
use of org.codehaus.janino.Parser in project drill by apache.
the class JaninoClassCompiler method doCompile.
private ClassFile[] doCompile(final String sourceCode) throws CompileException, IOException, ClassNotFoundException {
StringReader reader = new StringReader(sourceCode);
Scanner scanner = new Scanner((String) null, reader);
Java.CompilationUnit compilationUnit = new Parser(scanner).parseCompilationUnit();
return new UnitCompiler(compilationUnit, compilationClassLoader).compileUnit(this.debug, this.debug, this.debug);
}
Aggregations