Use `ClassToInstanceMap` for serialization dependencies.

Serialization deps are mappings from a class to an instance of that class. Using `ClassToInstanceMap` provides compile-time enforcement that the value is in fact an instance of the key.

Also use `checkNotNull` in `getDependency` to throw when a dependency is not present, so that callers don't have to check.

PiperOrigin-RevId: 369249742
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java b/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
index 4af015d..80c62c7 100644
--- a/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
+++ b/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
@@ -19,12 +19,13 @@
 import static com.google.devtools.build.lib.vfs.PathFragment.create;
 import static org.junit.Assert.assertThrows;
 
+import com.google.common.collect.ImmutableClassToInstanceMap;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.testing.EqualsTester;
 import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
+import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
 import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
 import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
 import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
@@ -295,7 +296,7 @@
   }
 
   @Test
-  public void testBasename() throws Exception {
+  public void testBasename() {
     assertThat(create("foo/bar").getBaseName()).isEqualTo("bar");
     assertThat(create("/foo/bar").getBaseName()).isEqualTo("bar");
     assertThat(create("foo/").getBaseName()).isEqualTo("foo");
@@ -307,7 +308,7 @@
   }
 
   @Test
-  public void testFileExtension() throws Exception {
+  public void testFileExtension() {
     assertThat(create("foo.bar").getFileExtension()).isEqualTo("bar");
     assertThat(create("foo.barr").getFileExtension()).isEqualTo("barr");
     assertThat(create("foo.b").getFileExtension()).isEqualTo("b");
@@ -321,7 +322,7 @@
   }
 
   @Test
-  public void testReplaceName() throws Exception {
+  public void testReplaceName() {
     assertThat(create("foo/bar").replaceName("baz").getPathString()).isEqualTo("foo/baz");
     assertThat(create("/foo/bar").replaceName("baz").getPathString()).isEqualTo("/foo/baz");
     assertThat(create("foo/bar").replaceName("").getPathString()).isEqualTo("foo");
@@ -341,8 +342,9 @@
     assertThat(create("foo/bar").replaceName("/absolute").getPathString()).isEqualTo("/absolute");
     assertThat(create("foo/bar").replaceName("/").getPathString()).isEqualTo("/");
   }
+
   @Test
-  public void testSubFragment() throws Exception {
+  public void testSubFragment() {
     assertThat(create("/foo/bar/baz").subFragment(0, 3).getPathString()).isEqualTo("/foo/bar/baz");
     assertThat(create("foo/bar/baz").subFragment(0, 3).getPathString()).isEqualTo("foo/bar/baz");
     assertThat(create("/foo/bar/baz").subFragment(0, 2).getPathString()).isEqualTo("/foo/bar");
@@ -403,7 +405,7 @@
     PathFragment.checkAllPathsAreUnder(toPathsSet("a/b", "a/c"), create("a"));
 
     // Check trivially passes:
-    PathFragment.checkAllPathsAreUnder(ImmutableList.<PathFragment>of(), create("a"));
+    PathFragment.checkAllPathsAreUnder(ImmutableList.of(), create("a"));
 
     // Check fails when some path does not start with startingWithPath:
     assertThrows(
@@ -467,7 +469,7 @@
     assertThrows(IllegalArgumentException.class, () -> create("foo").getDriveStr());
   }
 
-  static List<PathFragment> toPaths(List<String> strs) {
+  private static List<PathFragment> toPaths(List<String> strs) {
     List<PathFragment> paths = Lists.newArrayList();
     for (String s : strs) {
       paths.add(create(s));
@@ -475,7 +477,7 @@
     return paths;
   }
 
-  static ImmutableSet<PathFragment> toPathsSet(String... strs) {
+  private static ImmutableSet<PathFragment> toPathsSet(String... strs) {
     ImmutableSet.Builder<PathFragment> builder = ImmutableSet.builder();
     for (String str : strs) {
       builder.add(create(str));
@@ -484,7 +486,7 @@
   }
 
   @Test
-  public void testCompareTo() throws Exception {
+  public void testCompareTo() {
     List<String> pathStrs =
         ImmutableList.of(
             "",
@@ -619,13 +621,16 @@
     checkSerialization("foo/bar/baz", 16);
   }
 
-  private void checkSerialization(String pathFragmentString, int expectedSize) throws Exception {
+  private static void checkSerialization(String pathFragmentString, int expectedSize)
+      throws Exception {
     PathFragment a = create(pathFragmentString);
-    ByteString sa = TestUtils.toBytes(a, ImmutableMap.of());
+    ByteString sa =
+        TestUtils.toBytes(new SerializationContext(ImmutableClassToInstanceMap.of()), a);
     assertThat(sa.size()).isEqualTo(expectedSize);
 
     PathFragment a2 =
-        (PathFragment) TestUtils.fromBytes(new DeserializationContext(ImmutableMap.of()), sa);
+        (PathFragment)
+            TestUtils.fromBytes(new DeserializationContext(ImmutableClassToInstanceMap.of()), sa);
     assertThat(a2).isEqualTo(a);
   }