public void assembleCompilerClassPath(OnTheFlyJavaCompiler compiler) { if (parentClassLoader instanceof URLClassLoader) { URL[] urLs = ((URLClassLoader) parentClassLoader).getURLs(); for (URL url : urLs) { final String urlAsString = url.getFile(); compiler.addClassPath(urlAsString); } } }
public Class<?> compileToClass(String classname, String code) { return compileToClasses(singletonMap(classname, code)).get(classname); }
public Pair<ClassLoader, Map<String,Class<?>>> internalCompileToClasses(Map<String,String> sources) { File tempDir = createTempDir(); try { for (Entry<String, String> entry : sources.entrySet()) { final String classNameAsPath = classname.replace('.', File.separatorChar); final File srcFile = new File(tempDir, classNameAsPath + ".java"); createFolderStructure(srcFile.getParentFile()); srcFile.createNewFile(); Files.writeStringIntoFile(srcFile.getCanonicalPath(), code); StringBuilder sb = new StringBuilder(getComplianceLevelArg()); sb.append(" "); sb.append(getClasspathArgs()); sb.append(" "); sb.append('\"'); sb.append(tempDir.getCanonicalPath()); sb.append('\"'); boolean compile = compile(sb.toString()); if (!compile) throw new IllegalArgumentException("Couldn't compile : " throw new RuntimeException(e); } finally { cleanUpTmpFolder(tempDir);
protected Object internalCreateFunction(String code, Type returnType, @SuppressWarnings("unchecked") Pair<Type, String>... params) { Pair<String, String> fullCode = createFullCode(code, returnType, params); Class<?> class1 = compileToClass(fullCode.getFirst(), fullCode.getSecond()); try { return class1.newInstance(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new WrappedException(e); } }
public void initializeClassPath() { clearClassPath(); classPathAssembler.assembleCompilerClassPath(this); }