@AutoCodec AspectValue$AspectKey.

Also make AspectKey#toString a little more informative.

PiperOrigin-RevId: 185600069
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/AspectValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/AspectValue.java
index cf12d62..0186315 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/AspectValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/AspectValue.java
@@ -35,16 +35,10 @@
 import com.google.devtools.build.lib.packages.Package;
 import com.google.devtools.build.lib.skyframe.BuildConfigurationValue.Key;
 import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey.KeyAndHost;
-import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.ImmutableListCodec;
 import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
 import com.google.devtools.build.lib.syntax.SkylarkImport;
 import com.google.devtools.build.skyframe.SkyFunctionName;
-import com.google.protobuf.CodedInputStream;
-import com.google.protobuf.CodedOutputStream;
-import java.io.IOException;
 import java.util.List;
 import javax.annotation.Nullable;
 
@@ -61,8 +55,9 @@
   }
 
   /** A base class for a key representing an aspect applied to a particular target. */
+  @AutoCodec
   public static class AspectKey extends AspectValueKey {
-    public static final ObjectCodec<AspectKey> CODEC = new AspectKeyCodec();
+    public static final ObjectCodec<AspectKey> CODEC = new AspectValue_AspectKey_AutoCodec();
     private final Label label;
     private final ImmutableList<AspectKey> baseKeys;
     private final BuildConfigurationValue.Key aspectConfigurationKey;
@@ -83,6 +78,31 @@
       this.aspectDescriptor = aspectDescriptor;
     }
 
+    @AutoCodec.VisibleForSerialization
+    @AutoCodec.Instantiator
+    static AspectKey createAspectKey(
+        Label label,
+        ConfiguredTargetKey baseConfiguredTargetKey,
+        ImmutableList<AspectKey> baseKeys,
+        AspectDescriptor aspectDescriptor,
+        BuildConfigurationValue.Key aspectConfigurationKey,
+        boolean aspectConfigurationIsHost) {
+      return aspectKeyInterner.intern(
+          aspectConfigurationIsHost
+              ? new HostAspectKey(
+                  label,
+                  aspectConfigurationKey,
+                  baseConfiguredTargetKey,
+                  baseKeys,
+                  aspectDescriptor)
+              : new AspectKey(
+                  label,
+                  aspectConfigurationKey,
+                  baseConfiguredTargetKey,
+                  baseKeys,
+                  aspectDescriptor));
+    }
+
     @Override
     public SkyFunctionName functionName() {
       return SkyFunctions.ASPECT;
@@ -108,7 +128,7 @@
     }
 
     @Nullable
-    public ImmutableList<AspectKey> getBaseKeys() {
+    ImmutableList<AspectKey> getBaseKeys() {
       return baseKeys;
     }
 
@@ -123,6 +143,8 @@
       }
     }
 
+    // Note that this does not factor into equality/hash-code computations because its value is
+    // already encoded in the aspectConfigurationKey, albeit in an opaque way.
     protected boolean aspectConfigurationIsHost() {
       return false;
     }
@@ -223,13 +245,14 @@
     public String toString() {
       return (baseKeys == null ? label : baseKeys.toString())
           + "#"
-          + aspectDescriptor.getAspectClass().getName()
+          + aspectDescriptor
           + " "
           + aspectConfigurationKey
           + " "
           + baseConfiguredTargetKey
           + " "
-          + aspectDescriptor.getParameters();
+          + aspectDescriptor.getParameters()
+          + (aspectConfigurationIsHost() ? " (host)" : "");
     }
 
     AspectKey withLabel(Label label) {
@@ -270,38 +293,6 @@
     }
   }
 
-  private static class AspectKeyCodec implements ObjectCodec<AspectKey> {
-    private final ImmutableListCodec<AspectKey> listCodec = new ImmutableListCodec<>(this);
-
-    @Override
-    public Class<AspectKey> getEncodedClass() {
-      return AspectKey.class;
-    }
-
-    @Override
-    public void serialize(SerializationContext context, AspectKey obj, CodedOutputStream codedOut)
-        throws SerializationException, IOException {
-      Label.CODEC.serialize(context, obj.label, codedOut);
-      ConfiguredTargetKey.CODEC.serialize(context, obj.baseConfiguredTargetKey, codedOut);
-      listCodec.serialize(context, obj.baseKeys, codedOut);
-      AspectDescriptor.CODEC.serialize(context, obj.aspectDescriptor, codedOut);
-      Key.CODEC.serialize(context, obj.aspectConfigurationKey, codedOut);
-      codedOut.writeBoolNoTag(obj.aspectConfigurationIsHost());
-    }
-
-    @Override
-    public AspectKey deserialize(DeserializationContext context, CodedInputStream codedIn)
-        throws SerializationException, IOException {
-      return createAspectKey(
-          Label.CODEC.deserialize(context, codedIn),
-          ConfiguredTargetKey.CODEC.deserialize(context, codedIn),
-          listCodec.deserialize(context, codedIn),
-          AspectDescriptor.CODEC.deserialize(context, codedIn),
-          Key.CODEC.deserialize(context, codedIn),
-          codedIn.readBool());
-    }
-  }
-
   /**
    * The key for a skylark aspect.
    */
@@ -404,7 +395,7 @@
     }
 
     AspectKey toAspectKey(AspectClass aspectClass) {
-      return createAspectKey(
+      return AspectKey.createAspectKey(
           targetLabel,
           baseConfiguredTargetKey,
           ImmutableList.of(),
@@ -531,7 +522,7 @@
       AspectDescriptor aspectDescriptor,
       BuildConfiguration aspectConfiguration) {
     KeyAndHost aspectKeyAndHost = ConfiguredTargetKey.keyFromConfiguration(aspectConfiguration);
-    return createAspectKey(
+    return AspectKey.createAspectKey(
         label,
         ConfiguredTargetKey.of(label, baseConfiguration),
         baseKeys,
@@ -548,7 +539,7 @@
       AspectDescriptor aspectDescriptor,
       BuildConfiguration aspectConfiguration) {
     KeyAndHost aspectKeyAndHost = ConfiguredTargetKey.keyFromConfiguration(aspectConfiguration);
-    return createAspectKey(
+    return AspectKey.createAspectKey(
         label,
         ConfiguredTargetKey.of(label, baseConfiguration),
         ImmutableList.of(),
@@ -557,21 +548,6 @@
         aspectKeyAndHost.isHost);
   }
 
-  private static AspectKey createAspectKey(
-      Label label,
-      ConfiguredTargetKey configuredTargetKey,
-      ImmutableList<AspectKey> aspectKeys,
-      AspectDescriptor aspectDescriptor,
-      BuildConfigurationValue.Key aspectConfigurationKey,
-      boolean aspectConfigurationIsHost) {
-    return aspectKeyInterner.intern(
-        aspectConfigurationIsHost
-            ? new HostAspectKey(
-                label, aspectConfigurationKey, configuredTargetKey, aspectKeys, aspectDescriptor)
-            : new AspectKey(
-                label, aspectConfigurationKey, configuredTargetKey, aspectKeys, aspectDescriptor));
-  }
-
   private static final Interner<SkylarkAspectLoadingKey> skylarkAspectKeyInterner =
       BlazeInterners.newWeakInterner();