diff options
author | cushon <cushon@google.com> | 2018-02-14 14:35:13 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-14 14:37:19 -0800 |
commit | d63d3787828bc4750ed28b7fe65a0f26d366ab71 (patch) | |
tree | a8a350807358cffb2d63d9a853e7c44aadcbfb51 /src | |
parent | 40d0077016dee31865af5246cbd6b2953d6386b3 (diff) |
Use a diagnostic listener, and match diagnostic codes for the reduced classpath heuristic
PiperOrigin-RevId: 185746460
Diffstat (limited to 'src')
3 files changed, 54 insertions, 19 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbine.java b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbine.java index 99c195d55b..242bee83e5 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbine.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbine.java @@ -47,12 +47,14 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.regex.Pattern; import java.util.zip.ZipOutputStream; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; @@ -233,8 +235,7 @@ public class JavacTurbine implements AutoCloseable { } } - if (compileResult == null - || (!compileResult.success() && hasRecognizedError(compileResult.output()))) { + if (compileResult == null || shouldFallBack(compileResult)) { // fall back to transitive classpath actualClasspath = originalClasspath; requestBuilder.setClassPath(actualClasspath); @@ -250,6 +251,9 @@ public class JavacTurbine implements AutoCloseable { turbineOptions, compileResult.files(), transitive.collectTransitiveDependencies()); dependencyModule.emitDependencyInformation(actualClasspath, compileResult.success()); } else { + for (Diagnostic<? extends JavaFileObject> diagnostic : compileResult.diagnostics()) { + out.println(diagnostic.getMessage(Locale.getDefault())); + } out.print(compileResult.output()); } return result; @@ -450,21 +454,33 @@ public class JavacTurbine implements AutoCloseable { return fs; } - private static final Pattern MISSING_PACKAGE = - Pattern.compile("error: package ([\\p{javaJavaIdentifierPart}\\.]+) does not exist"); - /** * The compilation failed with an error that may indicate that the reduced class path was too * aggressive. * * <p>WARNING: keep in sync with ReducedClasspathJavaLibraryBuilder. */ - // TODO(cushon): use a diagnostic listener and match known codes instead - private static boolean hasRecognizedError(String javacOutput) { - return javacOutput.contains("error: cannot access") - || javacOutput.contains("error: cannot find symbol") - || javacOutput.contains("com.sun.tools.javac.code.Symbol$CompletionFailure") - || MISSING_PACKAGE.matcher(javacOutput).find(); + private static boolean shouldFallBack(JavacTurbineCompileResult result) { + if (result.success()) { + return false; + } + for (Diagnostic<? extends JavaFileObject> diagnostic : result.diagnostics()) { + String code = diagnostic.getCode(); + if (code.contains("doesnt.exist") + || code.contains("cant.resolve") + || code.contains("cant.access")) { + return true; + } + // handle -Xdoclint:reference errors, which don't have a diagnostic code + // TODO(cushon): this is locale-dependent + if (diagnostic.getMessage(Locale.getDefault()).contains("error: reference not found")) { + return true; + } + } + if (result.output().contains("com.sun.tools.javac.code.Symbol$CompletionFailure")) { + return true; + } + return false; } @Override diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompileResult.java b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompileResult.java index 23cade9224..b066537b9e 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompileResult.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompileResult.java @@ -15,27 +15,36 @@ package com.google.devtools.build.java.turbine.javac; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.sun.tools.javac.util.Context; -import java.io.StringWriter; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; /** The output from a {@link JavacTurbineCompiler} compilation. */ class JavacTurbineCompileResult { + enum Status { OK, ERROR } private final ImmutableMap<String, byte[]> files; private final Status status; - private final StringWriter sb; + private final String output; + private final ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics; private final Context context; JavacTurbineCompileResult( - ImmutableMap<String, byte[]> files, Status status, StringWriter sb, Context context) { + ImmutableMap<String, byte[]> files, + Status status, + String output, + ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics, + Context context) { this.files = files; this.status = status; - this.sb = sb; + this.output = output; + this.diagnostics = diagnostics; this.context = context; } @@ -46,7 +55,12 @@ class JavacTurbineCompileResult { /** The stderr from the compilation. */ String output() { - return sb.toString(); + return output; + } + + /** The diagnostics from the compilation. */ + ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics() { + return diagnostics; } /** The files produced by the compilation. */ diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompiler.java b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompiler.java index 6db9b88426..58417f1fbf 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompiler.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/turbine/javac/JavacTurbineCompiler.java @@ -45,6 +45,8 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; import javax.annotation.Nullable; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; import javax.tools.StandardLocation; /** Performs a javac-based turbine compilation given a {@link JavacTurbineCompileRequest}. */ @@ -55,6 +57,8 @@ public class JavacTurbineCompiler { Map<String, byte[]> files = new LinkedHashMap<>(); Status status; StringWriter sw = new StringWriter(); + ImmutableList.Builder<Diagnostic<? extends JavaFileObject>> diagnostics = + ImmutableList.builder(); Context context = new Context(); try (PrintWriter pw = new PrintWriter(sw)) { @@ -67,7 +71,7 @@ public class JavacTurbineCompiler { .getTask( pw, fm, - null /*diagnostics*/, + diagnostics::add, request.javacOptions(), ImmutableList.of() /*classes*/, fm.getJavaFileObjectsFromPaths(request.sources()), @@ -107,7 +111,8 @@ public class JavacTurbineCompiler { } } - return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context); + return new JavacTurbineCompileResult( + ImmutableMap.copyOf(files), status, sw.toString(), diagnostics.build(), context); } /** Mask the annotation processor classpath to avoid version skew. */ |