Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * Copyright (c) 2013, 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 */
24 /*
25 * @test
26 * @bug 8011738
27 * @author sogoel
28 * @summary Code translation test for Lambda expressions, method references
29 * @run main ByteCodeTest
30 */
32 import com.sun.tools.classfile.Attribute;
33 import com.sun.tools.classfile.BootstrapMethods_attribute;
34 import com.sun.tools.classfile.ClassFile;
35 import com.sun.tools.classfile.ConstantPool;
36 import com.sun.tools.classfile.ConstantPoolException;
37 import com.sun.tools.classfile.ConstantPool.*;
39 import java.io.BufferedWriter;
40 import java.io.File;
41 import java.io.FileWriter;
42 import java.io.IOException;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Map;
51 public class ByteCodeTest {
53 static boolean IS_DEBUG = false;
54 public static void main(String[] args) {
55 File classFile = null;
56 int err = 0;
57 boolean verifyResult = false;
58 for(TestCases tc : TestCases.values()) {
59 classFile = getCompiledFile(tc.name(), tc.srcCode);
60 if(classFile == null) { // either testFile or classFile was not created
61 err++;
62 } else {
63 verifyResult = verifyClassFileAttributes(classFile, tc);
64 if(!verifyResult)
65 System.out.println("Bootstrap class file attributes did not match for " + tc.name());
66 }
67 }
68 if(err > 0)
69 throw new RuntimeException("Found " + err + " found");
70 else
71 System.out.println("Test passed");
72 }
74 private static boolean verifyClassFileAttributes(File classFile, TestCases tc) {
75 ClassFile c = null;
76 try {
77 c = ClassFile.read(classFile);
78 } catch (IOException | ConstantPoolException e) {
79 e.printStackTrace();
80 }
81 ConstantPoolVisitor cpv = new ConstantPoolVisitor(c, c.constant_pool.size());
82 Map<Integer, String> hm = cpv.getBSMMap();
84 List<String> expectedValList = tc.getExpectedArgValues();
85 expectedValList.add(tc.bsmSpecifier.specifier);
86 if(!(hm.values().containsAll(new HashSet<String>(expectedValList)))) {
87 System.out.println("Values do not match");
88 return false;
89 }
90 return true;
91 }
93 private static File getCompiledFile(String fname, String srcString) {
94 File testFile = null, classFile = null;
95 boolean isTestFileCreated = true;
97 try {
98 testFile = writeTestFile(fname+".java", srcString);
99 } catch(IOException ioe) {
100 isTestFileCreated = false;
101 System.err.println("fail to write" + ioe);
102 }
104 if(isTestFileCreated) {
105 try {
106 classFile = compile(testFile);
107 } catch (Error err) {
108 System.err.println("fail compile. Source:\n" + srcString);
109 throw err;
110 }
111 }
112 return classFile;
113 }
115 static File writeTestFile(String fname, String source) throws IOException {
116 File f = new File(fname);
117 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
118 out.println(source);
119 out.close();
120 return f;
121 }
123 static File compile(File f) {
124 int rc = com.sun.tools.javac.Main.compile(new String[] {
125 "-source", "1.8", "-g", f.getPath() });
126 if (rc != 0)
127 throw new Error("compilation failed. rc=" + rc);
128 String path = f.getPath();
129 return new File(path.substring(0, path.length() - 5) + ".class");
130 }
132 static void debugln(String str) {
133 if(IS_DEBUG)
134 System.out.println(str);
135 }
137 enum BSMSpecifier {
138 SPECIFIER1("REF_invokeStatic java/lang/invoke/LambdaMetafactory metaFactory " +
139 "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;" +
140 "Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)" +
141 "Ljava/lang/invoke/CallSite;"),
142 SPECIFIER2("REF_invokeStatic java/lang/invoke/LambdaMetafactory altMetaFactory " +
143 "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;" +
144 "[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;");
146 String specifier;
147 private BSMSpecifier(String specifier) {
148 this.specifier = specifier;
149 }
150 }
152 enum TestCases {
153 // Single line lambda expression
154 TC1("class TC1 {\n" +
155 " public static void main(String[] args) {\n" +
156 " Object o = (Runnable) () -> { System.out.println(\"hi\");};\n" +
157 " }\n"+
158 "}", BSMSpecifier.SPECIFIER1) {
160 @Override
161 List<String> getExpectedArgValues() {
162 List<String> valList = new ArrayList<>();
163 valList.add("REF_invokeInterface java/lang/Runnable run ()V");
164 valList.add("REF_invokeStatic TC1 lambda$0 ()V");
165 valList.add("()V");
166 return valList;
167 }
168 },
170 // Lambda expression in a for loop
171 TC2("import java.util.*;\n" +
172 "public class TC2 {\n" +
173 " void TC2_test() {\n" +
174 " List<String> list = new ArrayList<>();\n" +
175 " list.add(\"A\");\n" +
176 " list.add(\"B\");\n" +
177 " list.stream().forEach( s -> { System.out.println(s); } );\n" +
178 " }\n" +
179 " public static void main(String[] args) {\n" +
180 " new TC2().TC2_test();\n" +
181 " }\n" +
182 "}", BSMSpecifier.SPECIFIER1) {
184 @Override
185 List<String> getExpectedArgValues() {
186 List<String> valList = new ArrayList<>();
187 valList.add("REF_invokeInterface java/util/function/Consumer accept (Ljava/lang/Object;)V");
188 valList.add("REF_invokeStatic TC2 lambda$0 (Ljava/lang/String;)V");
189 valList.add("(Ljava/lang/String;)V");
190 return valList;
191 }
192 },
194 // Lambda initializer
195 TC3("class TC3 {\n" +
196 " interface SAM {\n" +
197 " void m(int i);\n" +
198 " }\n" +
199 " SAM lambda_03 = (int pos) -> { };\n" +
200 "}", BSMSpecifier.SPECIFIER1) {
202 @Override
203 List<String> getExpectedArgValues() {
204 List<String> valList = new ArrayList<>();
205 valList.add("REF_invokeInterface TC3$SAM m (I)V");
206 valList.add("REF_invokeStatic TC3 lambda$0 (I)V");
207 valList.add("(I)V");
208 return valList;
209 }
210 },
212 // Array initializer
213 TC4("class TC4 {\n" +
214 " interface Block<T> {\n" +
215 " void m(T t);\n" +
216 " }\n" +
217 " void test1() {\n" +
218 " Block<?>[] arr1 = { t -> { }, t -> { } };\n" +
219 " }\n" +
220 "}", BSMSpecifier.SPECIFIER1) {
222 @Override
223 List<String> getExpectedArgValues() {
224 List<String> valList = new ArrayList<>();
225 valList.add("REF_invokeInterface TC4$Block m (Ljava/lang/Object;)V");
226 valList.add("REF_invokeStatic TC4 lambda$0 (Ljava/lang/Object;)V");
227 valList.add("(Ljava/lang/Object;)V");
228 valList.add("REF_invokeStatic TC4 lambda$1 (Ljava/lang/Object;)V");
229 return valList;
230 }
231 },
233 //Lambda expression as a method arg
234 TC5("class TC5 {\n"+
235 " interface MapFun<T,R> { R m( T n); }\n" +
236 " void meth( MapFun<String,Integer> mf ) {\n" +
237 " assert( mf.m(\"four\") == 4);\n" +
238 " }\n"+
239 " void test(Integer i) {\n" +
240 " meth(s -> { Integer len = s.length(); return len; } );\n" +
241 " }\n"+
242 "}", BSMSpecifier.SPECIFIER1) {
244 @Override
245 List<String> getExpectedArgValues() {
246 List<String> valList = new ArrayList<>();
247 valList.add("REF_invokeInterface TC5$MapFun m (Ljava/lang/Object;)Ljava/lang/Object;");
248 valList.add("REF_invokeStatic TC5 lambda$0 (Ljava/lang/String;)Ljava/lang/Integer;");
249 valList.add("(Ljava/lang/String;)Ljava/lang/Integer;");
250 return valList;
251 }
252 },
254 //Inner class of Lambda expression
255 TC6("class TC6 {\n" +
256 " interface MapFun<T, R> { R m( T n); }\n" +
257 " MapFun<Class<?>,String> cs;\n" +
258 " void test() {\n" +
259 " cs = c -> {\n" +
260 " class innerClass {\n" +
261 " Class<?> icc;\n" +
262 " innerClass(Class<?> _c) { icc = _c; }\n" +
263 " String getString() { return icc.toString(); }\n" +
264 " }\n" +
265 " return new innerClass(c).getString();\n"+
266 " };\n" +
267 " }\n" +
268 "}\n", BSMSpecifier.SPECIFIER1) {
270 @Override
271 List<String> getExpectedArgValues() {
272 List<String> valList = new ArrayList<>();
273 valList.add("REF_invokeInterface TC6$MapFun m (Ljava/lang/Object;)Ljava/lang/Object;");
274 valList.add("REF_invokeSpecial TC6 lambda$0 (Ljava/lang/Class;)Ljava/lang/String;");
275 valList.add("(Ljava/lang/Class;)Ljava/lang/String;");
276 return valList;
277 }
278 },
280 // Method reference
281 TC7("class TC7 {\n" +
282 " static interface SAM {\n" +
283 " void m(Integer i);\n" +
284 " }\n" +
285 " void m(Integer i) {}\n" +
286 " SAM s = this::m;\n" +
287 "}\n", BSMSpecifier.SPECIFIER1) {
289 @Override
290 List<String> getExpectedArgValues() {
291 List<String> valList = new ArrayList<>();
292 valList.add("REF_invokeInterface TC7$SAM m (Ljava/lang/Integer;)V");
293 valList.add("REF_invokeVirtual TC7 m (Ljava/lang/Integer;)V");
294 valList.add("(Ljava/lang/Integer;)V");
295 return valList;
296 }
297 },
299 // Constructor reference
300 TC8("public class TC8 {\n" +
301 " static interface A {Fee<String> m();}\n" +
302 " static class Fee<T> {\n" +
303 " private T t;\n" +
304 " public Fee() {}\n" +
305 " }\n" +
306 " public static void main(String[] args) {\n" +
307 " A a = Fee<String>::new; \n" +
308 " }\n" +
309 "}\n", BSMSpecifier.SPECIFIER1) {
311 @Override
312 List<String> getExpectedArgValues() {
313 List<String> valList = new ArrayList<>();
314 valList.add("REF_invokeInterface TC8$A m ()LTC8$Fee;");
315 valList.add("REF_newInvokeSpecial TC8$Fee <init> ()V");
316 valList.add("()LTC8$Fee;");
317 return valList;
318 }
319 },
321 // Recursive lambda expression
322 TC9("class TC9 {\n" +
323 " interface Recursive<T, R> { T apply(R n); };\n" +
324 " Recursive<Integer,Integer> factorial;\n" +
325 " void test(Integer j) {\n" +
326 " factorial = i -> { return i == 0 ? 1 : i * factorial.apply( i - 1 ); };\n" +
327 " }\n" +
328 "}\n", BSMSpecifier.SPECIFIER1) {
330 @Override
331 List<String> getExpectedArgValues() {
332 List<String> valList = new ArrayList<>();
333 valList.add("REF_invokeInterface TC9$Recursive apply (Ljava/lang/Object;)Ljava/lang/Object;");
334 valList.add("REF_invokeSpecial TC9 lambda$0 (Ljava/lang/Integer;)Ljava/lang/Integer;");
335 valList.add("(Ljava/lang/Integer;)Ljava/lang/Integer;");
336 return valList;
337 }
338 },
340 //Serializable Lambda
341 TC10("import java.io.Serializable;\n" +
342 "class TC10 {\n" +
343 " interface Foo { int m(); }\n" +
344 " public static void main(String[] args) {\n" +
345 " Foo f1 = (Foo & Serializable)() -> 3;\n" +
346 " }\n" +
347 "}\n", BSMSpecifier.SPECIFIER2) {
349 @Override
350 List<String> getExpectedArgValues() {
351 List<String> valList = new ArrayList<>();
352 valList.add("REF_invokeStatic java/lang/invoke/LambdaMetafactory altMetaFactory (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;");
353 valList.add("REF_invokeInterface TC10$Foo m ()I");
354 valList.add("REF_invokeStatic TC10 lambda$main$3231c38a$0 ()I");
355 valList.add("()I");
356 valList.add("1");
357 return valList;
358 }
359 };
361 String srcCode;
362 BSMSpecifier bsmSpecifier;
364 TestCases(String src, BSMSpecifier bsmSpecifier) {
365 this.srcCode = src;
366 // By default, all test cases will have bootstrap method specifier as Lambda.MetaFactory
367 // For serializable lambda test cases, bootstrap method specifier changed to altMetaFactory
368 this.bsmSpecifier = bsmSpecifier;
369 }
371 List<String> getExpectedArgValues() {
372 return null;
373 }
375 void setSrcCode(String src) {
376 srcCode = src;
377 }
378 }
380 static class ConstantPoolVisitor implements ConstantPool.Visitor<String, Integer> {
381 final List<String> slist;
382 final ClassFile cf;
383 final ConstantPool cfpool;
384 final Map<Integer, String> bsmMap;
387 public ConstantPoolVisitor(ClassFile cf, int size) {
388 slist = new ArrayList<>(size);
389 for (int i = 0 ; i < size; i++) {
390 slist.add(null);
391 }
392 this.cf = cf;
393 this.cfpool = cf.constant_pool;
394 bsmMap = readBSM();
395 }
397 public Map<Integer, String> getBSMMap() {
398 return Collections.unmodifiableMap(bsmMap);
399 }
401 public String visit(CPInfo c, int index) {
402 return c.accept(this, index);
403 }
405 private Map<Integer, String> readBSM() {
406 BootstrapMethods_attribute bsmAttr =
407 (BootstrapMethods_attribute) cf.getAttribute(Attribute.BootstrapMethods);
408 if (bsmAttr != null) {
409 Map<Integer, String> out =
410 new HashMap<>(bsmAttr.bootstrap_method_specifiers.length);
411 for (BootstrapMethods_attribute.BootstrapMethodSpecifier bsms :
412 bsmAttr.bootstrap_method_specifiers) {
413 int index = bsms.bootstrap_method_ref;
414 try {
415 String value = slist.get(index);
416 if (value == null) {
417 value = visit(cfpool.get(index), index);
418 debugln("[SG]: index " + index);
419 debugln("[SG]: value " + value);
420 slist.set(index, value);
421 out.put(index, value);
422 }
423 for (int idx : bsms.bootstrap_arguments) {
424 value = slist.get(idx);
425 if (value == null) {
426 value = visit(cfpool.get(idx), idx);
427 debugln("[SG]: idx " + idx);
428 debugln("[SG]: value " + value);
429 slist.set(idx, value);
430 out.put(idx, value);
431 }
432 }
433 } catch (InvalidIndex ex) {
434 ex.printStackTrace();
435 }
436 }
437 return out;
438 }
439 return new HashMap<>(0);
440 }
442 @Override
443 public String visitClass(CONSTANT_Class_info c, Integer p) {
445 String value = slist.get(p);
446 if (value == null) {
447 try {
448 value = visit(cfpool.get(c.name_index), c.name_index);
449 slist.set(p, value);
450 } catch (ConstantPoolException ex) {
451 ex.printStackTrace();
452 }
453 }
454 return value;
455 }
457 @Override
458 public String visitDouble(CONSTANT_Double_info c, Integer p) {
460 String value = slist.get(p);
461 if (value == null) {
462 value = Double.toString(c.value);
463 slist.set(p, value);
464 }
465 return value;
466 }
468 @Override
469 public String visitFieldref(CONSTANT_Fieldref_info c, Integer p) {
471 String value = slist.get(p);
472 if (value == null) {
473 try {
474 value = visit(cfpool.get(c.class_index), c.class_index);
475 value = value.concat(" " + visit(cfpool.get(c.name_and_type_index),
476 c.name_and_type_index));
477 slist.set(p, value);
478 } catch (ConstantPoolException ex) {
479 ex.printStackTrace();
480 }
481 }
482 return value;
483 }
485 @Override
486 public String visitFloat(CONSTANT_Float_info c, Integer p) {
488 String value = slist.get(p);
489 if (value == null) {
490 value = Float.toString(c.value);
491 slist.set(p, value);
492 }
493 return value;
494 }
496 @Override
497 public String visitInteger(CONSTANT_Integer_info cnstnt, Integer p) {
499 String value = slist.get(p);
500 if (value == null) {
501 value = Integer.toString(cnstnt.value);
502 slist.set(p, value);
503 }
504 return value;
505 }
507 @Override
508 public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info c,
509 Integer p) {
511 String value = slist.get(p);
512 if (value == null) {
513 try {
514 value = visit(cfpool.get(c.class_index), c.class_index);
515 value = value.concat(" " +
516 visit(cfpool.get(c.name_and_type_index),
517 c.name_and_type_index));
518 slist.set(p, value);
519 } catch (ConstantPoolException ex) {
520 ex.printStackTrace();
521 }
522 }
523 return value;
524 }
526 @Override
527 public String visitInvokeDynamic(CONSTANT_InvokeDynamic_info c, Integer p) {
529 String value = slist.get(p);
530 if (value == null) {
531 try {
532 value = bsmMap.get(c.bootstrap_method_attr_index) + " "
533 + visit(cfpool.get(c.name_and_type_index), c.name_and_type_index);
534 slist.set(p, value);
535 } catch (ConstantPoolException ex) {
536 ex.printStackTrace();
537 }
538 }
539 return value;
540 }
542 @Override
543 public String visitLong(CONSTANT_Long_info c, Integer p) {
545 String value = slist.get(p);
546 if (value == null) {
547 value = Long.toString(c.value);
548 slist.set(p, value);
549 }
550 return value;
551 }
553 @Override
554 public String visitNameAndType(CONSTANT_NameAndType_info c, Integer p) {
556 String value = slist.get(p);
557 if (value == null) {
558 try {
559 value = visit(cfpool.get(c.name_index), c.name_index);
560 value = value.concat(" " +
561 visit(cfpool.get(c.type_index), c.type_index));
562 slist.set(p, value);
563 } catch (InvalidIndex ex) {
564 ex.printStackTrace();
565 }
566 }
567 return value;
568 }
570 @Override
571 public String visitMethodref(CONSTANT_Methodref_info c, Integer p) {
573 String value = slist.get(p);
574 if (value == null) {
575 try {
576 value = visit(cfpool.get(c.class_index), c.class_index);
577 value = value.concat(" " +
578 visit(cfpool.get(c.name_and_type_index),
579 c.name_and_type_index));
580 slist.set(p, value);
581 } catch (ConstantPoolException ex) {
582 ex.printStackTrace();
583 }
584 }
585 return value;
586 }
588 @Override
589 public String visitMethodHandle(CONSTANT_MethodHandle_info c, Integer p) {
591 String value = slist.get(p);
592 if (value == null) {
593 try {
594 value = c.reference_kind.name();
595 value = value.concat(" "
596 + visit(cfpool.get(c.reference_index), c.reference_index));
597 slist.set(p, value);
598 } catch (ConstantPoolException ex) {
599 ex.printStackTrace();
600 }
601 }
602 return value;
603 }
605 @Override
606 public String visitMethodType(CONSTANT_MethodType_info c, Integer p) {
608 String value = slist.get(p);
609 if (value == null) {
610 try {
611 value = visit(cfpool.get(c.descriptor_index), c.descriptor_index);
612 slist.set(p, value);
613 } catch (ConstantPoolException ex) {
614 ex.printStackTrace();
615 }
616 }
617 return value;
618 }
620 @Override
621 public String visitString(CONSTANT_String_info c, Integer p) {
623 try {
624 String value = slist.get(p);
625 if (value == null) {
626 value = c.getString();
627 slist.set(p, value);
628 }
629 return value;
630 } catch (ConstantPoolException ex) {
631 throw new RuntimeException("Fatal error", ex);
632 }
633 }
635 @Override
636 public String visitUtf8(CONSTANT_Utf8_info cnstnt, Integer p) {
638 String value = slist.get(p);
639 if (value == null) {
640 value = cnstnt.value;
641 slist.set(p, value);
642 }
643 return value;
644 }
645 }
646 }