Add test for invoke-static without interface bit set
This is strangely allowed on the JDK8 JVM but not later
Bug: 144861100
Change-Id: I2943ff27794e429fe3b68b8758be9c459f328c9e
diff --git a/src/test/java/com/android/tools/r8/graph/invokestatic/InvokeStaticOnInterfaceTest.java b/src/test/java/com/android/tools/r8/graph/invokestatic/InvokeStaticOnInterfaceTest.java
new file mode 100644
index 0000000..0f4fe9b
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/invokestatic/InvokeStaticOnInterfaceTest.java
@@ -0,0 +1,99 @@
+// 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.graph.invokestatic;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.objectweb.asm.Opcodes.INVOKESTATIC;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NeverMerge;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
+import com.android.tools.r8.TestRuntime.CfVm;
+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 InvokeStaticOnInterfaceTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withCfRuntimes().build();
+ }
+
+ public InvokeStaticOnInterfaceTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testJvm() throws IOException, CompilationFailedException, ExecutionException {
+ assertTrue(parameters.isCfRuntime());
+ TestRunResult<?> runResult =
+ testForRuntime(parameters)
+ .addProgramClasses(I.class)
+ .addProgramClassFileData(getClassWithTransformedInvoked())
+ .run(parameters.getRuntime(), Main.class);
+ if (parameters.getRuntime().asCf().isNewerThan(CfVm.JDK8)) {
+ runResult.assertFailureWithErrorThatMatches(
+ containsString(
+ "java.lang.IncompatibleClassChangeError: Method"
+ + " com.android.tools.r8.graph.invokestatic.InvokeStaticOnInterfaceTest$I.foo()V"
+ + " must be InterfaceMethodref constant"));
+ } else {
+ runResult.assertSuccessWithOutputLines("Hello World!");
+ }
+ }
+
+ @Test
+ public void testCfInvokeOnStaticInterfaceMethod()
+ throws ExecutionException, CompilationFailedException, IOException {
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Main.class)
+ .addProgramClassFileData(getClassWithTransformedInvoked())
+ .enableInliningAnnotations()
+ .enableMergeAnnotations()
+ .addKeepMainRule(Main.class)
+ .run(parameters.getRuntime(), Main.class);
+ }
+
+ private byte[] getClassWithTransformedInvoked() throws IOException {
+ return transformer(Main.class)
+ .transformMethodInsnInMethod(
+ "main",
+ (opcode, owner, name, descriptor, isInterface, continuation) -> {
+ assertEquals(INVOKESTATIC, opcode);
+ assertTrue(isInterface);
+ continuation.apply(opcode, owner, name, descriptor, false);
+ })
+ .transform();
+ }
+
+ @NeverMerge
+ public interface I {
+
+ @NeverInline
+ static void foo() {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ I.foo();
+ }
+ }
+}