blob: 6c2effec6c1a8b1f170040bd09b669ef989b73ef [file] [log] [blame]
// 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.resolution;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
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 com.android.tools.r8.ToolHelper.DexVm.Version;
import com.android.tools.r8.utils.DescriptorUtils;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class InvokeVirtualOnInterfaceTest extends TestBase {
private final TestParameters parameters;
@Parameterized.Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimes().withAllApiLevels().build();
}
public InvokeVirtualOnInterfaceTest(TestParameters parameters) {
this.parameters = parameters;
}
@Test
public void testReference() throws Exception {
testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
.addProgramClasses(I.class, C1.class, C2.class)
.addProgramClassFileData(transformMain())
.run(parameters.getRuntime(), Main.class)
.assertFailureWithErrorThatMatches(getExpectedFailureMatcher(false));
}
@Test
public void testR8() throws Exception {
try {
testForR8(parameters.getBackend())
.addProgramClasses(I.class, C1.class, C2.class)
.addProgramClassFileData(transformMain())
.addKeepMainRule(Main.class)
.setMinApi(parameters.getApiLevel())
.compile()
.run(parameters.getRuntime(), Main.class)
.assertFailureWithErrorThatMatches(getExpectedFailureMatcher(true));
} catch (CompilationFailedException e) {
// TODO(b/144085169): The class file pipeline throws an assertion error, but should not.
assertTrue(parameters.isCfRuntime());
}
}
private Matcher<String> getExpectedFailureMatcher(boolean isR8) {
if (parameters.getRuntime().isDex()
&& parameters
.getRuntime()
.asDex()
.getVm()
.getVersion()
.isOlderThanOrEqual(Version.V4_4_4)) {
return containsString("VerifyError");
}
if (isR8
&& parameters.getRuntime().isDex()
&& parameters
.getRuntime()
.asDex()
.getVm()
.getVersion()
.isOlderThanOrEqual(Version.V7_0_0)) {
// TODO(b/144085169): R8 ends up causing a code change changing the error on these runtimes.
return containsString("NoSuchMethodError");
}
return containsString("IncompatibleClassChangeError");
}
public interface I {
void f();
}
public static class C1 implements I {
@Override
public void f() {
System.out.println("C1::f");
}
}
public static class C2 implements I {
@Override
public void f() {
System.out.println("C2::f");
}
}
static class Main {
public static void main(String[] args) {
I i = args.length % 2 == 0 ? new C1() : new C2();
i.f();
}
}
private static byte[] transformMain() throws Exception {
String binaryNameForI = DescriptorUtils.getBinaryNameFromJavaType(I.class.getTypeName());
return transformer(Main.class)
.transformMethodInsnInMethod(
"main",
(opcode, owner, name, descriptor, isInterface, continuation) -> {
if (owner.equals(binaryNameForI) && name.equals("f")) {
assertEquals(INVOKEINTERFACE, opcode);
assertTrue(isInterface);
continuation.visitMethodInsn(INVOKEVIRTUAL, owner, name, descriptor, false);
} else {
continuation.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
})
.transform();
}
}