author | vjovanovic |
Mon, 28 Oct 2019 15:03:36 +0100 | |
changeset 58871 | 27c2d2a4b695 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
14547 | 1 |
/* |
22448
a85fbad9d687
8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
ksrini
parents:
18730
diff
changeset
|
2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
14547 | 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 bridge methods for certain SAM conversions |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
29 |
* Tests that jdk.internal.lambda.disableEagerInitialization=true creates a |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
30 |
* get$Lambda method for non-capturing lambdas |
14547 | 31 |
* @compile LambdaTest6.java |
32 |
* @run main LambdaTest6 |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
33 |
* @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true LambdaTest6 |
14547 | 34 |
*/ |
35 |
||
36 |
import java.lang.reflect.Method; |
|
37 |
import java.util.HashSet; |
|
38 |
import java.util.Set; |
|
39 |
||
40 |
public class LambdaTest6<T> { |
|
41 |
||
42 |
interface H {Object m();} |
|
43 |
||
44 |
interface K<U> {void m(U element);} |
|
45 |
||
46 |
interface L extends K<String> {} //generic substitution |
|
47 |
||
48 |
interface M {void m(String s);} |
|
49 |
||
50 |
interface KM extends K<String>, M{} //generic substitution |
|
51 |
||
52 |
interface N extends H {String m();} //covariant return |
|
53 |
||
54 |
private static void assertTrue(boolean b) { |
|
55 |
if(!b) |
|
56 |
throw new AssertionError(); |
|
57 |
} |
|
58 |
||
59 |
private Set<String> setOfStringObject() { |
|
60 |
Set<String> s = new HashSet<>(); |
|
61 |
s.add("java.lang.String"); |
|
62 |
s.add("java.lang.Object"); |
|
63 |
return s; |
|
64 |
} |
|
65 |
||
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
66 |
private static Set<String> allowedMethods() { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
67 |
Set<String> s = new HashSet<>(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
68 |
s.add("m"); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
69 |
if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
70 |
s.add("get$Lambda"); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
71 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
72 |
return s; |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
73 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
74 |
|
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
75 |
private static boolean matchingMethodNames(Method[] methods) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
76 |
Set<String> methodNames = new HashSet<>(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
77 |
for (Method m : methods) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
78 |
methodNames.add(m.getName()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
79 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
80 |
return methodNames.equals(allowedMethods()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
81 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
82 |
|
14547 | 83 |
private void test1() |
84 |
{ |
|
85 |
L la = s -> { }; |
|
86 |
la.m("hi"); |
|
87 |
Class<? extends L> c1 = la.getClass(); |
|
88 |
Method[] methods = c1.getDeclaredMethods(); |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
89 |
assertTrue(matchingMethodNames(methods)); |
14547 | 90 |
Set<String> types = setOfStringObject(); |
91 |
for(Method m : methods) { |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
92 |
if ("m".equals(m.getName())) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
93 |
Class[] parameterTypes = m.getParameterTypes(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
94 |
assertTrue(parameterTypes.length == 1); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
95 |
assertTrue(types.remove(parameterTypes[0].getName())); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
96 |
} |
14547 | 97 |
} |
98 |
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); |
|
99 |
} |
|
100 |
||
101 |
private void test2() |
|
102 |
{ |
|
103 |
KM km = s -> { }; |
|
104 |
//km.m("hi"); |
|
105 |
Class<? extends KM> c2 = km.getClass(); |
|
106 |
Method[] methods = c2.getDeclaredMethods(); |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
107 |
assertTrue(matchingMethodNames(methods)); |
14547 | 108 |
Set<String> types = setOfStringObject(); |
109 |
for(Method m : methods) { |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
110 |
if ("m".equals(m.getName())) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
111 |
Class[] parameterTypes = m.getParameterTypes(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
112 |
assertTrue(parameterTypes.length == 1); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
113 |
assertTrue(types.remove(parameterTypes[0].getName())); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
114 |
} |
14547 | 115 |
} |
116 |
assertTrue(types.isEmpty()); |
|
117 |
} |
|
118 |
||
119 |
private void test3() |
|
120 |
{ |
|
121 |
N na = ()-> "hi"; |
|
122 |
assertTrue( na.m().equals("hi") ); |
|
123 |
assertTrue( ((H)na).m().equals("hi") ); |
|
124 |
Class<? extends N> c3 = na.getClass(); |
|
125 |
Method[] methods = c3.getDeclaredMethods(); |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
126 |
assertTrue(matchingMethodNames(methods)); |
14547 | 127 |
Set<String> types = setOfStringObject(); |
128 |
for(Method m : methods) { |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
129 |
if ("m".equals(m.getName())) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
130 |
Class returnType = m.getReturnType(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
131 |
assertTrue(types.remove(returnType.getName())); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
132 |
} |
14547 | 133 |
} |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
134 |
assertTrue(types.size() == 1); //there's a bridge |
14547 | 135 |
} |
136 |
||
137 |
||
138 |
public static void main(String[] args) { |
|
139 |
LambdaTest6 test = new LambdaTest6(); |
|
140 |
test.test1(); |
|
141 |
test.test2(); |
|
142 |
test.test3(); |
|
143 |
} |
|
144 |
} |