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); }
private void generateEvaluatorClass() throws Exception { Scanner scanner = new Scanner(null, new StringReader(expression)); Parser parser = new Parser(scanner); Location loc = parser.location(); String className = "TupleExpression" + counter.incrementAndGet(); setClassName(packageName + "." + className); Java.CompilationUnit cu = makeCompilationUnit(parser); cu.setPackageDeclaration(new Java.PackageDeclaration(loc, packageName)); Java.PackageMemberClassDeclaration cd = new Java.PackageMemberClassDeclaration(loc, null, new Java.Modifiers(Mod.PUBLIC), className, null, null, new Java.Type[] { classToType(loc, iface) } ); cu.addPackageMemberTypeDeclaration(cd); cd.addConstructor(nullConstructor(loc)); cd.addDeclaredMethod(generateFrontendMethod(loc)); cd.addDeclaredMethod(generateBackendMethod(parser)); this.evaluatorClass = compileToClass(cu); this.evaluator = evaluatorClass.getConstructor().newInstance(); }
private Java.MethodDeclarator generateBackendMethod(Parser parser) throws Exception { Location loc = parser.location(); List<Java.BlockStatement> statements = Lists.newArrayList(); Java.Rvalue[] exprs = parser.parseExpressionList(); for (int i=0; i<exprs.length; i++) { if (i == exprs.length - 1) { statements.add(maybeGenerateReturn(loc, exprs[i])); } else { statements.add(new Java.ExpressionStatement(exprs[i])); } } return new Java.MethodDeclarator(loc, null, new Java.Modifiers(Mod.PRIVATE), classToType(loc, returnType), "doEval", generateArgs(loc, schema.tupleClass()), new Java.Type[0], statements ); }
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);
private CompilationUnit get(Class<?> c) throws IOException { URL u = getSourceURL(c); try (Reader reader = Resources.asCharSource(u, UTF_8).openStream()) { String body = CharStreams.toString(reader); // TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem... body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", ""); for(Replacement r : REPLACERS){ body = r.apply(body); } // System.out.println("original"); // System.out.println(body);; // System.out.println("decompiled"); // System.out.println(decompile(c)); try { return new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit(); } catch (CompileException e) { logger.warn("Failure while parsing function class:\n{}", body, e); return null; } } }
@Override protected ClassBytes[] getByteCode(final ClassNames className, final String sourcecode, boolean debug) throws CompileException, IOException, ClassNotFoundException, ClassTransformationException { StringReader reader = new StringReader(sourcecode); Scanner scanner = new Scanner((String) null, reader); Java.CompilationUnit compilationUnit = new Parser(scanner).parseCompilationUnit(); ClassFile[] classFiles = new UnitCompiler(compilationUnit, compilationClassLoader) .compileUnit(debug, debug, debug); ClassBytes[] byteCodes = new ClassBytes[classFiles.length]; for(int i = 0; i < classFiles.length; i++){ ClassFile file = classFiles[i]; byteCodes[i] = new ClassBytes(file.getThisClassName(), file.toByteArray()); } return byteCodes; }
new Parser(scanner).parseCompilationUnit(), new ClassLoaderIClassLoader(cl)); ClassFile[] classFiles = unitCompiler.compileUnit(true, true, true);
@Override public Class<?> compileClass(SourceCodeContext sourceCode) throws SourceCodeGenerationException { Scanner scanner; try { scanner = new Scanner(sourceCode.getClassName(), new StringReader(sourceCode.toSourceFile())); Java.CompilationUnit localCompilationUnit = new Parser(scanner).parseCompilationUnit(); UnitCompiler unitCompile = new UnitCompiler(localCompilationUnit, iClassLoader); ClassFile[] classes = unitCompile.compileUnit(false, false, false); return classLoader.findClass(classes[0].getThisClassName()); } catch (Exception e) { LOG.error("Can not compile {0}", sourceCode.getClassName(), e); throw new MappingException("Can not compile the generated mapper", e); } }
new Parser(scanner).parseCompilationUnit(), new ClassLoaderIClassLoader(cl)); ClassFile[] classFiles = unitCompiler.compileUnit(true, true, true);