diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD index 4816cd8216359a..a42b09e0a9e98b 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/BUILD @@ -12,7 +12,7 @@ load(":build_defs.bzl", "default_javacopts") # Exports the canonical javac bootclasspath artifact locations genrule( name = "javac-bootclasspath-locations", - srcs = ["@bazel_tools//tools/jdk:bootclasspath"], + srcs = ["//tools/jdk:platformclasspath8"], outs = ["JavacBootclasspathLocations.java"], cmd = "$(location javac-bootclasspath-locations.sh) $@ $(GENDIR) $(SRCS)", tools = ["javac-bootclasspath-locations.sh"], @@ -25,7 +25,7 @@ java_library( "JavacBootclasspath.java", ":javac-bootclasspath-locations", ], - data = ["@bazel_tools//tools/jdk:bootclasspath"], + data = ["//tools/jdk:platformclasspath8"], visibility = ["//visibility:public"], deps = ["//third_party:guava"], ) diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/javac-bootclasspath-locations.sh b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/javac-bootclasspath-locations.sh index d4bad63c756c44..16c69dcfdc014a 100755 --- a/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/javac-bootclasspath-locations.sh +++ b/src/java_tools/buildjar/java/com/google/devtools/build/java/bazel/javac-bootclasspath-locations.sh @@ -31,6 +31,7 @@ BOOTCLASSPATH=$(echo "$*" | \ sed "s|^${GENDIR}/||" | \ sed "s|^.*local_jdk|local_jdk|" | \ sed "s|^third_party|${PWD##*/}/third_party|" | \ + sed "s|^tools/jdk|${PWD##*/}/tools/jdk|" | \ tr "\n" ":" | \ sed "s/:$//" ) diff --git a/src/main/java/com/google/devtools/build/lib/remote/Retrier.java b/src/main/java/com/google/devtools/build/lib/remote/Retrier.java index 68b8e8a17648d6..075ddaebeea233 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/Retrier.java +++ b/src/main/java/com/google/devtools/build/lib/remote/Retrier.java @@ -345,7 +345,11 @@ private void onExecuteAsyncFailure( try { ListenableScheduledFuture sf = retryService.schedule( - () -> executeAsync(call, outerF, backoff), waitMillis, TimeUnit.MILLISECONDS); + () -> { + executeAsync(call, outerF, backoff); + }, + waitMillis, + TimeUnit.MILLISECONDS); Futures.addCallback( sf, new FutureCallback() { diff --git a/src/main/java/com/google/devtools/build/skydoc/BUILD b/src/main/java/com/google/devtools/build/skydoc/BUILD index d5b485fe7ea512..86d11e799f2e01 100644 --- a/src/main/java/com/google/devtools/build/skydoc/BUILD +++ b/src/main/java/com/google/devtools/build/skydoc/BUILD @@ -30,6 +30,15 @@ filegroup( java_binary( name = "skydoc", + jvm_flags = [ + # quiet warnings from com.google.protobuf.UnsafeUtil, + # see: https://github.com/google/protobuf/issues/3781 + # and: https://github.com/bazelbuild/bazel/issues/5599 + "--add-opens=java.base/java.nio=ALL-UNNAMED", + "--add-opens=java.base/java.lang=ALL-UNNAMED", + # ... but only on JDK >= 9 + "-XX:+IgnoreUnrecognizedVMOptions", + ], main_class = "com.google.devtools.build.skydoc.SkydocMain", visibility = ["//visibility:public"], runtime_deps = [ diff --git a/src/main/java/com/google/devtools/skylark/BUILD b/src/main/java/com/google/devtools/skylark/BUILD index 6a05f40d181f0a..006f9aa2099d99 100644 --- a/src/main/java/com/google/devtools/skylark/BUILD +++ b/src/main/java/com/google/devtools/skylark/BUILD @@ -6,6 +6,15 @@ package(default_visibility = ["//src:__subpackages__"]) java_binary( name = "Skylark", srcs = ["Skylark.java"], + jvm_flags = [ + # quiet warnings from com.google.protobuf.UnsafeUtil, + # see: https://github.com/google/protobuf/issues/3781 + # and: https://github.com/bazelbuild/bazel/issues/5599 + "--add-opens=java.base/java.nio=ALL-UNNAMED", + "--add-opens=java.base/java.lang=ALL-UNNAMED", + # ... but only on JDK >= 9 + "-XX:+IgnoreUnrecognizedVMOptions", + ], main_class = "com.google.devtools.skylark.Skylark", deps = [ "//src/main/java/com/google/devtools/build/lib:events", diff --git a/src/test/java/com/google/devtools/build/android/desugar/BUILD b/src/test/java/com/google/devtools/build/android/desugar/BUILD index 8d6b50ddd4bd2e..c97af3440c7a5d 100644 --- a/src/test/java/com/google/devtools/build/android/desugar/BUILD +++ b/src/test/java/com/google/devtools/build/android/desugar/BUILD @@ -316,8 +316,12 @@ java_test( srcs = [ "DesugarCoreLibraryFunctionalTest.java", ], - # TODO (b/72181101): -Xbootclasspath/p is removed in JDK 9. - jvm_flags = ["-Xbootclasspath/p:$(location :testdata_desugared_core_library):$(location //third_party/java/jacoco:blaze-agent)"], + jvm_flags = [ + # TODO (b/72181101): -Xbootclasspath/p is removed in JDK 9. + "-XX:+IgnoreUnrecognizedVMOptions", + "-Xbootclasspath/a:$(location :testdata_desugared_core_library):$(location //third_party/java/jacoco:blaze-agent)", + "--patch-module=java.base=$(location :testdata_desugared_core_library)", + ], tags = ["no_windows"], deps = [ ":testdata_desugared_core_library", # Make tests run against desugared library diff --git a/src/test/java/com/google/devtools/build/lib/blackbox/bazel/BUILD b/src/test/java/com/google/devtools/build/lib/blackbox/bazel/BUILD index 36a1294d9d1bf8..9bc0a978ccfa86 100644 --- a/src/test/java/com/google/devtools/build/lib/blackbox/bazel/BUILD +++ b/src/test/java/com/google/devtools/build/lib/blackbox/bazel/BUILD @@ -80,8 +80,7 @@ java_library( "//src/java_tools/junitrunner/java/com/google/testing/junit/runner:Runner_deploy.jar", "//third_party/ijar", "//third_party/java/jdk/langtools:test-srcs", - "@local_jdk//:bootclasspath", - "@local_jdk//:jdk", + "@bazel_tools//tools/jdk:current_java_runtime", ] + select({ # TODO(bazel-team): Once https://github.com/bazelbuild/bazel/issues/2241 # is resolved, use cc implementation of singlejar on windows diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index 0bf98856934c9d..323f4d54d46c6d 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -41,8 +41,7 @@ filegroup( "//third_party/java/jdk/langtools:test-srcs", "//tools:srcs", "@bazel_skylib//:test_deps", - "@local_jdk//:bootclasspath", - "@local_jdk//:jdk", + "@bazel_tools//tools/jdk:current_java_runtime", ] + select({ # TODO(bazel-team): Once https://github.com/bazelbuild/bazel/issues/2241 # is resolved, use cc implementation of singlejar on windows diff --git a/third_party/ijar/test/IjarTests.java b/third_party/ijar/test/IjarTests.java index 6be993290fd7f7..87fc0b3145fc27 100644 --- a/third_party/ijar/test/IjarTests.java +++ b/third_party/ijar/test/IjarTests.java @@ -15,14 +15,12 @@ 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 org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteStreams; -import com.google.devtools.build.java.bazel.BazelJavaCompiler; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -52,6 +50,7 @@ import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; +import javax.tools.ToolProvider; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -59,9 +58,7 @@ import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Opcodes; -/** - * JUnit tests for ijar tool. - */ +/** JUnit tests for ijar tool. */ @RunWith(JUnit4.class) public class IjarTests { @@ -80,27 +77,26 @@ private static File getTmpDir() { DiagnosticCollector diagnostics; private JavaCompiler.CompilationTask makeCompilationTask(String... files) throws IOException { - JavaCompiler compiler = BazelJavaCompiler.newInstance(); + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - fileManager.setLocation(StandardLocation.CLASS_PATH, - Arrays.asList(new File("third_party/ijar/test/interface_ijar_testlib.jar"))); - fileManager.setLocation(StandardLocation.CLASS_OUTPUT, - Arrays.asList(getTmpDir())); + fileManager.setLocation( + StandardLocation.CLASS_PATH, + Arrays.asList(new File("third_party/ijar/test/interface_ijar_testlib.jar"))); + fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(getTmpDir())); diagnostics = new DiagnosticCollector(); - return compiler.getTask(null, - fileManager, - diagnostics, - Arrays.asList("-Xlint:deprecation"), // used for deprecation tests - null, - fileManager.getJavaFileObjects(files)); + return compiler.getTask( + null, + fileManager, + diagnostics, + Arrays.asList("-Xlint:deprecation"), // used for deprecation tests + null, + fileManager.getJavaFileObjects(files)); } /** - * Test that the ijar tool preserves private nested classes as they - * may be exposed through public API. This test relies on an - * interface jar provided through the build rule - * :interface_ijar_testlib and the Java source file - * PrivateNestedClass.java. + * Test that the ijar tool preserves private nested classes as they may be exposed through public + * API. This test relies on an interface jar provided through the build rule + * :interface_ijar_testlib and the Java source file PrivateNestedClass.java. */ @Test public void testPrivateNestedClass() throws IOException { @@ -109,21 +105,16 @@ public void testPrivateNestedClass() throws IOException { } } - /** - * Test that the ijar tool preserves annotations, especially @Target - * meta-annotation. - */ + /** Test that the ijar tool preserves annotations, especially @Target meta-annotation. */ @Test public void testRestrictedAnnotations() throws IOException { assertFalse(makeCompilationTask("third_party/ijar/test/UseRestrictedAnnotation.java").call()); } /** - * Test that the ijar tool preserves private nested classes as they - * may be exposed through public API. This test relies on an - * interface jar provided through the build rule - * :interface_ijar_testlib and the Java source file - * PrivateNestedClass.java. + * Test that the ijar tool preserves private nested classes as they may be exposed through public + * API. This test relies on an interface jar provided through the build rule + * :interface_ijar_testlib and the Java source file PrivateNestedClass.java. */ @Test public void testDeprecatedParts() throws IOException { @@ -132,36 +123,42 @@ public void testDeprecatedParts() throws IOException { } int deprecatedWarningCount = 0; for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { - if ((diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING) && + if ((diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING) + && // Java 6: - (diagnostic.getMessage(Locale.ENGLISH).startsWith("[deprecation]") || - // Java 7: - diagnostic.getMessage(Locale.ENGLISH).contains("has been deprecated"))) { + (diagnostic.getMessage(Locale.ENGLISH).startsWith("[deprecation]") + || + // Java 7: + diagnostic.getMessage(Locale.ENGLISH).contains("has been deprecated"))) { deprecatedWarningCount++; } } - assertEquals(16, deprecatedWarningCount); + assertThat(deprecatedWarningCount).isAtLeast(16); } /** - * Test that the ijar tool preserves EnclosingMethod attributes and doesn't - * prevent annotation processors from accessing all the elements in a package. + * Test that the ijar tool preserves EnclosingMethod attributes and doesn't prevent annotation + * processors from accessing all the elements in a package. */ @Test public void testEnclosingMethod() throws IOException { - JavaCompiler.CompilationTask task = makeCompilationTask("third_party/ijar/test/package-info.java"); - task.setProcessors(Arrays.asList(new AbstractProcessor() { - @Override - public Set getSupportedAnnotationTypes() { - return Collections.singleton("*"); - } + JavaCompiler.CompilationTask task = + makeCompilationTask("third_party/ijar/test/package-info.java"); + task.setProcessors( + Arrays.asList( + new AbstractProcessor() { + @Override + public Set getSupportedAnnotationTypes() { + return Collections.singleton("*"); + } - @Override - public boolean process(Set annotations, RoundEnvironment roundEnv) { - roundEnv.getElementsAnnotatedWith(java.lang.Override.class); - return true; - } - })); + @Override + public boolean process( + Set annotations, RoundEnvironment roundEnv) { + roundEnv.getElementsAnnotatedWith(java.lang.Override.class); + return true; + } + })); if (!task.call()) { fail(getFailedCompilationMessage()); } @@ -196,12 +193,13 @@ private String getFailedCompilationMessage() { StringBuilder builder = new StringBuilder(); builder.append("Build failed unexpectedly"); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { - builder.append(String.format( - "\t%s line %d column %d: %s", - diagnostic.getKind().toString(), - diagnostic.getLineNumber(), - diagnostic.getColumnNumber(), - diagnostic.getMessage(Locale.ENGLISH))); + builder.append( + String.format( + "\t%s line %d column %d: %s", + diagnostic.getKind().toString(), + diagnostic.getLineNumber(), + diagnostic.getColumnNumber(), + diagnostic.getMessage(Locale.ENGLISH))); } return builder.toString(); } @@ -406,8 +404,7 @@ private static void assertNonManifestFilesBitIdentical(JarFile original, JarFile throws IOException { // Make sure that all other files came across bitwise equal for (String classEntry : - original - .stream() + original.stream() .map(JarEntry::getName) .filter(name -> !name.equals("META-INF/MANIFEST.MF")) .collect(toImmutableList())) { diff --git a/third_party/ijar/test/ijar_test.sh b/third_party/ijar/test/ijar_test.sh index d7a533625fa7f5..703ac90e448e83 100755 --- a/third_party/ijar/test/ijar_test.sh +++ b/third_party/ijar/test/ijar_test.sh @@ -350,7 +350,7 @@ function test_type_annotation() { $JAVAP -classpath $TYPEANN2_IJAR -v Util >& $TEST_log || fail "javap failed" expect_log "RuntimeVisibleTypeAnnotations" "RuntimeVisibleTypeAnnotations not preserved!" cp $TYPEANN2_JAVA $TEST_TMPDIR/TypeAnnotationTest2.java - $JAVAC -J-Xbootclasspath/p:$LANGTOOLS8 $TEST_TMPDIR/TypeAnnotationTest2.java -cp $TYPEANN2_IJAR || + $JAVAC $TEST_TMPDIR/TypeAnnotationTest2.java -cp $TYPEANN2_IJAR || fail "javac failed" } @@ -367,7 +367,7 @@ function test_object_class() { # Check that Object.class can be processed mkdir -p $TEST_TMPDIR/java/lang cp $OBJECT_JAVA $TEST_TMPDIR/java/lang/. - $JAVAC $TEST_TMPDIR/java/lang/Object.java || fail "javac failed" + $JAVAC -source 8 -target 8 $TEST_TMPDIR/java/lang/Object.java || fail "javac failed" $JAR cf $OBJECT_JAR -C $TEST_TMPDIR java/lang/Object.class || fail "jar failed" $IJAR $OBJECT_JAR $OBJECT_IJAR || fail "ijar failed" diff --git a/tools/jdk/DumpPlatformClassPath.java b/tools/jdk/DumpPlatformClassPath.java index dd33bba6e3f9ae..0ec566fa4ec119 100644 --- a/tools/jdk/DumpPlatformClassPath.java +++ b/tools/jdk/DumpPlatformClassPath.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UncheckedIOException; +import java.lang.reflect.Method; import java.net.URI; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; @@ -41,8 +42,10 @@ import java.util.zip.CRC32; import java.util.zip.ZipEntry; import javax.tools.JavaFileManager; +import javax.tools.JavaFileManager.Location; import javax.tools.JavaFileObject; import javax.tools.JavaFileObject.Kind; +import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; /** @@ -113,18 +116,39 @@ public static void main(String[] args) throws Exception { /* classes = */ null, /* compilationUnits = */ null, context); - JavaFileManager fileManager = context.get(JavaFileManager.class); - - for (JavaFileObject fileObject : - fileManager.list( - StandardLocation.PLATFORM_CLASS_PATH, - "", - EnumSet.of(Kind.CLASS), - /* recurse= */ true)) { - String binaryName = - fileManager.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fileObject); - entries.put( - binaryName.replace('.', '/') + ".class", toByteArray(fileObject.openInputStream())); + StandardJavaFileManager fileManager = + (StandardJavaFileManager) context.get(JavaFileManager.class); + + if (isJdk9OrEarlier()) { + for (Path path : getLocationAsPaths(fileManager)) { + Files.walkFileTree( + path, + new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { + if (file.getFileName().toString().endsWith(".sig")) { + String outputPath = path.relativize(file).toString(); + outputPath = + outputPath.substring(0, outputPath.length() - ".sig".length()) + ".class"; + entries.put(outputPath, Files.readAllBytes(file)); + } + return FileVisitResult.CONTINUE; + } + }); + } + } else { + for (JavaFileObject fileObject : + fileManager.list( + StandardLocation.PLATFORM_CLASS_PATH, + "", + EnumSet.of(Kind.CLASS), + /* recurse= */ true)) { + String binaryName = + fileManager.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fileObject); + entries.put( + binaryName.replace('.', '/') + ".class", toByteArray(fileObject.openInputStream())); + } } // Include the jdk.unsupported module for compatibility with JDK 8. @@ -191,4 +215,27 @@ private static byte[] toByteArray(InputStream is) throws IOException { } return boas.toByteArray(); } + + @SuppressWarnings("unchecked") + private static Iterable getLocationAsPaths(StandardJavaFileManager fileManager) { + try { + return (Iterable) + StandardJavaFileManager.class + .getMethod("getLocationAsPaths", Location.class) + .invoke(fileManager, StandardLocation.PLATFORM_CLASS_PATH); + } catch (ReflectiveOperationException e) { + throw new LinkageError(e.getMessage(), e); + } + } + + static boolean isJdk9OrEarlier() { + try { + Method versionMethod = Runtime.class.getMethod("version"); + Object version = versionMethod.invoke(null); + int majorVersion = (int) version.getClass().getMethod("major").invoke(version); + return majorVersion <= 9; + } catch (ReflectiveOperationException e) { + return true; + } + } }