Interfaces in Clazz.forName should be marked as live

Before this CL we only trace classes as input to Clazz.forName and not
interfaces. This CL changes this to also mark interfaces as live.

Bug: 146534384
Change-Id: I42c5d190e47c840b8d540da473a89dd1df344384
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 6bd3db2..4b412cf 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -726,7 +726,7 @@
       if (clazz != null) {
         KeepReason reason = KeepReason.methodHandleReferencedIn(currentMethod);
         if (clazz.isInterface() && !clazz.accessFlags.isAnnotation()) {
-          markInterfaceAsInstantiated(clazz, graphReporter.registerClass(clazz, reason));
+          markInterfaceAsInstantiated(clazz, graphReporter.registerInterface(clazz, reason));
         } else {
           markInstantiated(clazz, null, reason);
         }
@@ -2719,7 +2719,9 @@
       if (clazz == null) {
         return;
       }
-      if (!clazz.isInterface()) {
+      if (clazz.isInterface()) {
+        markTypeAsLive(clazz.type, KeepReason.reflectiveUseIn(method));
+      } else {
         markInstantiated(clazz, null, KeepReason.reflectiveUseIn(method));
         if (clazz.hasDefaultInitializer()) {
           DexEncodedMethod initializer = clazz.getDefaultInitializer();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/reflection/ForNameInterfaceTest.java b/src/test/java/com/android/tools/r8/ir/optimize/reflection/ForNameInterfaceTest.java
new file mode 100644
index 0000000..31f8463
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/reflection/ForNameInterfaceTest.java
@@ -0,0 +1,60 @@
+// 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.optimize.reflection;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class ForNameInterfaceTest extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public ForNameInterfaceTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  public interface I {}
+
+  public @interface J {}
+
+  public static class Main {
+
+    public static void main(String[] args) throws ClassNotFoundException {
+      Class<?> aClass =
+          Class.forName("com.android.tools.r8.ir.optimize.reflection.ForNameInterfaceTest$I");
+      System.out.println(aClass.getName());
+      aClass = Class.forName("com.android.tools.r8.ir.optimize.reflection.ForNameInterfaceTest$J");
+      System.out.println(aClass.getName());
+    }
+  }
+
+  @Test
+  public void testForNameOnInterface()
+      throws ExecutionException, CompilationFailedException, IOException {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(ForNameInterfaceTest.class)
+        .addKeepMainRule(Main.class)
+        .minification(false)
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines(
+            "com.android.tools.r8.ir.optimize.reflection.ForNameInterfaceTest$I",
+            "com.android.tools.r8.ir.optimize.reflection.ForNameInterfaceTest$J");
+  }
+}