blob: 09d345628a6ff1335b0ac0ca87534f8882f3be3a [file] [log] [blame]
// Copyright (c) 2023, 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.accessrelaxation;
import com.android.tools.r8.NeverClassInline;
import com.android.tools.r8.NeverInline;
import com.android.tools.r8.NoVerticalClassMerging;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.utils.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class PublicOverrideOfPrivateMethodTest extends TestBase {
private static final String EXPECTED_OUTPUT = StringUtils.lines("A", "B", "B", "A", "B", "A");
@Parameter(0)
public TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimesAndApiLevels().build();
}
@Test
public void testRuntime() throws Exception {
testForRuntime(parameters)
.addInnerClasses(getClass())
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutput(EXPECTED_OUTPUT);
}
@Test
public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addInnerClasses(getClass())
.addKeepMainRule(Main.class)
.allowAccessModification()
.enableInliningAnnotations()
.enableNeverClassInliningAnnotations()
.enableNoVerticalClassMergingAnnotations()
.setMinApi(parameters)
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutput(EXPECTED_OUTPUT);
}
static class Main {
public static void main(String[] args) {
new A().foo();
new B().foo();
A a = System.currentTimeMillis() > 0 ? new A() : new B();
a.foo();
A b = System.currentTimeMillis() > 0 ? new B() : new A();
b.foo();
}
}
@NeverClassInline
@NoVerticalClassMerging
static class A {
@NeverInline
private void foo() {
System.out.println("A");
}
}
@NeverClassInline
static class B extends A {
B() {
if (System.currentTimeMillis() > 0) {
foo();
}
}
@NeverInline
public void foo() {
System.out.println("B");
}
}
}