use of javax.tools.DiagnosticListener in project lombok by rzwitserloot.
the class TestClassFileMetaData method compile.
static byte[] compile(File file) {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
File tempDir = getTempDir();
tempDir.mkdirs();
List<String> options = Arrays.asList("-proc:none", "-d", tempDir.getAbsolutePath());
StringWriter captureWarnings = new StringWriter();
final StringBuilder compilerErrors = new StringBuilder();
DiagnosticListener<JavaFileObject> diagnostics = new DiagnosticListener<JavaFileObject>() {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
compilerErrors.append(diagnostic.toString()).append("\n");
}
};
CompilationTask task = compiler.getTask(captureWarnings, null, diagnostics, options, null, Collections.singleton(new ContentBasedJavaFileObject(file.getPath(), readFileAsString(file))));
Boolean taskResult = task.call();
assertTrue("Compilation task didn't succeed: \n<Warnings and Errors>\n" + compilerErrors.toString() + "\n</Warnings and Errors>", taskResult);
return PostCompilerApp.readFile(new File(tempDir, file.getName().replaceAll("\\.java$", ".class")));
} catch (Exception e) {
throw Lombok.sneakyThrow(e);
}
}
use of javax.tools.DiagnosticListener in project bazel by bazelbuild.
the class BazelJavaCompiler method newInstance.
private static JavaCompiler newInstance(final JavaCompiler delegate) {
// We forward most operations to the JavaCompiler implementation in langtools.jar.
return new JavaCompiler() {
@Override
public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
// We prepend bazel's default javacopts to user javacopts,
// so that the user can override them. javac supports this
// "last option wins" style of option override.
ImmutableList.Builder<String> fullOptions = ImmutableList.builder();
fullOptions.addAll(getDefaultJavacopts());
if (options != null) {
fullOptions.addAll(options);
}
return delegate.getTask(out, fileManager, diagnosticListener, fullOptions.build(), classes, compilationUnits);
}
@Override
public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
StandardJavaFileManager fileManager = delegate.getStandardFileManager(diagnosticListener, locale, charset);
try {
fileManager.setLocation(// bootclasspath
StandardLocation.PLATFORM_CLASS_PATH, JavacBootclasspath.asFiles());
} catch (IOException e) {
// Should never happen, according to javadocs for setLocation
throw new RuntimeException(e);
}
return fileManager;
}
@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
// prepend bazel's default javacopts to user arguments
List<String> args = ImmutableList.<String>builder().addAll(getDefaultJavacopts()).add(arguments).build();
return delegate.run(in, out, err, args.toArray(new String[0]));
}
@Override
public Set<SourceVersion> getSourceVersions() {
return delegate.getSourceVersions();
}
@Override
public int isSupportedOption(String option) {
return delegate.isSupportedOption(option);
}
};
}
use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.
the class JavapFileManager method create.
public static JavapFileManager create(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
Context javac_context = new Context();
if (dl != null)
javac_context.put(DiagnosticListener.class, dl);
javac_context.put(com.sun.tools.javac.util.Log.outKey, log);
return new JavapFileManager(javac_context, null);
}
use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.
the class JavahTask method getDiagnosticListenerForWriter.
private DiagnosticListener<JavaFileObject> getDiagnosticListenerForWriter(Writer w) {
final PrintWriter pw = getPrintWriterForWriter(w);
return new DiagnosticListener<JavaFileObject>() {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
pw.print(getMessage("err.prefix"));
pw.print(" ");
}
pw.println(diagnostic.getMessage(null));
}
};
}
use of javax.tools.DiagnosticListener in project ceylon-compiler by ceylon.
the class CeyloncTool method getTask.
public CeyloncTaskImpl getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
// final String kindMsg = "All compilation units must be of SOURCE kind";
if (options != null)
for (String option : options) // null check
option.getClass();
if (classes != null) {
for (String cls : classes) if (// implicit null check
!SourceVersion.isName(cls) && // FIX for ceylon because default is not a valid name for Java
!"default".equals(cls))
throw new IllegalArgumentException("Not a valid class name: " + cls);
}
if (fileManager == null)
fileManager = getStandardFileManager(out, diagnosticListener, null, null);
Context context = ((CeyloncFileManager) fileManager).getContext();
if (diagnosticListener != null && context.get(DiagnosticListener.class) == null)
context.put(DiagnosticListener.class, diagnosticListener);
context.put(JavaFileManager.class, fileManager);
JavacTool.processOptions(context, fileManager, options);
Main compiler = new Main("ceyloncTask", context.get(Log.outKey));
return new CeyloncTaskImpl(compiler, options, context, classes, compilationUnits);
}
Aggregations