@Override public PrintWriter openWriter(CompilationUnit source) throws IOException { if (source == null) { throw new IllegalArgumentException("source must not be null"); // $NON-NLS-1$ } return emitter.openFor(source); }
private void collect(JarOutputStream jar) throws IOException { assert jar != null; for (VolatileJavaFile file : emitter.getEmitted()) { String path = file.toUri().getPath(); JarEntry entry = new JarEntry(path); jar.putNextEntry(entry); jar.write(file.getCharContent(false).toString().getBytes(CHARSET)); jar.closeEntry(); } }
private void compile(JavaCompiler compiler, JarOutputStream jar) throws IOException { assert compiler != null; assert jar != null; DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); VolatileClassOutputManager fileManager = new VolatileClassOutputManager( compiler.getStandardFileManager(diagnostics, Locale.getDefault(), CHARSET)); try { List<String> arguments = new ArrayList<String>(); Collections.addAll(arguments, "-source", "1.6"); Collections.addAll(arguments, "-target", "1.6"); Collections.addAll(arguments, "-encoding", CHARSET.name()); StringWriter errors = new StringWriter(); PrintWriter pw = new PrintWriter(errors); CompilationTask task = compiler.getTask( pw, fileManager, diagnostics, arguments, Collections.<String>emptyList(), emitter.getEmitted()); Boolean successed = task.call(); pw.close(); for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) { switch (diagnostic.getKind()) { case ERROR: case MANDATORY_WARNING: getEnvironment().error(diagnostic.getMessage(null)); break; case WARNING: LOG.warn(diagnostic.getMessage(null)); break; default: LOG.info(diagnostic.getMessage(null)); break; } } if (successed != Boolean.TRUE) { throw new IOException( MessageFormat.format( "{0}のコンパイルに失敗しました: {1}", getEnvironment().getTargetId(), errors.toString())); } for (VolatileResourceFile file : fileManager.getResources()) { addEntry(jar, file); } for (VolatileClassFile file : fileManager.getCompiled()) { addEntry(jar, file); } for (Map.Entry<String, byte[]> entry : contents.entrySet()) { addEntry(jar, entry.getKey(), entry.getValue()); } } finally { fileManager.close(); } }