Bazel: Suppress sun.misc.Unsafe warnings in JavaBuilder and Turbine

JDK 24+ introduces strict checks regarding sun.misc.Unsafe, causing both
JavaBuilder and Turbine to log terminal deprecation warnings or crash
during builds due to older protobuf versions.

Conditionally add "--sun-misc-unsafe-memory-access=allow" to both
javabuilder_jvm_opts and turbine_jvm_opts for Java versions 24 and newer
preventing JVM startup errors on Java 21 toolchains while keeping logs
clean on modern JDKs.

See: https://github.com/bazelbuild/rules_java/issues/348
See: https://github.com/protocolbuffers/protobuf/issues/20760

Release-Notes: skip
Change-Id: Ib3d919c1a8f345ecfbcb95966e5e0e2ed7f0731f
diff --git a/tools/BUILD b/tools/BUILD
index 539aad5..31aa465 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -1,5 +1,6 @@
 load(
     "@bazel_tools//tools/jdk:default_java_toolchain.bzl",
+    "DEFAULT_TOOLCHAIN_CONFIGURATION",
     "default_java_toolchain",
 )
 load("@protobuf//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
@@ -22,11 +23,21 @@
     name = "error_prone_warnings_toolchain_java" + VERSION,
     configuration = dict(),
     java_runtime = "@rules_java//toolchains:remotejdk_" + VERSION,
+    # TODO(davido): Remove once protobuf no longer relies on sun.misc.Unsafe
+    # methods that require this JVM flag on JDK 24+.
+    # See: protocolbuffers/protobuf#20760.
+    javabuilder_jvm_opts = ["--sun-misc-unsafe-memory-access=allow"] if int(VERSION) >= 24 else [],
     package_configuration = [
         ":error_prone",
     ],
     source_version = VERSION,
     target_version = VERSION,
+    turbine_jvm_opts = DEFAULT_TOOLCHAIN_CONFIGURATION["jvm_opts"] + ([
+        # TODO(davido): Remove once protobuf no longer relies on sun.misc.Unsafe
+        # methods that require this JVM flag on JDK 24+.
+        # See: protocolbuffers/protobuf#20760.
+        "--sun-misc-unsafe-memory-access=allow",
+    ] if int(VERSION) >= 24 else []),
     visibility = ["//visibility:public"],
 ) for VERSION in [
     "21",