Christoffer Quist Adamsen | 10c8a16 | 2018-06-11 09:53:03 +0200 | [diff] [blame] | 1 | // Copyright (c) 2018, 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. |
| 4 | |
| 5 | package classmerging; |
| 6 | |
| 7 | import classmerging.pkg.SimpleInterfaceImplRetriever; |
| 8 | |
| 9 | public class SimpleInterfaceAccessTest { |
| 10 | public static void main(String[] args) { |
Christoffer Quist Adamsen | 510b799 | 2018-06-20 12:53:32 +0200 | [diff] [blame] | 11 | // Without access modifications, it is not possible to merge the interface SimpleInterface into |
| 12 | // SimpleInterfaceImpl, since this would lead to an illegal class access here. |
| 13 | SimpleInterface x = SimpleInterfaceImplRetriever.getSimpleInterfaceImpl(); |
| 14 | x.foo(); |
| 15 | |
| 16 | // Without access modifications, it is not possible to merge the interface OtherSimpleInterface |
| 17 | // into OtherSimpleInterfaceImpl, since this could lead to an illegal class access if another |
| 18 | // package references OtherSimpleInterface. |
| 19 | OtherSimpleInterface y = new OtherSimpleInterfaceImpl(); |
| 20 | y.bar(); |
Christoffer Quist Adamsen | bfdbe60 | 2019-10-18 12:03:31 +0200 | [diff] [blame] | 21 | |
| 22 | // Ensure that the instantiations are not dead code eliminated. |
| 23 | escape(x); |
| 24 | escape(y); |
| 25 | } |
| 26 | |
| 27 | @NeverInline |
| 28 | static void escape(Object o) { |
| 29 | if (System.currentTimeMillis() < 0) { |
| 30 | System.out.println(o); |
| 31 | } |
Christoffer Quist Adamsen | 510b799 | 2018-06-20 12:53:32 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Should only be merged into OtherSimpleInterfaceImpl if access modifications are allowed. |
Christoffer Quist Adamsen | 6fe706d | 2021-05-21 08:49:12 +0200 | [diff] [blame] | 35 | @NoHorizontalClassMerging |
Christoffer Quist Adamsen | 510b799 | 2018-06-20 12:53:32 +0200 | [diff] [blame] | 36 | public interface SimpleInterface { |
| 37 | |
| 38 | void foo(); |
| 39 | } |
| 40 | |
| 41 | // Should only be merged into OtherSimpleInterfaceImpl if access modifications are allowed. |
Christoffer Quist Adamsen | 6fe706d | 2021-05-21 08:49:12 +0200 | [diff] [blame] | 42 | @NoHorizontalClassMerging |
Christoffer Quist Adamsen | 510b799 | 2018-06-20 12:53:32 +0200 | [diff] [blame] | 43 | public interface OtherSimpleInterface { |
| 44 | |
| 45 | void bar(); |
| 46 | } |
| 47 | |
Christoffer Quist Adamsen | 801af25 | 2023-06-21 14:20:08 +0200 | [diff] [blame] | 48 | @NoAccessModification |
Christoffer Quist Adamsen | 510b799 | 2018-06-20 12:53:32 +0200 | [diff] [blame] | 49 | private static class OtherSimpleInterfaceImpl implements OtherSimpleInterface { |
| 50 | |
| 51 | @Override |
| 52 | public void bar() { |
| 53 | System.out.println("In bar on OtherSimpleInterfaceImpl"); |
| 54 | } |
Christoffer Quist Adamsen | 10c8a16 | 2018-06-11 09:53:03 +0200 | [diff] [blame] | 55 | } |
| 56 | } |