Synchronize rules_java's default_java_toolchain with the Bazel tree

PiperOrigin-RevId: 494764022
Change-Id: I864b3a97f7b808236e7c5901557dd49d937d3d52
diff --git a/toolchains/default_java_toolchain.bzl b/toolchains/default_java_toolchain.bzl
index 417f08b..b752d80 100644
--- a/toolchains/default_java_toolchain.bzl
+++ b/toolchains/default_java_toolchain.bzl
@@ -40,6 +40,9 @@
     # TODO(b/64485048): Disable this option in persistent worker mode only.
     # Disable symlinks resolution cache since symlinks in exec root change
     "-Dsun.io.useCanonCaches=false",
+
+    # Compact strings make JavaBuilder slightly slower.
+    "-XX:-CompactStrings",
 ]
 
 JDK9_JVM_OPTS = BASE_JDK9_JVM_OPTS
@@ -53,8 +56,7 @@
     "-Xep:ReturnValueIgnored:OFF",
 ]
 
-# java_toolchain parameters without specifying javac, java.compiler,
-# jdk.compiler module, and jvm_opts
+# Default java_toolchain parameters
 _BASE_TOOLCHAIN_CONFIGURATION = dict(
     forcibly_disable_header_compilation = False,
     genclass = ["@remote_java_tools//:GenClass"],
@@ -65,6 +67,10 @@
     javac_supports_workers = True,
     jacocorunner = "@remote_java_tools//:jacoco_coverage_runner_filegroup",
     jvm_opts = BASE_JDK9_JVM_OPTS,
+    turbine_jvm_opts = [
+        # Turbine is not a worker and parallel GC is faster for short-lived programs.
+        "-XX:+UseParallelGC",
+    ],
     misc = DEFAULT_JAVACOPTS,
     singlejar = ["//toolchains:singlejar"],
     # Code to enumerate target JVM boot classpath uses host JVM. Because
@@ -75,19 +81,10 @@
     reduced_classpath_incompatible_processors = [
         "dagger.hilt.processor.internal.root.RootProcessor",  # see b/21307381
     ],
+    java_runtime = "//toolchains:remotejdk_17",
 )
 
-DEFAULT_TOOLCHAIN_CONFIGURATION = dict(
-    jvm_opts = [
-        # Compact strings make JavaBuilder slightly slower.
-        "-XX:-CompactStrings",
-    ] + JDK9_JVM_OPTS,
-    turbine_jvm_opts = [
-        # Turbine is not a worker and parallel GC is faster for short-lived programs.
-        "-XX:+UseParallelGC",
-    ],
-    java_runtime = "//toolchains:remote_jdk11",
-)
+DEFAULT_TOOLCHAIN_CONFIGURATION = _BASE_TOOLCHAIN_CONFIGURATION
 
 # The 'vanilla' toolchain is an unsupported alternative to the default.
 #
@@ -105,6 +102,7 @@
 VANILLA_TOOLCHAIN_CONFIGURATION = dict(
     javabuilder = ["@remote_java_tools//:VanillaJavaBuilder"],
     jvm_opts = [],
+    java_runtime = None,
 )
 
 # The new toolchain is using all the pre-built tools, including
@@ -113,41 +111,27 @@
 # same, otherwise the binaries will not work on the execution
 # platform.
 PREBUILT_TOOLCHAIN_CONFIGURATION = dict(
-    jvm_opts = [
-        # Compact strings make JavaBuilder slightly slower.
-        "-XX:-CompactStrings",
-    ] + JDK9_JVM_OPTS,
-    turbine_jvm_opts = [
-        # Turbine is not a worker and parallel GC is faster for short-lived programs.
-        "-XX:+UseParallelGC",
-    ],
     ijar = ["//toolchains:ijar_prebuilt_binary"],
     singlejar = ["//toolchains:prebuilt_singlejar"],
-    java_runtime = "//toolchains:remote_jdk11",
 )
 
 # The new toolchain is using all the tools from sources.
 NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict(
-    jvm_opts = [
-        # Compact strings make JavaBuilder slightly slower.
-        "-XX:-CompactStrings",
-    ] + JDK9_JVM_OPTS,
-    turbine_jvm_opts = [
-        # Turbine is not a worker and parallel GC is faster for short-lived programs.
-        "-XX:+UseParallelGC",
-    ],
     ijar = ["@remote_java_tools//:ijar_cc_binary"],
     singlejar = ["@remote_java_tools//:singlejar_cc_bin"],
-    java_runtime = "//toolchains:remote_jdk11",
 )
 
-def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs):
+def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, exec_compatible_with = [], target_compatible_with = [], **kwargs):
     """Defines a remote java_toolchain with appropriate defaults for Bazel.
 
     Args:
         name: The name of the toolchain
         configuration: Toolchain configuration
         toolchain_definition: Whether to define toolchain target and its config setting
+        exec_compatible_with: A list of constraint values that must be
+            satisifed for the exec platform.
+        target_compatible_with: A list of constraint values that must be
+            satisifed for the target platform.
         **kwargs: More arguments for the java_toolchain target
     """
 
@@ -169,6 +153,8 @@
             toolchain_type = "@bazel_tools//tools/jdk:toolchain_type",
             target_settings = [name + "_version_setting"],
             toolchain = name,
+            exec_compatible_with = exec_compatible_with,
+            target_compatible_with = target_compatible_with,
         )
 
 def java_runtime_files(name, srcs):
@@ -200,8 +186,6 @@
     args.add("-target")
     args.add("8")
     args.add("-Xlint:-options")
-    args.add("-cp")
-    args.add("%s/lib/tools.jar" % host_javabase.java_home)
     args.add("-d")
     args.add_all([class_dir], expand_directories = False)
     args.add(ctx.file.src)
@@ -223,12 +207,7 @@
     args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
     args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED")
     args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED")
-    args.add_joined(
-        "-cp",
-        [class_dir, "%s/lib/tools.jar" % host_javabase.java_home],
-        join_with = ctx.configuration.host_path_separator,
-        expand_directories = False,
-    )
+    args.add("-cp", class_dir.path)
     args.add("DumpPlatformClassPath")
     args.add(bootclasspath)