Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 1 | // 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. |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 4 | |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 5 | package nesthostexample; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 6 | |
Christoffer Quist Adamsen | e1575c3 | 2021-02-18 10:23:19 +0100 | [diff] [blame] | 7 | import com.android.tools.r8.Jdk9TestUtils; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 8 | import com.android.tools.r8.R8TestCompileResult; |
| 9 | import com.android.tools.r8.TestBase; |
| 10 | import com.android.tools.r8.TestParameters; |
| 11 | import com.android.tools.r8.TestParametersCollection; |
| 12 | import com.android.tools.r8.TestRuntime.CfVm; |
| 13 | import com.android.tools.r8.utils.StringUtils; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 14 | import java.util.List; |
| 15 | import org.junit.Test; |
| 16 | import org.junit.runner.RunWith; |
| 17 | import org.junit.runners.Parameterized; |
Christoffer Quist Adamsen | 42e8bde | 2023-01-31 11:43:46 +0100 | [diff] [blame] | 18 | import org.junit.runners.Parameterized.Parameter; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 19 | import org.junit.runners.Parameterized.Parameters; |
| 20 | |
| 21 | @RunWith(Parameterized.class) |
| 22 | public class NestClassMergingTest extends TestBase { |
| 23 | |
Christoffer Quist Adamsen | 42e8bde | 2023-01-31 11:43:46 +0100 | [diff] [blame] | 24 | @Parameter(0) |
| 25 | public TestParameters parameters; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 26 | |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 27 | 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; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 31 | 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éra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 42 | return getTestParameters().withCfRuntimesStartingFromIncluding(CfVm.JDK11).build(); |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 43 | } |
| 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éra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 49 | new Class<?>[] {NEST_MAIN_CLASS}, new String[] {NEST_MAIN_EXPECTED_RESULT}); |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 50 | testClassMergeAcrossNest( |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 51 | new Class<?>[] {NEST_SUBCLASS_MAIN_CLASS}, |
| 52 | new String[] {NEST_SUBCLASS_MAIN_EXPECTED_RESULT}); |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 53 | testClassMergeAcrossNest( |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 54 | new Class<?>[] {NEST_MAIN_CLASS, NEST_SUBCLASS_MAIN_CLASS}, |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 55 | 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éra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 62 | new Class<?>[] { |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 63 | 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éra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 71 | new Class<?>[] {OUTSIDE_NO_ACCESS_MAIN_CLASS}, |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 72 | new String[] {OUTSIDE_NO_ACCESS_MAIN_EXPECTED_RESULT}); |
| 73 | testClassMergeAcrossNest( |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 74 | new Class<?>[] {OUTSIDE_WITH_ACCESS_MAIN_CLASS}, |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 75 | new String[] {OUTSIDE_WITH_ACCESS_MAIN_EXPECTED_RESULT}); |
| 76 | } |
| 77 | |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 78 | public void testClassMergeAcrossNest(Class<?>[] mainClasses, String[] expectedResults) |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 79 | throws Exception { |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 80 | R8TestCompileResult compileResult = |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 81 | testForR8(parameters.getBackend()) |
| 82 | .apply( |
| 83 | b -> { |
| 84 | for (Class<?> clazz : mainClasses) { |
| 85 | b.addKeepMainRule(clazz); |
| 86 | } |
| 87 | }) |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 88 | .addOptionsModification( |
| 89 | options -> { |
| 90 | // Disable optimizations else additional classes are removed since they become |
| 91 | // unused. |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 92 | options.enableClassInlining = false; |
clementbera | 8b50edc | 2019-05-23 09:30:59 +0200 | [diff] [blame] | 93 | options.enableNestReduction = false; |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 94 | }) |
Rudi Horn | 6b53ef2 | 2020-09-29 07:27:50 +0000 | [diff] [blame] | 95 | .enableInliningAnnotations() |
Clément Béra | fdced05 | 2024-06-06 11:17:38 +0200 | [diff] [blame] | 96 | .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 Adamsen | e1575c3 | 2021-02-18 10:23:19 +0100 | [diff] [blame] | 102 | .applyIf(parameters.isCfRuntime(), Jdk9TestUtils.addJdk9LibraryFiles(temp)) |
Morten Krogh-Jespersen | f6625e9 | 2021-02-24 11:44:17 +0100 | [diff] [blame] | 103 | .addKeepPackageNamesRule("nesthostexample") |
clementbera | d5bd4f3 | 2019-05-21 12:22:06 +0200 | [diff] [blame] | 104 | .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 | } |