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:
20603
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 in certain SAM conversion |
|
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 BridgeMethod.java |
32 |
* @run main BridgeMethod |
|
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 BridgeMethod |
14547 | 34 |
*/ |
35 |
||
36 |
import java.lang.reflect.Method; |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
37 |
import java.util.Arrays; |
14547 | 38 |
import java.util.HashSet; |
39 |
import java.util.Set; |
|
40 |
||
41 |
public class BridgeMethod { |
|
42 |
||
43 |
interface H {Object m();} |
|
44 |
||
45 |
interface K<T> {void m(T t);} |
|
46 |
||
47 |
interface L extends K<String> {} //generic substitution |
|
48 |
||
49 |
interface M {void m(String s);} |
|
50 |
||
51 |
interface KM extends K<String>, M{} //generic substitution |
|
52 |
||
53 |
interface N extends H {String m();} //covariant return |
|
54 |
||
55 |
private static void assertTrue(boolean cond) { |
|
56 |
if (!cond) |
|
57 |
throw new AssertionError(); |
|
58 |
} |
|
59 |
||
60 |
static void bar(String s) { |
|
61 |
System.out.println("BridgeMethod.bar(String) " + s); |
|
62 |
} |
|
63 |
||
64 |
String moo() { |
|
65 |
return "moo"; |
|
66 |
} |
|
67 |
||
68 |
private static Set<String> setOfStringObject() { |
|
69 |
Set<String> s = new HashSet<>(); |
|
70 |
s.add("java.lang.String"); |
|
71 |
s.add("java.lang.Object"); |
|
72 |
return s; |
|
73 |
} |
|
74 |
||
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
75 |
private static Set<String> allowedMethods() { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
76 |
Set<String> s = new HashSet<>(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
77 |
s.add("m"); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
78 |
if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
79 |
s.add("get$Lambda"); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
80 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
81 |
return s; |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
82 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
83 |
|
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
84 |
private static boolean matchingMethodNames(Method[] methods) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
85 |
Set<String> methodNames = new HashSet<>(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
86 |
for (Method m : methods) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
87 |
methodNames.add(m.getName()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
88 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
89 |
return methodNames.equals(allowedMethods()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
90 |
} |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
91 |
|
14547 | 92 |
public static void main(String[] args) { |
93 |
L la = BridgeMethod::bar; //static reference |
|
94 |
la.m("hi"); |
|
95 |
Class<? extends L> c1 = la.getClass(); |
|
96 |
Method[] methods = c1.getDeclaredMethods(); |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
97 |
assertTrue(matchingMethodNames(methods)); |
14547 | 98 |
Set<String> types = setOfStringObject(); |
99 |
System.out.println("methods in SAM conversion of L:"); |
|
100 |
for(Method m : methods) { |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
101 |
if (m.getName().equals("m")) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
102 |
System.out.println(m.toGenericString()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
103 |
Class[] parameterTypes = m.getParameterTypes(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
104 |
assertTrue(parameterTypes.length == 1); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
105 |
assertTrue(types.remove(parameterTypes[0].getName())); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
106 |
} |
14547 | 107 |
} |
108 |
assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); |
|
109 |
||
110 |
KM km = BridgeMethod::bar; |
|
111 |
//km.m("hi"); //will be uncommented when CR7028808 fixed |
|
112 |
Class<? extends KM> c2 = km.getClass(); |
|
113 |
methods = c2.getDeclaredMethods(); |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
114 |
assertTrue(matchingMethodNames(methods)); |
14547 | 115 |
types = setOfStringObject(); |
116 |
System.out.println("methods in SAM conversion of KM:"); |
|
117 |
for(Method m : methods) { |
|
58871
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
118 |
if (m.getName().equals("m")) { |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
119 |
System.out.println(m.toGenericString()); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
120 |
Class<?>[] parameterTypes = m.getParameterTypes(); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
121 |
assertTrue(parameterTypes.length == 1); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
122 |
assertTrue(types.remove(parameterTypes[0].getName())); |
27c2d2a4b695
8232806: Introduce a system property to disable eager lambda initialization
vjovanovic
parents:
47216
diff
changeset
|
123 |
} |
14547 | 124 |
} |
125 |
assertTrue(types.isEmpty()); |
|
126 |
||
127 |
N n = new BridgeMethod()::moo; //instance reference |
|
128 |
assertTrue( n.m().equals("moo") ); |
|
129 |
assertTrue( ((H)n).m().equals("moo") ); |
|
130 |
Class<? extends N> c3 = n.getClass(); |
|
131 |
methods = c3.getDeclaredMethods(); |
|
132 |
types = setOfStringObject(); |
|
133 |
System.out.println("methods in SAM conversion of N:"); |
|
134 |
for(Method m : methods) { |
|
135 |
System.out.println(m.toGenericString()); |
|
20603
1cba7386cfd9
8025548: langtools test tools/javac/lambda/methodReference/BridgeMethod.java incorrectly assumes no other methods generated in lambda class
rfield
parents:
18730
diff
changeset
|
136 |
if (m.getName().equals("m")) { |
1cba7386cfd9
8025548: langtools test tools/javac/lambda/methodReference/BridgeMethod.java incorrectly assumes no other methods generated in lambda class
rfield
parents:
18730
diff
changeset
|
137 |
Class<?> returnType = m.getReturnType(); |
1cba7386cfd9
8025548: langtools test tools/javac/lambda/methodReference/BridgeMethod.java incorrectly assumes no other methods generated in lambda class
rfield
parents:
18730
diff
changeset
|
138 |
assertTrue(types.remove(returnType.getName())); |
1cba7386cfd9
8025548: langtools test tools/javac/lambda/methodReference/BridgeMethod.java incorrectly assumes no other methods generated in lambda class
rfield
parents:
18730
diff
changeset
|
139 |
} |
14547 | 140 |
} |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18412
diff
changeset
|
141 |
assertTrue(types.size() == 1); //there's a bridge |
14547 | 142 |
} |
143 |
} |