Christoffer Quist Adamsen | e494526 | 2018-06-20 10:45:33 +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 | public class LambdaRewritingTest { |
| 8 | |
| 9 | public static void main(String[] args) { |
| 10 | Interface obj = new InterfaceImpl(); |
| 11 | |
| 12 | // Leads to an invoke-custom instruction that mentions the type of `obj` since it is captured. |
| 13 | invoke(() -> obj.foo()); |
Ian Zerny | b3a7299 | 2020-01-22 10:12:03 +0100 | [diff] [blame] | 14 | |
| 15 | FunctionImpl functionImpl = new FunctionImpl(); |
| 16 | if (System.currentTimeMillis() < 0) { |
| 17 | System.out.println(functionImpl); |
| 18 | } |
Christoffer Quist Adamsen | e494526 | 2018-06-20 10:45:33 +0200 | [diff] [blame] | 19 | } |
| 20 | |
Ian Zerny | b3a7299 | 2020-01-22 10:12:03 +0100 | [diff] [blame] | 21 | @NeverInline |
Christoffer Quist Adamsen | e494526 | 2018-06-20 10:45:33 +0200 | [diff] [blame] | 22 | private static void invoke(Function f) { |
| 23 | f.accept(); |
| 24 | } |
| 25 | |
Ian Zerny | b3a7299 | 2020-01-22 10:12:03 +0100 | [diff] [blame] | 26 | // Cannot be merged as it has two subtypes: FunctionImpl and a lambda. |
Christoffer Quist Adamsen | e494526 | 2018-06-20 10:45:33 +0200 | [diff] [blame] | 27 | public interface Function { |
| 28 | |
| 29 | void accept(); |
| 30 | } |
| 31 | |
Christoffer Quist Adamsen | 943db6e | 2018-06-25 12:43:18 +0200 | [diff] [blame] | 32 | public static class FunctionImpl implements Function { |
| 33 | |
| 34 | @Override |
| 35 | public void accept() { |
| 36 | System.out.println("In FunctionImpl.accept()"); |
| 37 | } |
| 38 | } |
| 39 | |
Christoffer Quist Adamsen | e494526 | 2018-06-20 10:45:33 +0200 | [diff] [blame] | 40 | // Will be merged into InterfaceImpl. |
| 41 | public interface Interface { |
| 42 | |
| 43 | void foo(); |
| 44 | } |
| 45 | |
| 46 | public static class InterfaceImpl implements Interface { |
| 47 | |
| 48 | @Override |
| 49 | public void foo() { |
| 50 | System.out.println("In InterfaceImpl.foo()"); |
| 51 | } |
| 52 | } |
| 53 | } |