Bazel: Add support for JDK 21

There are two failing tests when switching to JDK 21.

One failure is related to the changed behaviour related to the locale
providers. Adapt `GitDateFormatterTest` to changes in unicode [1].

Second failure related to changed behaviour in URL.openConnection(),
see: [2] for more details.

Before JDK 20, some of the parsing/validation performed by the JDK
built-in URLStreamHander implementations were delayed until
URL::openConnection or URLConnection::connect was called. Starting
JDK 20, some of these parsing/validations are now performed early,
i.e. within URL constructors.

IOW, the assumption made in HttpSupport.TesttestMalformedUri() isn't
met any more: providing mailformed URI to the URL ctor now throws an
exception starting with JDK 20. To rectify the problem, remove the
offending test.

Test plan:

To build with JDK 21 and run the tests locally:

  $> bazel test --config=java21 //...

To build with JDK 21 and run the tests on RBE:

  $> bazel test --config=remote21 --remote_instance_name=$PROJECT //...

[1] https://bugs.openjdk.org/browse/JDK-8304925
[2] https://bugs.openjdk.org/browse/JDK-8293590

Change-Id: I796de67f7945d5f1fa5e8146f4ec8cbe9ac7bd3e
diff --git a/.bazelrc b/.bazelrc
index 51be79b..7c71c4a 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -21,7 +21,7 @@
 build:remote11 --tool_java_runtime_version=remotejdk_11
 build:remote11 --config=remote
 
-# Builds using remote_jdk17, executes using remote_jdk11 or local_jdk
+# Builds using remote_jdk17, executes using remote_jdk17 or local_jdk
 build:java17 --java_language_version=17
 build:java17 --java_runtime_version=remotejdk_17
 build:java17 --tool_java_language_version=17
@@ -34,6 +34,19 @@
 build:remote17 --tool_java_runtime_version=remotejdk_17
 build:remote17 --config=remote
 
+# Builds using remote_jdk21, executes using remote_jdk21 or local_jdk
+build:java21 --java_language_version=21
+build:java21 --java_runtime_version=remotejdk_21
+build:java21 --tool_java_language_version=21
+build:java21 --tool_java_runtime_version=remotejdk_21
+
+# Builds and executes on RBE using remotejdk_21
+build:remote21 --java_language_version=21
+build:remote21 --java_runtime_version=remotejdk_21
+build:remote21 --tool_java_language_version=21
+build:remote21 --tool_java_runtime_version=remotejdk_21
+build:remote21 --config=remote
+
 test --build_tests_only
 test --test_output=errors
 test --flaky_test_attempts=3
diff --git a/WORKSPACE b/WORKSPACE
index b0d0ea0..07d4d5c 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -11,6 +11,18 @@
 )
 
 http_archive(
+    name = "rules_java",
+    sha256 = "4da3761f6855ad916568e2bfe86213ba6d2637f56b8360538a7fb6125abf6518",
+    urls = [
+        "https://github.com/bazelbuild/rules_java/releases/download/7.5.0/rules_java-7.5.0.tar.gz",
+    ],
+)
+
+load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
+
+rules_java_dependencies()
+
+http_archive(
     name = "ubuntu2204_jdk17",
     sha256 = "8ea82b81c9707e535ff93ef5349d11e55b2a23c62bcc3b0faaec052144aed87d",
     strip_prefix = "rbe_autoconfig-5.1.0",
@@ -24,6 +36,14 @@
 
 register_toolchains("//tools:error_prone_warnings_toolchain_java17_definition")
 
+register_toolchains("//tools:error_prone_warnings_toolchain_java21_definition")
+
+# Order of registering toolchains matters. rules_java toolchains take precedence
+# over the custom toolchains, so the default jdk21 toolchain gets picked
+# (one without custom package_config). That's why the `rules_java_toolchains()`
+# must be called after the `register_toolchain()` invocation.
+rules_java_toolchains()
+
 JMH_VERS = "1.37"
 
 maven_jar(
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java
index 0bd7e0b..6a531fe 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java
@@ -89,15 +89,19 @@
 	@Test
 	public void LOCALE() {
 		String date = new GitDateFormatter(Format.LOCALE).formatDate(ident);
+		System.out.println(date);
 		assertTrue("Sep 20, 2011 7:09:25 PM -0400".equals(date)
-				|| "Sep 20, 2011, 7:09:25 PM -0400".equals(date)); // JDK-8206961
+				|| "Sep 20, 2011, 7:09:25 PM -0400".equals(date) // JDK-8206961
+				|| "Sep 20, 2011, 7:09:25\u202FPM -0400".equals(date)); // JDK-8304925
 	}
 
 	@Test
 	public void LOCALELOCAL() {
 		String date = new GitDateFormatter(Format.LOCALELOCAL)
 				.formatDate(ident);
+		System.out.println(date);
 		assertTrue("Sep 20, 2011 7:39:25 PM".equals(date)
-				|| "Sep 20, 2011, 7:39:25 PM".equals(date)); // JDK-8206961
+				|| "Sep 20, 2011, 7:39:25 PM".equals(date) // JDK-8206961
+				|| "Sep 20, 2011, 7:39:25\u202FPM".equals(date)); // JDK-8304925
 	}
 }
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java
index cbe4eb2..a3a5697 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java
@@ -47,18 +47,6 @@
 	}
 
 	@Test
-	public void testMalformedUri() throws Exception {
-		// Valid URL, but backslash is not allowed in a URI in the userinfo part
-		// per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 .
-		// Test that conversion to URI to call the ProxySelector does not throw
-		// an exception.
-		Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
-				"http://infor\\c.jones@somehost/somewhere/someproject.git"));
-		assertNotNull(proxy);
-		assertEquals(Proxy.Type.HTTP, proxy.type());
-	}
-
-	@Test
 	public void testCorrectUri() throws Exception {
 		// Backslash escaped as %5C is correct.
 		Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
diff --git a/tools/BUILD b/tools/BUILD
index 2ad281e..c7ec638 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -17,7 +17,7 @@
 default_java_toolchain(
     name = "error_prone_warnings_toolchain_java17",
     configuration = dict(),
-    java_runtime = "@bazel_tools//tools/jdk:remotejdk_17",
+    java_runtime = "@rules_java//toolchains:remotejdk_17",
     package_configuration = [
         ":error_prone",
     ],
@@ -26,6 +26,18 @@
     visibility = ["//visibility:public"],
 )
 
+default_java_toolchain(
+    name = "error_prone_warnings_toolchain_java21",
+    configuration = dict(),
+    java_runtime = "@rules_java//toolchains:remotejdk_21",
+    package_configuration = [
+        ":error_prone",
+    ],
+    source_version = "21",
+    target_version = "21",
+    visibility = ["//visibility:public"],
+)
+
 # Error Prone errors enabled by default; see ../.bazelrc for how this is
 # enabled. This warnings list is originally based on:
 # https://github.com/bazelbuild/BUILD_file_generator/blob/master/tools/bazel_defs/java.bzl