Search in sources :

Example 1 with Scanner

use of org.codehaus.janino.Scanner 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);
        }
    }
}
Also used : Scanner(org.codehaus.janino.Scanner) InputStream(java.io.InputStream) StringReader(java.io.StringReader) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) Parser(org.codehaus.janino.Parser)

Example 2 with Scanner

use of org.codehaus.janino.Scanner 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);
}
Also used : Scanner(org.codehaus.janino.Scanner) Java(org.codehaus.janino.Java) UnitCompiler(org.codehaus.janino.UnitCompiler) StringReader(java.io.StringReader) Parser(org.codehaus.janino.Parser)

Aggregations

StringReader (java.io.StringReader)2 Parser (org.codehaus.janino.Parser)2 Scanner (org.codehaus.janino.Scanner)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CompileException (org.codehaus.commons.compiler.CompileException)1 Java (org.codehaus.janino.Java)1 UnitCompiler (org.codehaus.janino.UnitCompiler)1