clementbera | 0fe940d | 2019-04-23 12:45:18 +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 | |
clementbera | be19d30 | 2019-05-23 17:23:21 +0200 | [diff] [blame] | 5 | package nesthostexample; |
clementbera | 0fe940d | 2019-04-23 12:45:18 +0200 | [diff] [blame] | 6 | |
| 7 | public class BasicNestHostWithAnonymousInnerClass { |
| 8 | |
| 9 | private String method() { |
| 10 | return "hostMethod"; |
| 11 | } |
| 12 | |
| 13 | private static String staticMethod() { |
| 14 | return "staticHostMethod"; |
| 15 | } |
| 16 | |
| 17 | private String field = "field"; |
| 18 | private static String staticField = "staticField"; |
| 19 | |
| 20 | public static InterfaceForAnonymousClass createAnonymousNestedInstance() { |
| 21 | return new InterfaceForAnonymousClass() { |
| 22 | public String accessOuter(BasicNestHostWithAnonymousInnerClass o) { |
| 23 | return o.field |
| 24 | + o.staticField |
| 25 | + BasicNestHostWithAnonymousInnerClass.staticField |
| 26 | + o.method() |
| 27 | + o.staticMethod() |
| 28 | + BasicNestHostWithAnonymousInnerClass.staticMethod(); |
| 29 | } |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | public interface InterfaceForAnonymousClass { |
| 34 | String accessOuter(BasicNestHostWithAnonymousInnerClass o); |
| 35 | } |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | BasicNestHostWithAnonymousInnerClass outer = new BasicNestHostWithAnonymousInnerClass(); |
| 39 | InterfaceForAnonymousClass anonymousInner = |
| 40 | BasicNestHostWithAnonymousInnerClass.createAnonymousNestedInstance(); |
| 41 | System.out.println(anonymousInner.accessOuter(outer)); |
| 42 | } |
| 43 | } |