blob: 273b7ec0efc46a3c5884cbf528ccb877befc4e97 [file] [log] [blame]
Clément Bérafdced052024-06-06 11:17:38 +02001// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
clementberad5bd4f32019-05-21 12:22:06 +02004
Clément Bérafdced052024-06-06 11:17:38 +02005package nesthostexample;
clementberad5bd4f32019-05-21 12:22:06 +02006
Christoffer Quist Adamsene1575c32021-02-18 10:23:19 +01007import com.android.tools.r8.Jdk9TestUtils;
clementberad5bd4f32019-05-21 12:22:06 +02008import com.android.tools.r8.R8TestCompileResult;
9import com.android.tools.r8.TestBase;
10import com.android.tools.r8.TestParameters;
11import com.android.tools.r8.TestParametersCollection;
12import com.android.tools.r8.TestRuntime.CfVm;
13import com.android.tools.r8.utils.StringUtils;
clementberad5bd4f32019-05-21 12:22:06 +020014import java.util.List;
15import org.junit.Test;
16import org.junit.runner.RunWith;
17import org.junit.runners.Parameterized;
Christoffer Quist Adamsen42e8bde2023-01-31 11:43:46 +010018import org.junit.runners.Parameterized.Parameter;
clementberad5bd4f32019-05-21 12:22:06 +020019import org.junit.runners.Parameterized.Parameters;
20
21@RunWith(Parameterized.class)
22public class NestClassMergingTest extends TestBase {
23
Christoffer Quist Adamsen42e8bde2023-01-31 11:43:46 +010024 @Parameter(0)
25 public TestParameters parameters;
clementberad5bd4f32019-05-21 12:22:06 +020026
Clément Bérafdced052024-06-06 11:17:38 +020027 private final Class<?> NEST_MAIN_CLASS = NestHostInlining.class;
28 private final Class<?> NEST_SUBCLASS_MAIN_CLASS = NestHostInliningSubclasses.class;
29 private final Class<?> OUTSIDE_WITH_ACCESS_MAIN_CLASS = OutsideInliningWithAccess.class;
30 private final Class<?> OUTSIDE_NO_ACCESS_MAIN_CLASS = OutsideInliningNoAccess.class;
clementberad5bd4f32019-05-21 12:22:06 +020031 private final String NEST_MAIN_EXPECTED_RESULT =
32 StringUtils.lines("inlining", "InnerNoPrivAccess");
33 private final String NEST_SUBCLASS_MAIN_EXPECTED_RESULT =
34 StringUtils.lines("inliningSubclass", "InnerNoPrivAccessSubclass");
35 private final String OUTSIDE_WITH_ACCESS_MAIN_EXPECTED_RESULT =
36 StringUtils.lines("OutsideInliningNoAccess", "inlining");
37 private final String OUTSIDE_NO_ACCESS_MAIN_EXPECTED_RESULT =
38 StringUtils.lines("OutsideInliningNoAccess");
39
40 @Parameters(name = "{0}")
41 public static TestParametersCollection data() {
Clément Bérafdced052024-06-06 11:17:38 +020042 return getTestParameters().withCfRuntimesStartingFromIncluding(CfVm.JDK11).build();
clementberad5bd4f32019-05-21 12:22:06 +020043 }
44
45 @Test
46 public void testClassMergeAcrossTwoNests() throws Exception {
47 // Potentially merge classes from one nest with classes from another nest.
48 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020049 new Class<?>[] {NEST_MAIN_CLASS}, new String[] {NEST_MAIN_EXPECTED_RESULT});
clementberad5bd4f32019-05-21 12:22:06 +020050 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020051 new Class<?>[] {NEST_SUBCLASS_MAIN_CLASS},
52 new String[] {NEST_SUBCLASS_MAIN_EXPECTED_RESULT});
clementberad5bd4f32019-05-21 12:22:06 +020053 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020054 new Class<?>[] {NEST_MAIN_CLASS, NEST_SUBCLASS_MAIN_CLASS},
clementberad5bd4f32019-05-21 12:22:06 +020055 new String[] {NEST_MAIN_EXPECTED_RESULT, NEST_SUBCLASS_MAIN_EXPECTED_RESULT});
56 }
57
58 @Test
59 public void testClassMergeAcrossNestAndNonNest() throws Exception {
60 // Potentially merge classes from a nest with non nest classes.
61 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020062 new Class<?>[] {
clementberad5bd4f32019-05-21 12:22:06 +020063 NEST_MAIN_CLASS, OUTSIDE_NO_ACCESS_MAIN_CLASS, OUTSIDE_WITH_ACCESS_MAIN_CLASS
64 },
65 new String[] {
66 NEST_MAIN_EXPECTED_RESULT,
67 OUTSIDE_NO_ACCESS_MAIN_EXPECTED_RESULT,
68 OUTSIDE_WITH_ACCESS_MAIN_EXPECTED_RESULT
69 });
70 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020071 new Class<?>[] {OUTSIDE_NO_ACCESS_MAIN_CLASS},
clementberad5bd4f32019-05-21 12:22:06 +020072 new String[] {OUTSIDE_NO_ACCESS_MAIN_EXPECTED_RESULT});
73 testClassMergeAcrossNest(
Clément Bérafdced052024-06-06 11:17:38 +020074 new Class<?>[] {OUTSIDE_WITH_ACCESS_MAIN_CLASS},
clementberad5bd4f32019-05-21 12:22:06 +020075 new String[] {OUTSIDE_WITH_ACCESS_MAIN_EXPECTED_RESULT});
76 }
77
Clément Bérafdced052024-06-06 11:17:38 +020078 public void testClassMergeAcrossNest(Class<?>[] mainClasses, String[] expectedResults)
clementberad5bd4f32019-05-21 12:22:06 +020079 throws Exception {
clementberad5bd4f32019-05-21 12:22:06 +020080 R8TestCompileResult compileResult =
Clément Bérafdced052024-06-06 11:17:38 +020081 testForR8(parameters.getBackend())
82 .apply(
83 b -> {
84 for (Class<?> clazz : mainClasses) {
85 b.addKeepMainRule(clazz);
86 }
87 })
clementberad5bd4f32019-05-21 12:22:06 +020088 .addOptionsModification(
89 options -> {
90 // Disable optimizations else additional classes are removed since they become
91 // unused.
clementberad5bd4f32019-05-21 12:22:06 +020092 options.enableClassInlining = false;
clementbera8b50edc2019-05-23 09:30:59 +020093 options.enableNestReduction = false;
clementberad5bd4f32019-05-21 12:22:06 +020094 })
Rudi Horn6b53ef22020-09-29 07:27:50 +000095 .enableInliningAnnotations()
Clément Bérafdced052024-06-06 11:17:38 +020096 .addProgramClassesAndInnerClasses(
97 List.of(
98 NEST_MAIN_CLASS,
99 NEST_SUBCLASS_MAIN_CLASS,
100 OUTSIDE_WITH_ACCESS_MAIN_CLASS,
101 OUTSIDE_NO_ACCESS_MAIN_CLASS))
Christoffer Quist Adamsene1575c32021-02-18 10:23:19 +0100102 .applyIf(parameters.isCfRuntime(), Jdk9TestUtils.addJdk9LibraryFiles(temp))
Morten Krogh-Jespersenf6625e92021-02-24 11:44:17 +0100103 .addKeepPackageNamesRule("nesthostexample")
clementberad5bd4f32019-05-21 12:22:06 +0200104 .compile()
105 .inspect(NestAttributesUpdateTest::assertNestAttributesCorrect);
106 for (int i = 0; i < mainClasses.length; i++) {
107 compileResult
108 .run(parameters.getRuntime(), mainClasses[i])
109 .assertSuccessWithOutput(expectedResults[i]);
110 }
111 }
112}