Add temporary compatibility layer for human specification 100

- Fix internal bots

Bug: b/309735284
Change-Id: I88616cd8c0946bb8726b430f0b14f13796fcdb3e
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
index 32dfd13..d993797 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanDesugaredLibrarySpecificationParser.java
@@ -345,8 +345,15 @@
     if (jsonFlagSet.has(EMULATE_INTERFACE_KEY)) {
       for (Map.Entry<String, JsonElement> itf :
           jsonFlagSet.get(EMULATE_INTERFACE_KEY).getAsJsonObject().entrySet()) {
-        builder.putEmulatedInterface(
-            stringDescriptorToDexType(itf.getKey()), parseEmulatedInterface(itf.getValue()));
+        if (itf.getValue().isJsonPrimitive()) {
+          builder.putLegacyEmulatedInterface(
+              stringDescriptorToDexType(itf.getKey()),
+              stringDescriptorToDexType(itf.getValue().getAsString()));
+        } else {
+          builder.putSpecifiedEmulatedInterface(
+              stringDescriptorToDexType(itf.getKey()),
+              parseEmulatedInterfaceDescriptor(itf.getValue()));
+        }
       }
     }
     if (jsonFlagSet.has(CUSTOM_CONVERSION_KEY)) {
@@ -392,7 +399,7 @@
     }
   }
 
-  private HumanEmulatedInterfaceDescriptor parseEmulatedInterface(JsonElement value) {
+  private HumanEmulatedInterfaceDescriptor parseEmulatedInterfaceDescriptor(JsonElement value) {
     JsonObject jsonObject = value.getAsJsonObject();
     DexType rewrittenType =
         stringDescriptorToDexType(required(jsonObject, REWRITTEN_TYPE_KEY).getAsString());
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanRewritingFlags.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanRewritingFlags.java
index e4db485..c4283b3 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanRewritingFlags.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/HumanRewritingFlags.java
@@ -5,6 +5,7 @@
 package com.android.tools.r8.ir.desugar.desugaredlibrary.humanspecification;
 
 import com.android.tools.r8.graph.DexField;
+import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexType;
 import com.android.tools.r8.graph.FieldAccessFlags;
@@ -116,6 +117,10 @@
       this.emulatedMethods = emulatedMethods;
     }
 
+    public boolean isLegacy() {
+      return false;
+    }
+
     public DexType getRewrittenType() {
       return rewrittenType;
     }
@@ -149,6 +154,35 @@
       builder.addAll(other.getEmulatedMethods());
       return new HumanEmulatedInterfaceDescriptor(rewrittenType, builder.build());
     }
+
+    public boolean containsEmulatedMethod(DexMethod reference, DexItemFactory factory) {
+      return getEmulatedMethods().contains(reference);
+    }
+  }
+
+  // TODO(b/309735284): Temporary work-around.
+  public static class LegacyHumanEmulatedInterfaceDescriptor
+      extends HumanEmulatedInterfaceDescriptor {
+
+    public LegacyHumanEmulatedInterfaceDescriptor(DexType rewrittenType) {
+      super(rewrittenType, ImmutableSet.of());
+    }
+
+    public boolean isLegacy() {
+      return true;
+    }
+
+    public HumanEmulatedInterfaceDescriptor merge(HumanEmulatedInterfaceDescriptor other) {
+      throw new UnsupportedOperationException();
+    }
+
+    public boolean containsEmulatedMethod(DexMethod reference, DexItemFactory factory) {
+      // Equivalence for parsing specification with format version 100.
+      DexMethod dontRewrite =
+          factory.createMethod(
+              factory.iteratorType, factory.createProto(factory.voidType), "remove");
+      return reference != dontRewrite;
+    }
   }
 
   public static Builder builder(Reporter reporter, Origin origin) {
@@ -413,7 +447,16 @@
       return this;
     }
 
-    public Builder putEmulatedInterface(
+    public Builder putLegacyEmulatedInterface(DexType interfaceType, DexType rewrittenType) {
+      put(
+          emulatedInterfaces,
+          interfaceType,
+          new LegacyHumanEmulatedInterfaceDescriptor(rewrittenType),
+          HumanDesugaredLibrarySpecificationParser.EMULATE_INTERFACE_KEY);
+      return this;
+    }
+
+    public Builder putSpecifiedEmulatedInterface(
         DexType interfaceType, HumanEmulatedInterfaceDescriptor newDescriptor) {
       assert newDescriptor != null;
       HumanEmulatedInterfaceDescriptor oldDescriptor = emulatedInterfaces.get(interfaceType);
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationFlagDeduplicator.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationFlagDeduplicator.java
index 12758eb..cb907d1 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationFlagDeduplicator.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationFlagDeduplicator.java
@@ -92,8 +92,8 @@
     deduplicateEmulatedInterfaceFlags(
         flags.getEmulatedInterfaces(),
         otherFlags.getEmulatedInterfaces(),
-        commonBuilder::putEmulatedInterface,
-        builder::putEmulatedInterface);
+        commonBuilder::putSpecifiedEmulatedInterface,
+        builder::putSpecifiedEmulatedInterface);
     deduplicateFlags(
         flags.getRetargetMethodToType(),
         otherFlags.getRetargetMethodToType(),
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationJsonExporter.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationJsonExporter.java
index e59cf92..d5dc102 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationJsonExporter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/humanspecification/MultiAPILevelHumanDesugaredLibrarySpecificationJsonExporter.java
@@ -123,6 +123,7 @@
             .forEach(
                 (itf, descriptor) -> {
                   TreeMap<String, Object> value = new TreeMap<>();
+                  assert !descriptor.isLegacy();
                   emulatedInterfaces.put(toString(itf), value);
                   value.put(REWRITTEN_TYPE_KEY, toString(descriptor.getRewrittenType()));
                   value.put(EMULATED_METHODS_KEY, setToString(descriptor.getEmulatedMethods()));
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/HumanToMachineEmulatedInterfaceConverter.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/HumanToMachineEmulatedInterfaceConverter.java
index f97a6f3..c908340 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/HumanToMachineEmulatedInterfaceConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/HumanToMachineEmulatedInterfaceConverter.java
@@ -58,7 +58,8 @@
       itfClass.forEachClassMethodMatching(
           m ->
               m.isDefaultMethod()
-                  && humanDescriptor.getEmulatedMethods().contains(m.getReference()),
+                  && humanDescriptor.containsEmulatedMethod(
+                      m.getReference(), appInfo.dexItemFactory()),
           method ->
               emulatedMethods.put(
                   method.getReference(),
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
index 79db832..c36af1c 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/specificationconversion/LegacyToHumanSpecificationConverter.java
@@ -233,7 +233,7 @@
     dexClass
         .virtualMethods(m -> m.isDefaultMethod() && !dontRewrite.contains(m.getReference()))
         .forEach(m -> emulatedMethods.add(m.getReference()));
-    builder.putEmulatedInterface(
+    builder.putSpecifiedEmulatedInterface(
         type, new HumanEmulatedInterfaceDescriptor(rewrittenType, emulatedMethods));
   }