blob: b5752ccffa018beadabecbab91aefce4d163bd64 [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.ir.optimize.uninstantiatedtypes;
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
import static org.hamcrest.MatcherAssert.assertThat;
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.utils.AndroidApiLevel;
import com.android.tools.r8.utils.StringUtils;
import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/** Reproduction for b/128917897. */
@RunWith(Parameterized.class)
public class NestedInterfaceMethodTest extends TestBase {
private final TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection params() {
return getTestParameters().withAllRuntimes().build();
}
public NestedInterfaceMethodTest(TestParameters parameters) {
this.parameters = parameters;
}
@Test
public void test() throws Exception {
String expectedOutput = StringUtils.lines("In A.m()", "In A.m()");
if (parameters.isCfRuntime()) {
testForJvm().addTestClasspath().run(TestClass.class).assertSuccessWithOutput(expectedOutput);
}
CodeInspector inspector =
testForR8(parameters.getBackend())
.addInnerClasses(NestedInterfaceMethodTest.class)
.addKeepMainRule(TestClass.class)
.enableInliningAnnotations()
.enableMergeAnnotations()
.addOptionsModification(
options -> {
options.enableDevirtualization = false;
options.enableInliningOfInvokesWithNullableReceivers = false;
})
.setMinApi(AndroidApiLevel.B)
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutput(expectedOutput)
.inspector();
ClassSubject interfaceSubject = inspector.clazz(I.class);
assertThat(interfaceSubject, isPresent());
assertThat(interfaceSubject.method(Uninstantiated.class.getTypeName(), "m"), isPresent());
ClassSubject classSubject = inspector.clazz(A.class);
assertThat(classSubject, isPresent());
assertThat(classSubject.method(Uninstantiated.class.getTypeName(), "m"), isPresent());
}
static class TestClass {
public static void main(String[] args) {
test(new B());
test(new C());
}
@NeverInline
private static void test(I obj) {
obj.m();
}
}
@NeverMerge
interface I {
Uninstantiated m();
}
@NeverMerge
interface J extends I {}
static class A implements J {
@Override
public Uninstantiated m() {
System.out.println("In A.m()");
return null;
}
}
static class B extends A {}
// The purpose of this class is merely to avoid that the invoke-interface instruction in
// TestClass.test() gets devirtualized to an invoke-virtual instruction. Otherwise the method
// I.m() would not be present in the output.
static class C extends A {}
static class Uninstantiated {}
}