Fix toolchain_java9 on --host_javabase=<jdk9> after 7eb9ea150fb889a93908d96896db77d5658e5005

See #6127

PiperOrigin-RevId: 212600423
diff --git a/tools/jdk/DumpPlatformClassPath.java b/tools/jdk/DumpPlatformClassPath.java
index 8ebb203..2a0a474 100644
--- a/tools/jdk/DumpPlatformClassPath.java
+++ b/tools/jdk/DumpPlatformClassPath.java
@@ -138,7 +138,10 @@
       StandardJavaFileManager fileManager =
           (StandardJavaFileManager) context.get(JavaFileManager.class);
 
-      if (isJdk9OrEarlier()) {
+      int majorVersion = majorVersion();
+      if (majorVersion == 9 && release == 8) {
+        // Work-around: when running on a JDK 9 host_javabase with --release 8, the ct.sym
+        // handling isn't compatible with the FileManager#list code path in the branch below.
         for (Path path : getLocationAsPaths(fileManager)) {
           Files.walkFileTree(
               path,
@@ -190,6 +193,12 @@
           });
     }
 
+    if (!entries.containsKey("java/lang/Object.class")) {
+      throw new AssertionError(
+          "\nCould not find java.lang.Object on bootclasspath; something has gone terribly wrong.\n"
+              + "Please file a bug: https://github.com/bazelbuild/bazel/issues");
+    }
+
     try (OutputStream os = Files.newOutputStream(output);
         BufferedOutputStream bos = new BufferedOutputStream(os, 65536);
         JarOutputStream jos = new JarOutputStream(bos)) {
@@ -247,14 +256,19 @@
     }
   }
 
-  static boolean isJdk9OrEarlier() {
+  static int majorVersion() {
     try {
       Method versionMethod = Runtime.class.getMethod("version");
       Object version = versionMethod.invoke(null);
-      int majorVersion = (int) version.getClass().getMethod("major").invoke(version);
-      return majorVersion <= 9;
+      return (int) version.getClass().getMethod("major").invoke(version);
     } catch (ReflectiveOperationException e) {
-      return true;
+      // Runtime.version() isn't available on JDK 8; continue below
     }
+    int version = (int) Double.parseDouble(System.getProperty("java.class.version"));
+    if (49 <= version && version <= 52) {
+      return version - (49 - 5);
+    }
+    throw new IllegalStateException(
+        "Unknown Java version: " + System.getProperty("java.specification.version"));
   }
 }