blob: 007cd0cd3dcd33e0c388441835f0681f98734b7a [file] [log] [blame]
Sebastien Hertz964c5c22017-05-23 15:22:23 +02001// Copyright (c) 2017, 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
5public class DebugLambda {
6
7 interface I {
8 int getInt();
9 }
10
11 private static void printInt(I i) {
12 System.out.println(i.getInt());
13 }
14
15 public static void testLambda(int i, int j) {
16 printInt(() -> i + j);
17 }
18
Sebastien Hertzca758652017-06-16 18:18:04 +020019 private static void printInt2(I i) {
20 System.out.println(i.getInt());
Sebastien Hertz964c5c22017-05-23 15:22:23 +020021 }
22
Sebastien Hertzca758652017-06-16 18:18:04 +020023 public static void testLambdaWithMethodReference() {
24 printInt2(DebugLambda::returnOne);
25 }
26
27 private static int returnOne() {
28 return 1;
29 }
30
31 private static void printInt3(BinaryOpInterface i, int a, int b) {
32 System.out.println(i.binaryOp(a, b));
33 }
34
35 public static void testLambdaWithArguments(int i, int j) {
36 printInt3((a, b) -> {
37 return a + b;
38 }, i, j);
39 }
40
41 interface ObjectProvider {
42 Object foo(String a, String b, String c);
43 }
44
45 private static void testLambdaWithMethodReferenceAndConversion(ObjectProvider objectProvider) {
46 System.out.println(objectProvider.foo("A", "B", "C"));
47 }
48
49 public static void main(String[] args) {
50 DebugLambda.testLambda(5, 10);
51 DebugLambda.testLambdaWithArguments(5, 10);
52 DebugLambda.testLambdaWithMethodReference();
53 DebugLambda.testLambdaWithMethodReferenceAndConversion(DebugLambda::concatObjects);
54 }
55
56 private static Object concatObjects(Object... objects) {
57 StringBuilder sb = new StringBuilder();
58 for (Object o : objects) {
59 sb.append(o.toString());
60 }
61 return sb.toString();
62 }
63
64 interface BinaryOpInterface {
65 int binaryOp(int a, int b);
66 }
Sebastien Hertz964c5c22017-05-23 15:22:23 +020067}