private Compile(String html, String code) throws IOException { this.pkg = find("package", ';', code); this.cls = find("class ", ' ', code); this.html = html; this.clfm = new ClassLoaderFileManager(); final JavaFileObject file = clfm.createMemoryFileObject( ClassLoaderFileManager.convertFQNToResource(pkg.isEmpty() ? cls : pkg + "." + cls) + Kind.SOURCE.extension, Kind.SOURCE, code.getBytes()); final JavaFileObject htmlFile = clfm.createMemoryFileObject( ClassLoaderFileManager.convertFQNToResource(pkg), Kind.OTHER, html.getBytes()); JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(clfm) { @Override public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { if (location == StandardLocation.SOURCE_PATH) { if (packageName.equals(pkg)) { return htmlFile; } } return null; } }; this.info = new CompilationInfo(file, jfm); }
private Map<String, byte[]> compile(final String html, final String code) throws IOException { final ClassLoaderFileManager clfm = new ClassLoaderFileManager(); final JavaFileObject file = clfm.createMemoryFileObject( ClassLoaderFileManager.convertFQNToResource(pkg.isEmpty() ? cls : pkg + "." + cls) + Kind.SOURCE.extension, Kind.SOURCE, code.getBytes()); final JavaFileObject htmlFile = clfm.createMemoryFileObject( ClassLoaderFileManager.convertFQNToResource(pkg), Kind.OTHER, html.getBytes()); JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(clfm) { @Override public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { if (location == StandardLocation.SOURCE_PATH) { if (packageName.equals(pkg)) { return htmlFile; } } return null; } }; final Boolean res = ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call(); Map<String, byte[]> result = new HashMap<>(); for (MemoryFileObject generated : clfm.getGeneratedFiles(Kind.CLASS)) { result.put(generated.getName().substring(1), generated.getContent()); } return result; }