Desugar calls to Set.of

Test: tools/test.py --no-internal -v *BackportJava9Test*
Change-Id: I58e06a6fc1da56ffdf3a06890de38e5eeb4cd293
diff --git a/src/main/java/com/android/tools/r8/graph/DexItemFactory.java b/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
index 10e75e8..41b11d1 100644
--- a/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
+++ b/src/main/java/com/android/tools/r8/graph/DexItemFactory.java
@@ -214,6 +214,7 @@
   public final DexString proxyDescriptor = createString("Ljava/lang/reflect/Proxy;");
   public final DexString serviceLoaderDescriptor = createString("Ljava/util/ServiceLoader;");
   public final DexString listDescriptor = createString("Ljava/util/List;");
+  public final DexString setDescriptor = createString("Ljava/util/Set;");
   public final DexString comparatorDescriptor = createString("Ljava/util/Comparator;");
   public final DexString callableDescriptor = createString("Ljava/util/concurrent/Callable;");
   public final DexString supplierDescriptor = createString("Ljava/util/function/Supplier;");
@@ -296,6 +297,7 @@
   public final DexType proxyType = createType(proxyDescriptor);
   public final DexType serviceLoaderType = createType(serviceLoaderDescriptor);
   public final DexType listType = createType(listDescriptor);
+  public final DexType setType = createType(setDescriptor);
   public final DexType comparatorType = createType(comparatorDescriptor);
   public final DexType callableType = createType(callableDescriptor);
   public final DexType supplierType = createType(supplierDescriptor);
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
index 91e5902..decfd0a 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
@@ -40,6 +40,7 @@
 import com.android.tools.r8.ir.desugar.backports.MathMethods;
 import com.android.tools.r8.ir.desugar.backports.ObjectsMethods;
 import com.android.tools.r8.ir.desugar.backports.OptionalMethods;
+import com.android.tools.r8.ir.desugar.backports.SetMethods;
 import com.android.tools.r8.ir.desugar.backports.ShortMethods;
 import com.android.tools.r8.ir.desugar.backports.StringMethods;
 import com.android.tools.r8.ir.synthetic.TemplateMethodCode;
@@ -852,12 +853,18 @@
       addProvider(new MethodGenerator(clazz, method, proto, ObjectsMethods::new));
 
       // List<E> List.of(<args>) for 0 to 10 arguments
-      clazz = factory.listDescriptor;
+      // Set<E> Set.of(<args>) for 0 to 10 arguments
       method = factory.createString("of");
       ArrayList<DexType> parameters = new ArrayList<>();
       for (int i = 0; i <= 10; i++) {
+        clazz = factory.listDescriptor;
         proto = factory.createProto(factory.listType, parameters);
         addProvider(new MethodGenerator(clazz, method, proto, ListMethods::new));
+
+        clazz = factory.setDescriptor;
+        proto = factory.createProto(factory.setType, parameters);
+        addProvider(new MethodGenerator(clazz, method, proto, SetMethods::new));
+
         parameters.add(factory.objectType);
       }
 
@@ -866,6 +873,12 @@
       method = factory.createString("of");
       proto = factory.createProto(factory.listType, factory.objectArrayType);
       addProvider(new MethodGenerator(clazz, method, proto, ListMethods::new, "ofVarargs"));
