1/* 2 * Copyright (c) 2014, 2016, 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 8067422 27 * @summary Check that the lambda names are not unnecessarily unstable 28 * @library /tools/lib 29 * @modules jdk.jdeps/com.sun.tools.classfile 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.main 32 * jdk.jdeps/com.sun.tools.javap 33 * @build toolbox.ToolBox toolbox.JavacTask 34 * @run main TestNonSerializableLambdaNameStability 35 */ 36 37import java.io.ByteArrayInputStream; 38import java.io.InputStream; 39import java.util.ArrayList; 40import java.util.List; 41import javax.tools.StandardLocation; 42 43import com.sun.tools.classfile.ClassFile; 44import com.sun.tools.classfile.Method; 45 46import toolbox.JavacTask; 47import toolbox.ToolBox; 48 49public class TestNonSerializableLambdaNameStability { 50 51 public static void main(String... args) throws Exception { 52 new TestNonSerializableLambdaNameStability().run(); 53 } 54 55 String lambdaSource = "public class L%d {\n" + 56 " public static class A {\n" + 57 " private Runnable r = () -> { };\n" + 58 " }\n" + 59 " public static class B {\n" + 60 " private Runnable r = () -> { };\n" + 61 " }\n" + 62 " private Runnable r = () -> { };\n" + 63 "}\n"; 64 65 String expectedLambdaMethodName = "lambda$new$0"; 66 67 void run() throws Exception { 68 List<String> sources = new ArrayList<>(); 69 70 for (int i = 0; i < 3; i++) { 71 sources.add(String.format(lambdaSource, i)); 72 } 73 74 ToolBox tb = new ToolBox(); 75 76 try (ToolBox.MemoryFileManager fm = new ToolBox.MemoryFileManager()) { 77 new JavacTask(tb) 78 .sources(sources.toArray(new String[sources.size()])) 79 .fileManager(fm) 80 .run(); 81 82 for (String file : fm.getFileNames(StandardLocation.CLASS_OUTPUT)) { 83 byte[] fileBytes = fm.getFileBytes(StandardLocation.CLASS_OUTPUT, file); 84 try (InputStream in = new ByteArrayInputStream(fileBytes)) { 85 boolean foundLambdaMethod = false; 86 ClassFile cf = ClassFile.read(in); 87 StringBuilder seenMethods = new StringBuilder(); 88 String sep = ""; 89 for (Method m : cf.methods) { 90 String methodName = m.getName(cf.constant_pool); 91 if (expectedLambdaMethodName.equals(methodName)) { 92 foundLambdaMethod = true; 93 break; 94 } 95 seenMethods.append(sep); 96 seenMethods.append(methodName); 97 sep = ", "; 98 } 99 100 if (!foundLambdaMethod) { 101 throw new AbstractMethodError("Did not find the lambda method, " + 102 "found methods: " + seenMethods.toString()); 103 } 104 } 105 } 106 } 107 } 108} 109