test/tools/javac/lambda/methodReference/MethodRef7.java

changeset 1415
01c9d4161882
parent 0
959103a6100f
equal deleted inserted replaced
1414:843d3b191773 1415:01c9d4161882
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @bug 8003280
27 * @summary Add lambda tests
28 * Test that parameter types are inferred from SAM descriptor when method parameters are elided,
29 with different types of method references
30 * @compile MethodRef7.java
31 * @run main MethodRef7
32 */
33
34 public class MethodRef7 {
35
36 static interface A {void m();}
37
38 static interface A2 {void m(int n);}
39
40 static interface B {String m();}
41
42 static interface B2 {String m(int n);}
43
44 static interface C {String m(MethodRef7 mr);}
45
46 static interface C2 {String m(MethodRef7 mr, int n);}
47
48 static interface D {Fee<String> m();}
49
50 static interface D2 {Fee<String> m(String s);}
51
52 static class Fee<T> {
53
54 public Fee() {
55 System.out.println("Fee<T> instantiated");
56 }
57
58 public Fee(String s) {
59 System.out.println("Fee<T> instantiated: " + s);
60 }
61 }
62
63 private static void assertTrue(boolean cond) {
64 if (!cond)
65 throw new AssertionError();
66 }
67
68 static void bar() {
69 System.out.println("MethodRef_neg1.bar()");
70 }
71
72 static void bar(int x) {
73 System.out.println("MethodRef_neg1.bar(int) " + x);
74 }
75
76 String wahoo() {
77 return "wahoo";
78 }
79
80 String wahoo(int x) {
81 return "wahoo " + x;
82 }
83
84 public static void main(String[] args) {
85
86 A a = MethodRef7::bar; //static reference to bar()
87 a.m();
88 A2 a2 = MethodRef7::bar; //static reference to bar(int x)
89 a2.m(10);
90
91 MethodRef7 mr = new MethodRef7();
92 B b = mr::wahoo; //instance reference to wahoo()
93 assertTrue(b.m().equals("wahoo"));
94 B2 b2 = mr::wahoo; //instance reference to wahoo(int x)
95 assertTrue(b2.m(1).equals("wahoo 1"));
96
97 C c = MethodRef7::wahoo; //unbound reference to wahoo()
98 assertTrue(c.m(mr).equals("wahoo"));
99 C2 c2 = MethodRef7::wahoo; //unbound reference to wahoo(int x)
100 assertTrue(c2.m(mr, 2).equals("wahoo 2"));
101
102 D d = Fee<String>::new; //constructor reference to Fee()
103 D2 d2 = Fee<String>::new; //constructor reference to Fee(String s)
104 }
105 }

mercurial