blob: ae2b6ae6acf8fa93260d45d14c7c6a01da62faa1 [file] [log] [blame]
// Copyright (c) 2021, 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.desugar.nestaccesscontrol;
import com.android.tools.r8.DesugarTestConfiguration;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.TestRuntime.CfVm;
import com.android.tools.r8.utils.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class NestPrivateInterfaceMethodsTest extends TestBase {
static final String EXPECTED = StringUtils.lines("Hello world!");
private final TestParameters parameters;
@Parameterized.Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
}
public NestPrivateInterfaceMethodsTest(TestParameters parameters) {
this.parameters = parameters;
}
private byte[] getClassWithNest(Class<?> clazz) throws Exception {
return transformer(clazz)
.setNest(I.class, J.class)
.setAccessFlags(
I.class.getMethod("foo"),
flags -> {
flags.unsetPublic();
flags.setPrivate();
})
.transform();
}
@Test
public void test() throws Exception {
testForDesugaring(parameters)
.addProgramClasses(TestClass.class)
.addProgramClassFileData(getClassWithNest(I.class), getClassWithNest(J.class))
.run(parameters.getRuntime(), TestClass.class)
// TODO(191115349): Nest desugar does not downgrade the classfile version.
.applyIf(
c ->
DesugarTestConfiguration.isNotJavac(c)
|| parameters.getRuntime().asCf().isNewerThanOrEqual(CfVm.JDK11),
r -> r.assertSuccessWithOutput(EXPECTED),
r -> r.assertFailureWithErrorThatThrows(UnsupportedClassVersionError.class));
}
interface I {
default /* will be private */ void foo() {
System.out.println("Hello world!");
}
}
interface J {
default void bar(I o) {
o.foo();
}
}
static class TestClass implements I, J {
public static void main(String[] args) {
TestClass o = new TestClass();
o.bar(o);
}
}
}