24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 8003280 |
26 * @bug 8003280 |
27 * @summary Add lambda tests |
27 * @summary Add lambda tests |
28 * Test bridge methods in certain SAM conversion |
28 * Test bridge methods in certain SAM conversion |
|
29 * Tests that jdk.internal.lambda.disableEagerInitialization=true creates a |
|
30 * get$Lambda method for non-capturing lambdas |
29 * @compile BridgeMethod.java |
31 * @compile BridgeMethod.java |
30 * @run main BridgeMethod |
32 * @run main BridgeMethod |
|
33 * @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true BridgeMethod |
31 */ |
34 */ |
32 |
35 |
33 import java.lang.reflect.Method; |
36 import java.lang.reflect.Method; |
|
37 import java.util.Arrays; |
34 import java.util.HashSet; |
38 import java.util.HashSet; |
35 import java.util.Set; |
39 import java.util.Set; |
36 |
40 |
37 public class BridgeMethod { |
41 public class BridgeMethod { |
38 |
42 |
66 s.add("java.lang.String"); |
70 s.add("java.lang.String"); |
67 s.add("java.lang.Object"); |
71 s.add("java.lang.Object"); |
68 return s; |
72 return s; |
69 } |
73 } |
70 |
74 |
|
75 private static Set<String> allowedMethods() { |
|
76 Set<String> s = new HashSet<>(); |
|
77 s.add("m"); |
|
78 if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) { |
|
79 s.add("get$Lambda"); |
|
80 } |
|
81 return s; |
|
82 } |
|
83 |
|
84 private static boolean matchingMethodNames(Method[] methods) { |
|
85 Set<String> methodNames = new HashSet<>(); |
|
86 for (Method m : methods) { |
|
87 methodNames.add(m.getName()); |
|
88 } |
|
89 return methodNames.equals(allowedMethods()); |
|
90 } |
|
91 |
71 public static void main(String[] args) { |
92 public static void main(String[] args) { |
72 L la = BridgeMethod::bar; //static reference |
93 L la = BridgeMethod::bar; //static reference |
73 la.m("hi"); |
94 la.m("hi"); |
74 Class<? extends L> c1 = la.getClass(); |
95 Class<? extends L> c1 = la.getClass(); |
75 Method[] methods = c1.getDeclaredMethods(); |
96 Method[] methods = c1.getDeclaredMethods(); |
|
97 assertTrue(matchingMethodNames(methods)); |
76 Set<String> types = setOfStringObject(); |
98 Set<String> types = setOfStringObject(); |
77 System.out.println("methods in SAM conversion of L:"); |
99 System.out.println("methods in SAM conversion of L:"); |
78 for(Method m : methods) { |
100 for(Method m : methods) { |
79 System.out.println(m.toGenericString()); |
101 if (m.getName().equals("m")) { |
80 assertTrue(m.getName().equals("m")); |
102 System.out.println(m.toGenericString()); |
81 Class[] parameterTypes = m.getParameterTypes(); |
103 Class[] parameterTypes = m.getParameterTypes(); |
82 assertTrue(parameterTypes.length == 1); |
104 assertTrue(parameterTypes.length == 1); |
83 assertTrue(types.remove(parameterTypes[0].getName())); |
105 assertTrue(types.remove(parameterTypes[0].getName())); |
|
106 } |
84 } |
107 } |
85 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); |
108 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String"))); |
86 |
109 |
87 KM km = BridgeMethod::bar; |
110 KM km = BridgeMethod::bar; |
88 //km.m("hi"); //will be uncommented when CR7028808 fixed |
111 //km.m("hi"); //will be uncommented when CR7028808 fixed |
89 Class<? extends KM> c2 = km.getClass(); |
112 Class<? extends KM> c2 = km.getClass(); |
90 methods = c2.getDeclaredMethods(); |
113 methods = c2.getDeclaredMethods(); |
|
114 assertTrue(matchingMethodNames(methods)); |
91 types = setOfStringObject(); |
115 types = setOfStringObject(); |
92 System.out.println("methods in SAM conversion of KM:"); |
116 System.out.println("methods in SAM conversion of KM:"); |
93 for(Method m : methods) { |
117 for(Method m : methods) { |
94 System.out.println(m.toGenericString()); |
118 if (m.getName().equals("m")) { |
95 assertTrue(m.getName().equals("m")); |
119 System.out.println(m.toGenericString()); |
96 Class<?>[] parameterTypes = m.getParameterTypes(); |
120 Class<?>[] parameterTypes = m.getParameterTypes(); |
97 assertTrue(parameterTypes.length == 1); |
121 assertTrue(parameterTypes.length == 1); |
98 assertTrue(types.remove(parameterTypes[0].getName())); |
122 assertTrue(types.remove(parameterTypes[0].getName())); |
|
123 } |
99 } |
124 } |
100 assertTrue(types.isEmpty()); |
125 assertTrue(types.isEmpty()); |
101 |
126 |
102 N n = new BridgeMethod()::moo; //instance reference |
127 N n = new BridgeMethod()::moo; //instance reference |
103 assertTrue( n.m().equals("moo") ); |
128 assertTrue( n.m().equals("moo") ); |