blob: b4202683995184df158b5d866f06136fa65f4434 [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.desugar.sealed;
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeTrue;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestBuilder;
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 com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import com.google.common.collect.ImmutableList;
import org.hamcrest.Matcher;
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 SealedClassesIllegalSubclassMergedTest extends TestBase {
@Parameter(0)
public TestParameters parameters;
static final Matcher<String> EXPECTED = containsString("cannot inherit from sealed class");
static final String EXPECTED_WITHOUT_PERMITTED_SUBCLASSES_ATTRIBUTE_OR_FIXED_ATTRIBUTE =
StringUtils.lines("Success!");
@Parameters(name = "{0}, keepPermittedSubclasses = {1}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
}
private void addTestClasses(TestBuilder<?, ?> builder) throws Exception {
builder
.addProgramClasses(TestClass.class, Sub1.class, Sub2.class)
.addProgramClassFileData(getTransformedClasses());
}
@Test
public void testJvm() throws Exception {
parameters.assumeJvmTestParameters();
assumeTrue(parameters.asCfRuntime().isNewerThanOrEqual(CfVm.JDK17));
testForJvm(parameters)
.apply(this::addTestClasses)
.run(parameters.getRuntime(), TestClass.class)
.assertFailureWithErrorThatMatches(EXPECTED);
}
private void inspect(CodeInspector inspector) {
ClassSubject clazz = inspector.clazz(Super.class);
assertThat(clazz, isPresentAndRenamed());
ClassSubject sub1 = inspector.clazz(Sub1.class);
assertThat(sub1, isPresentAndRenamed());
assertEquals(
parameters.isCfRuntime() ? ImmutableList.of(sub1.asTypeSubject()) : ImmutableList.of(),
clazz.getFinalPermittedSubclassAttributes());
}
@Test
public void testR8() throws Exception {
parameters.assumeR8TestParameters();
testForR8(parameters.getBackend())
.apply(this::addTestClasses)
.setMinApi(parameters)
.addKeepAttributePermittedSubclasses()
.addKeepClassRulesWithAllowObfuscation(Super.class)
.addKeepMainRule(TestClass.class)
.addHorizontallyMergedClassesInspector(
inspector -> {
inspector
.assertIsCompleteMergeGroup(Sub2.class, Sub1.class)
.assertNoOtherClassesMerged();
})
.compile()
.inspect(this::inspect)
.run(parameters.getRuntime(), TestClass.class)
.applyIf(
!parameters.isCfRuntime() || parameters.asCfRuntime().isNewerThanOrEqual(CfVm.JDK17),
// On JDK 17 the class merging also prevents "cannot inherit from sealed class".
r ->
r.assertSuccessWithOutput(
EXPECTED_WITHOUT_PERMITTED_SUBCLASSES_ATTRIBUTE_OR_FIXED_ATTRIBUTE),
r -> r.assertFailureWithErrorThatThrows(UnsupportedClassVersionError.class));
}
public byte[] getTransformedClasses() throws Exception {
return transformer(Super.class).setPermittedSubclasses(Super.class, Sub1.class).transform();
}
static class TestClass {
public static void main(String[] args) {
new Sub1();
new Sub2();
System.out.println("Success!");
}
}
abstract static class Super /* permits Sub1 */ {}
static class Sub1 extends Super {}
static class Sub2 extends Super {}
}