clementbera | 02d4cf2 | 2019-06-03 16:02:58 +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. |
| 4 | |
| 5 | package nesthostexample; |
| 6 | |
| 7 | public class NestPvtMethodCallInlined { |
| 8 | |
| 9 | public static class Inner { |
| 10 | |
| 11 | public String methodWithPvtCallToInline() { |
| 12 | return notInlinedPvtCall(); |
| 13 | } |
| 14 | |
| 15 | @NeverInline |
| 16 | private String notInlinedPvtCall() { |
| 17 | return "notInlinedPvtCallInner"; |
| 18 | } |
| 19 | |
| 20 | private String nestPvtCallToInline() { |
| 21 | return "nestPvtCallToInlineInner"; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public interface InnerInterface { |
| 26 | |
| 27 | default String methodWithPvtCallToInline() { |
| 28 | return notInlinedPvtCall(); |
| 29 | } |
| 30 | |
| 31 | @NeverInline |
| 32 | private String notInlinedPvtCall() { |
| 33 | return "notInlinedPvtCallInnerInterface"; |
| 34 | } |
| 35 | |
| 36 | private String nestPvtCallToInline() { |
| 37 | return "nestPvtCallToInlineInnerInterface"; |
| 38 | } |
| 39 | |
| 40 | default String dispatch(InnerSub sub) { |
| 41 | return sub.notInlinedPvtCall(); |
| 42 | } |
| 43 | |
| 44 | @NeverInline |
| 45 | default String dispatchInlining(InnerSub iSub) { |
| 46 | return iSub.dispatch(this); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static class InnerInterfaceImpl implements InnerInterface {} |
| 51 | |
| 52 | public static class InnerSub extends Inner { |
| 53 | |
| 54 | @NeverInline |
| 55 | public String dispatchInlining(InnerInterface impl) { |
| 56 | return impl.dispatch(this); |
| 57 | } |
| 58 | |
| 59 | public String dispatch(InnerInterface itf) { |
| 60 | return itf.notInlinedPvtCall(); |
| 61 | } |
| 62 | |
| 63 | @NeverInline |
| 64 | private String notInlinedPvtCall() { |
| 65 | return "notInlinedPvtCallInnerSub"; |
| 66 | } |
| 67 | |
| 68 | private String nestPvtCallToInline() { |
| 69 | return "nestPvtCallToInlineInnerSub"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public static void main(String[] args) { |
| 74 | Inner i = new Inner(); |
| 75 | InnerSub iSub = new InnerSub(); |
| 76 | InnerInterface impl = new InnerInterfaceImpl(); |
| 77 | |
| 78 | // Inlining through nest access (invoke virtual/interface). |
| 79 | System.out.println(i.nestPvtCallToInline()); |
| 80 | System.out.println(impl.nestPvtCallToInline()); |
| 81 | |
| 82 | // Inlining transformations. |
| 83 | // Invoke direct -> invoke virtual. |
| 84 | System.out.println(i.methodWithPvtCallToInline()); |
| 85 | // Invoke interface -> invoke virtual. |
| 86 | System.out.println(impl.methodWithPvtCallToInline()); |
| 87 | // Invoke virtual -> invoke direct. |
| 88 | System.out.println(iSub.dispatchInlining(impl)); |
| 89 | // Invoke interface -> invoke direct. |
| 90 | System.out.println(impl.dispatchInlining(iSub)); |
| 91 | |
| 92 | // Inheritance + invoke virtual and nest access. |
| 93 | // This may mess up lookup logic. |
| 94 | System.out.println(iSub.nestPvtCallToInline()); |
| 95 | System.out.println(((Inner) iSub).nestPvtCallToInline()); |
| 96 | } |
| 97 | } |