org.codehaus.janino.Scanner Java Examples
The following examples show how to use
org.codehaus.janino.Scanner.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RexExecutable.java From Bats with Apache License 2.0 | 6 votes |
private static Function1<DataContext, Object[]> compile(String code, Object reason) { try { final ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(GENERATED_CLASS_NAME); cbe.setExtendedClass(Utilities.class); cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class}); cbe.setParentClassLoader(RexExecutable.class.getClassLoader()); cbe.cook(new Scanner(null, new StringReader(code))); Class c = cbe.getClazz(); //noinspection unchecked final Constructor<Function1<DataContext, Object[]>> constructor = c.getConstructor(); return constructor.newInstance(); } catch (CompileException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException("While compiling " + reason, e); } }
Example #2
Source File: RexExecutable.java From Quicksql with MIT License | 6 votes |
private static Function1<DataContext, Object[]> compile(String code, Object reason) { try { final ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(GENERATED_CLASS_NAME); cbe.setExtendedClass(Utilities.class); cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class}); cbe.setParentClassLoader(RexExecutable.class.getClassLoader()); cbe.cook(new Scanner(null, new StringReader(code))); Class c = cbe.getClazz(); //noinspection unchecked final Constructor<Function1<DataContext, Object[]>> constructor = c.getConstructor(); return constructor.newInstance(); } catch (CompileException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException("While compiling " + reason, e); } }
Example #3
Source File: JaninoClassCompiler.java From dremio-oss with Apache License 2.0 | 6 votes |
@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; }
Example #4
Source File: FunctionInitializer.java From dremio-oss with Apache License 2.0 | 6 votes |
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; } } }
Example #5
Source File: RexExecutable.java From calcite with Apache License 2.0 | 6 votes |
private static Function1<DataContext, Object[]> compile(String code, Object reason) { try { final ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(GENERATED_CLASS_NAME); cbe.setExtendedClass(Utilities.class); cbe.setImplementedInterfaces(new Class[] {Function1.class, Serializable.class}); cbe.setParentClassLoader(RexExecutable.class.getClassLoader()); cbe.cook(new Scanner(null, new StringReader(code))); Class c = cbe.getClazz(); //noinspection unchecked final Constructor<Function1<DataContext, Object[]>> constructor = c.getConstructor(); return constructor.newInstance(); } catch (CompileException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException("While compiling " + reason, e); } }
Example #6
Source File: JaninoClassCompiler.java From Bats with Apache License 2.0 | 5 votes |
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); }
Example #7
Source File: FunctionInitializer.java From Bats with Apache License 2.0 | 5 votes |
/** * 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 */ @VisibleForTesting CompilationUnit convertToCompilationUnit(Class<?> clazz) throws IOException { String path = clazz.getName(); path = path.replaceFirst("\\$.*", ""); path = path.replace(".", DrillFileUtils.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); } } }
Example #8
Source File: GASMifierTest.java From annotation-tools with MIT License | 4 votes |
public byte[] compile(String name, String source) throws Exception { Parser p = new Parser(new Scanner(name, new StringReader(source))); UnitCompiler uc = new UnitCompiler(p.parseCompilationUnit(), CL); return uc.compileUnit(DebuggingInformation.ALL)[0].toByteArray(); }
Example #9
Source File: ASMifierTest.java From annotation-tools with MIT License | 4 votes |
public byte[] compile(String name, String source) throws Exception { Parser p = new Parser(new Scanner(name, new StringReader(source))); UnitCompiler uc = new UnitCompiler(p.parseCompilationUnit(), CL); return uc.compileUnit(DebuggingInformation.ALL)[0].toByteArray(); }