Serialization Test fixes for JDK 21.

ImmutableMapCodecTest - was using getCanonicalName to retrieve the name
of an anonymous inner class and was getting null, as is the defined
behavior for anonymous inner classes. Switching to getName fixes the test,
which makes it consistent with production code anyway.

MethodCodecTest - this seems like weird JDK21 behavior. Looking up methods
returned by Class.getMethods against the declaring classes does not return
the same method. In this case, in particular, String.class.getMethods includes

  java.lang.String.resolveConstantDesc(java.lang.invoke.MethodHandles$Lookup)
      throws java.lang.ReflectiveOperationException

which is the method signature according to the ConstantDesc interface. Adding
to the strangeness, calling getDeclaringClass returns String instead of
ConstantDesc.

However, looking up the method with String.class.getDeclaredMethod by name and
parameters, results in

  java.lang.String.resolveConstantDesc(java.lang.invoke.MethodHandles$Lookup)

which drops the ReflectiveOperationException. This is actually the proper
signature for strings. This difference shouldn't really matter for
serialization correctness as long as both Method objects are invokable.

Changing the test to serialize the result of looking up via getDeclaredMethod
instead of getMethods fixes it.

PiperOrigin-RevId: 514686339
Change-Id: I1eecae589858fc9296fedee49f810d56720d6ab2
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java
index 26e092c..ff5009c 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ImmutableMapCodecTest.java
@@ -96,7 +96,7 @@
                         .buildOrThrow()));
     assertThat(thrown)
         .hasMessageThat()
-        .startsWith("No default codec available for " + comparator.getClass().getCanonicalName());
+        .startsWith("No default codec available for " + comparator.getClass().getName());
   }
 
   @Test
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/MethodCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/MethodCodecTest.java
index 0874331..1ff33fd 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/MethodCodecTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/MethodCodecTest.java
@@ -27,11 +27,17 @@
 public class MethodCodecTest {
   @Test
   public void smoke() throws Exception {
-    new SerializationTester(
-            ImmutableList.<Method>builder()
-                .add(String.class.getMethods())
-                .add(List.class.getMethods())
-                .build())
-        .runTests();
+    var methods = new ImmutableList.Builder<Method>();
+    getMethods(String.class, methods);
+    getMethods(List.class, methods);
+    new SerializationTester(methods.build()).runTests();
+  }
+
+  private static void getMethods(Class<?> clazz, ImmutableList.Builder<Method> out)
+      throws NoSuchMethodException {
+    for (var method : clazz.getMethods()) {
+      Class<?> declaringClass = method.getDeclaringClass();
+      out.add(declaringClass.getDeclaredMethod(method.getName(), method.getParameterTypes()));
+    }
   }
 }