diff options
author | cushon <cushon@google.com> | 2018-04-18 14:56:25 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-04-18 14:58:10 -0700 |
commit | cc4196166234ecd3ac376a6f9b52b226462f0d0a (patch) | |
tree | 1e52874594838dcabcae76420e8055bfa2db70f2 /src/java_tools/buildjar | |
parent | 8cbb372e65b0e6a5a3ced23abdef11df3dce294b (diff) |
Fix a crash in javac-turbine transitive classpath fallback
This code was failing to reset the SJD plugin before falling back, which
caused a reference to a JimFS Path in the original filesystem to leak.
PiperOrigin-RevId: 193421565
Diffstat (limited to 'src/java_tools/buildjar')
2 files changed, 111 insertions, 0 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 c71fcd9bc6..1cc9391ca3 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 @@ -154,6 +154,8 @@ public class JavacTurbine implements AutoCloseable { if (compileResult == null || shouldFallBack(compileResult)) { // fall back to transitive classpath actualClasspath = originalClasspath; + // reset SJD plugin + requestBuilder.setStrictDepsPlugin(new StrictJavaDepsPlugin(dependencyModule)); requestBuilder.setClassPath(actualClasspath); compileResult = JavacTurbineCompiler.compile(requestBuilder.build()); if (compileResult.success()) { diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java index ac285d04ca..b7b8250f17 100644 --- a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java @@ -14,6 +14,7 @@ package com.google.devtools.build.java.turbine.javac; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toSet; @@ -277,6 +278,114 @@ public class JavacTurbineTest extends AbstractJavacTurbineCompilationTest { } } + /** + * A sample annotation processor for testing. + * + * <p>Writes an output file containing a SJD violation. + */ + @SupportedAnnotationTypes("MyAnnotation") + public static class SjdProcessor extends AbstractProcessor { + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latest(); + } + + boolean first = true; + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + if (!first) { + return false; + } + if (roundEnv.getRootElements().isEmpty()) { + return false; + } + first = false; + Element element = roundEnv.getRootElements().iterator().next(); + try { + JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile("Generated", element); + try (OutputStream os = sourceFile.openOutputStream()) { + os.write( + ("public class Generated {\n" + + " public static final int CONST = A.CONST;" + + " public static B b;" + + "}") + .getBytes(UTF_8)); + } + } catch (IOException e) { + throw new IOError(e); + } + return false; + } + } + + @Test + public void processingStrict() throws Exception { + + Path libD = temp.newFile("libd.jar").toPath(); + compileLib( + libD, + Collections.<Path>emptyList(), + ImmutableList.of( + new StringJavaFileObject("D.java", "public class D { static final int CONST = 42; }"))); + + Path libC = temp.newFile("libc.jar").toPath(); + compileLib( + libC, + Collections.singleton(libD), + ImmutableList.of(new StringJavaFileObject("C.java", "class C extends D {}"))); + + Path libB = temp.newFile("libb.jar").toPath(); + compileLib( + libB, + ImmutableList.of(libC, libD), + ImmutableList.of(new StringJavaFileObject("B.java", "class B extends C {}"))); + + Path libA = temp.newFile("liba.jar").toPath(); + compileLib( + libA, + ImmutableList.of(libB, libC, libD), + ImmutableList.of(new StringJavaFileObject("A.java", "class A extends B {}"))); + Path depsA = + writedeps( + "liba.jdeps", + Deps.Dependencies.newBuilder() + .setSuccess(true) + .setRuleLabel("//lib:a") + .addDependency( + Deps.Dependency.newBuilder() + .setPath(libB.toString()) + .setKind(Deps.Dependency.Kind.EXPLICIT)) + .build()); + + addSourceLines( + "MyAnnotation.java", // + "public @interface MyAnnotation {}"); + addSourceLines( + "Hello.java", // + "@MyAnnotation", + "class Hello {}"); + + optionsBuilder.addSources(sources.stream().map(p -> p.toString()).collect(toImmutableList())); + optionsBuilder.addProcessors(ImmutableList.of(SjdProcessor.class.getName())); + optionsBuilder.addProcessorPathEntries(HOST_CLASSPATH); + optionsBuilder.addClassPathEntries( + ImmutableList.of(libA.toString(), libB.toString(), libC.toString(), libD.toString())); + optionsBuilder.addAllDepsArtifacts(ImmutableList.of(depsA.toString())); + optionsBuilder.addDirectJars(ImmutableList.of(libA.toString())); + optionsBuilder.setTargetLabel("//my:target"); + + StringWriter errOutput = new StringWriter(); + Result result; + try (JavacTurbine turbine = + new JavacTurbine(new PrintWriter(errOutput, true), optionsBuilder.build())) { + result = turbine.compile(); + } + assertThat(errOutput.toString()).isEmpty(); + assertThat(result).isEqualTo(Result.OK_WITH_FULL_CLASSPATH); + } + static Map<String, byte[]> collectFiles(Path jar) throws IOException { Map<String, byte[]> files = new LinkedHashMap<>(); try (JarFile jf = new JarFile(jar.toFile())) { |