+
+      // Set<E> Set.of(E...)
+      clazz = factory.setDescriptor;
+      method = factory.createString("of");
+      proto = factory.createProto(factory.setType, factory.objectArrayType);
+      addProvider(new MethodGenerator(clazz, method, proto, SetMethods::new, "ofVarargs"));
     }
 
     private void initializeJava11MethodProviders(DexItemFactory factory) {
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/backports/SetMethods.java b/src/main/java/com/android/tools/r8/ir/desugar/backports/SetMethods.java
new file mode 100644
index 0000000..8d57e8f
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/ir/desugar/backports/SetMethods.java
@@ -0,0 +1,89 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.ir.desugar.backports;
+
+import com.android.tools.r8.graph.DexMethod;
+import com.android.tools.r8.ir.synthetic.TemplateMethodCode;
+import com.android.tools.r8.utils.InternalOptions;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+@SuppressWarnings("unchecked")
+public class SetMethods extends TemplateMethodCode {
+
+  public SetMethods(InternalOptions options, DexMethod method, String methodName) {
+    super(options, method, methodName, method.proto.toDescriptorString());
+  }
+
+  public static <E> Set<E> of() {
+    return Collections.emptySet();
+  }
+
+  public static <E> Set<E> of(E e0) {
+    return Collections.singleton(Objects.requireNonNull(e0));
+  }
+
+  public static <E> Set<E> of(E e0, E e1) {
+    E[] elements = (E[]) new Object[] { e0, e1 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5, E e6) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5, e6 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5, e6, e7 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5, e6, e7, e8 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5, e6, e7, e8, e9 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> of(E e0, E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
+    E[] elements = (E[]) new Object[] { e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10 };
+    return Set.of(elements);
+  }
+
+  public static <E> Set<E> ofVarargs(E[] elements) {
+    HashSet<E> set = new HashSet<>(elements.length);
+    for (E element : elements) {
+      if (!set.add(Objects.requireNonNull(element))) {
+        throw new IllegalArgumentException("duplicate element: " + element);
+      }
+    }
+    return Collections.unmodifiableSet(set);
+  }
+}
diff --git a/src/test/examplesJava9/backport/SetBackportJava9Main.java b/src/test/examplesJava9/backport/SetBackportJava9Main.java
new file mode 100644
index 0000000..c6f03ae
--- /dev/null
+++ b/src/test/examplesJava9/backport/SetBackportJava9Main.java
@@ -0,0 +1,204 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package backport;
+
+import java.util.Set;
+
+public class SetBackportJava9Main {
+
+  public static void main(String[] args) {
+    testOf0();
+    testOf1();
+    testOf2();
+    testOf10();
+    testOfVarargs();
+  }
+
+  private static void testOf0() {
+    Set<Object> ofObject = Set.of();
+    assertEquals(0, ofObject.size());
+    assertFalse(ofObject.contains(new Object()));
+    assertMutationNotAllowed(ofObject);
+
+    Set<Integer> ofInteger = Set.of();
+    assertEquals(0, ofInteger.size());
+    assertFalse(ofInteger.contains(0));
+  }
+
+  private static void testOf1() {
+    Object anObject = new Object();
+    Set<Object> ofObject = Set.of(anObject);
+    assertEquals(1, ofObject.size());
+    assertTrue(ofObject.contains(anObject));
+    assertFalse(ofObject.contains(new Object()));
+    assertMutationNotAllowed(ofObject);
+
+    Set<Integer> ofInteger = Set.of(1);
+    assertEquals(1, ofInteger.size());
+    assertTrue(ofInteger.contains(1));
+    assertFalse(ofInteger.contains(2));
+
+    try {
+      Set.of((Object) null);
+      throw new AssertionError();
+    } catch (NullPointerException expected) {
+    }
+  }
+
+  private static void testOf2() {
+    Object anObject0 = new Object();
+    Object anObject1 = new Object();
+    Set<Object> ofObject = Set.of(anObject0, anObject1);
+    assertEquals(2, ofObject.size());
+    assertTrue(ofObject.contains(anObject0));
+    assertTrue(ofObject.contains(anObject1));
+    assertFalse(ofObject.contains(new Object()));
+    assertMutationNotAllowed(ofObject);
+
+    Set<Integer> ofInteger = Set.of(1, 2);
+    assertEquals(2, ofInteger.size());
+    assertTrue(ofInteger.contains(1));
+    assertTrue(ofInteger.contains(2));
+    assertFalse(ofInteger.contains(3));
+
+    Set<Object> ofMixed = Set.of(anObject0, 1);
+    assertEquals(2, ofMixed.size());
+    assertTrue(ofMixed.contains(anObject0));
+    assertTrue(ofMixed.contains(1));
+    assertFalse(ofMixed.contains(2));
+    assertFalse(ofMixed.contains(anObject1));
+    assertMutationNotAllowed(ofMixed);
+
+    try {
+      Set.of(1, null);
+      throw new AssertionError();
+    } catch (NullPointerException expected) {
+    }
+
+    try {
+      Set.of(1, 1);
+      throw new AssertionError();
+    } catch (IllegalArgumentException expected) {
+    }
+  }
+
+  private static void testOf10() {
+    Object anObject0 = new Object();
+    Object anObject6 = new Object();
+    Object anObject9 = new Object();
+    Set<Object> ofObject =
+        Set.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
+            anObject6, new Object(), new Object(), anObject9);
+    assertEquals(10, ofObject.size());
+    assertTrue(ofObject.contains(anObject0));
+    assertTrue(ofObject.contains(anObject6));
+    assertTrue(ofObject.contains(anObject9));
+    assertFalse(ofObject.contains(new Object()));
+    assertMutationNotAllowed(ofObject);
+
+    Set<Integer> ofInteger = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
+    assertEquals(10, ofInteger.size());
+    assertTrue(ofInteger.contains(0));
+    assertTrue(ofInteger.contains(6));
+    assertTrue(ofInteger.contains(9));
+    assertFalse(ofInteger.contains(10));
+
+    Set<Object> ofMixed = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, anObject9);
+    assertEquals(10, ofMixed.size());
+    assertTrue(ofMixed.contains(0));
+    assertTrue(ofMixed.contains(6));
+    assertTrue(ofMixed.contains(anObject9));
+    assertFalse(ofMixed.contains(anObject0));
+    assertMutationNotAllowed(ofMixed);
+
+    try {
+      Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, null);
+      throw new AssertionError();
+    } catch (NullPointerException expected) {
+    }
+
+    try {
+      Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 0);
+      throw new AssertionError();
+    } catch (IllegalArgumentException expected) {
+    }
+  }
+
+  private static void testOfVarargs() {
+    Object anObject0 = new Object();
+    Object anObject6 = new Object();
+    Object anObject10 = new Object();
+    Set<Object> ofObject =
+        Set.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
+            anObject6, new Object(), new Object(), new Object(), anObject10);
+    assertEquals(11, ofObject.size());
+    assertTrue(ofObject.contains(anObject0));
+    assertTrue(ofObject.contains(anObject6));
+    assertTrue(ofObject.contains(anObject10));
+    assertFalse(ofObject.contains(new Object()));
+    assertMutationNotAllowed(ofObject);
+
+    Set<Integer> ofInteger = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+    assertEquals(11, ofInteger.size());
+    assertTrue(ofInteger.contains(0));
+    assertTrue(ofInteger.contains(6));
+    assertTrue(ofInteger.contains(10));
+    assertFalse(ofInteger.contains(11));
+
+    Set<Object> ofMixed = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, anObject10);
+    assertEquals(11, ofMixed.size());
+    assertTrue(ofMixed.contains(0));
+    assertTrue(ofMixed.contains(6));
+    assertTrue(ofMixed.contains(anObject10));
+    assertFalse(ofMixed.contains(10));
+    assertFalse(ofMixed.contains(anObject0));
+    assertMutationNotAllowed(ofMixed);
+
+    // Ensure the supplied mutable array is not used directly since it is mutable.
+    Object[] mutableArray = { anObject0 };
+    Set<Object> ofMutableArray = Set.of(mutableArray);
+    mutableArray[0] = anObject10;
+    assertTrue(ofMutableArray.contains(anObject0));
+    assertFalse(ofMutableArray.contains(anObject10));
+
+    try {
+      Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null);
+      throw new AssertionError();
+    } catch (NullPointerException expected) {
+    }
+
+    try {
+      Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
+      throw new AssertionError();
+    } catch (IllegalArgumentException expected) {
+    }
+  }
+
+  private static void assertMutationNotAllowed(Set<Object> ofObject) {
+    try {
+      ofObject.add(new Object());
+      throw new AssertionError();
+    } catch (UnsupportedOperationException expected) {
+    }
+  }
+
+  private static void assertTrue(boolean value) {
+    if (!value) {
+      throw new AssertionError("Expected <true> but was <false>");
+    }
+  }
+
+  private static void assertFalse(boolean value) {
+    if (value) {
+      throw new AssertionError("Expected <false> but was <true>");
+    }
+  }
+
+  private static void assertEquals(Object expected, Object actual) {
+    if (expected != actual && !expected.equals(actual)) {
+      throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">");
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/backports/SetBackportJava9Test.java b/src/test/java/com/android/tools/r8/desugar/backports/SetBackportJava9Test.java
new file mode 100644
index 0000000..3144382
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/backports/SetBackportJava9Test.java
@@ -0,0 +1,35 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.desugar.backports;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime.CfVm;
+import com.android.tools.r8.ToolHelper;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import static com.android.tools.r8.utils.FileUtils.JAR_EXTENSION;
+
+@RunWith(Parameterized.class)
+public class SetBackportJava9Test extends AbstractBackportTest {
+  @Parameters(name = "{0}")
+  public static Iterable<?> data() {
+    return getTestParameters()
+        .withCfRuntimesStartingFromIncluding(CfVm.JDK9)
+        .withDexRuntimes()
+        .build();
+  }
+
+  private static final Path TEST_JAR =
+      Paths.get(ToolHelper.EXAMPLES_JAVA9_BUILD_DIR).resolve("backport" + JAR_EXTENSION);
+
+  public SetBackportJava9Test(TestParameters parameters) {
+    super(parameters, Byte.class, TEST_JAR, "backport.SetBackportJava9Main");
+    // TODO Once shipped in an actual API level, migrate to SetBackportTest
+  }
+}