Wed, 16 Sep 2015 10:56:23 +0200
8067422: Lambda method names are unnecessarily unstable
Summary: Lambda method numbers are now assigned per class for non-serializable lambdas.
Reviewed-by: mcimadamore, rfield, vromero
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Mon Mar 16 11:52:25 2015 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Wed Sep 16 10:56:23 2015 +0200 1.3 @@ -1179,12 +1179,14 @@ 1.4 @Override 1.5 public void visitClassDef(JCClassDecl tree) { 1.6 List<Frame> prevStack = frameStack; 1.7 + int prevLambdaCount = lambdaCount; 1.8 SyntheticMethodNameCounter prevSyntheticMethodNameCounts = 1.9 syntheticMethodNameCounts; 1.10 Map<ClassSymbol, Symbol> prevClinits = clinits; 1.11 DiagnosticSource prevSource = log.currentSource(); 1.12 try { 1.13 log.useSource(tree.sym.sourcefile); 1.14 + lambdaCount = 0; 1.15 syntheticMethodNameCounts = new SyntheticMethodNameCounter(); 1.16 prevClinits = new HashMap<ClassSymbol, Symbol>(); 1.17 if (tree.sym.owner.kind == MTH) { 1.18 @@ -1211,6 +1213,7 @@ 1.19 finally { 1.20 log.useSource(prevSource.getFile()); 1.21 frameStack = prevStack; 1.22 + lambdaCount = prevLambdaCount; 1.23 syntheticMethodNameCounts = prevSyntheticMethodNameCounts; 1.24 clinits = prevClinits; 1.25 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/lambda/lambdaNaming/TestNonSerializableLambdaNameStability.java Wed Sep 16 10:56:23 2015 +0200 2.3 @@ -0,0 +1,157 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8067422 2.30 + * @summary Check that the lambda names are not unnecessarily unstable 2.31 + * @run main TestNonSerializableLambdaNameStability 2.32 + */ 2.33 + 2.34 +import com.sun.tools.classfile.ClassFile; 2.35 +import com.sun.tools.classfile.Method; 2.36 + 2.37 +import java.io.ByteArrayInputStream; 2.38 +import java.io.ByteArrayOutputStream; 2.39 +import java.io.IOException; 2.40 +import java.io.InputStream; 2.41 +import java.io.OutputStream; 2.42 +import java.net.URI; 2.43 +import java.net.URISyntaxException; 2.44 +import java.util.ArrayList; 2.45 +import java.util.HashMap; 2.46 +import java.util.List; 2.47 +import java.util.Map; 2.48 + 2.49 +import javax.tools.FileObject; 2.50 +import javax.tools.ForwardingJavaFileManager; 2.51 +import javax.tools.JavaCompiler; 2.52 +import javax.tools.JavaFileManager; 2.53 +import javax.tools.JavaFileObject; 2.54 +import javax.tools.JavaFileObject.Kind; 2.55 +import javax.tools.SimpleJavaFileObject; 2.56 +import javax.tools.ToolProvider; 2.57 + 2.58 +public class TestNonSerializableLambdaNameStability { 2.59 + 2.60 + public static void main(String... args) throws Exception { 2.61 + new TestNonSerializableLambdaNameStability().run(); 2.62 + } 2.63 + 2.64 + String lambdaSource = "public class L%d {\n" + 2.65 + " public static class A {\n" + 2.66 + " private Runnable r = () -> { };\n" + 2.67 + " }\n" + 2.68 + " public static class B {\n" + 2.69 + " private Runnable r = () -> { };\n" + 2.70 + " }\n" + 2.71 + " private Runnable r = () -> { };\n" + 2.72 + "}\n"; 2.73 + 2.74 + String expectedLambdaMethodName = "lambda$new$0"; 2.75 + 2.76 + void run() throws Exception { 2.77 + List<JavaFileObject> sources = new ArrayList<>(); 2.78 + 2.79 + for (int i = 0; i < 3; i++) { 2.80 + sources.add(new SourceJavaFileObject("L" + i, String.format(lambdaSource, i))); 2.81 + } 2.82 + 2.83 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 2.84 + 2.85 + try (MemoryFileManager fm = new MemoryFileManager(compiler.getStandardFileManager(null, null, null))) { 2.86 + if (!compiler.getTask(null, fm, null, null, null, sources).call()) { 2.87 + throw new AssertionError("Compilation failed!"); 2.88 + } 2.89 + 2.90 + for (String file : fm.name2Content.keySet()) { 2.91 + byte[] fileBytes = fm.name2Content.get(file); 2.92 + try (InputStream in = new ByteArrayInputStream(fileBytes)) { 2.93 + boolean foundLambdaMethod = false; 2.94 + ClassFile cf = ClassFile.read(in); 2.95 + StringBuilder seenMethods = new StringBuilder(); 2.96 + String sep = ""; 2.97 + for (Method m : cf.methods) { 2.98 + String methodName = m.getName(cf.constant_pool); 2.99 + if (expectedLambdaMethodName.equals(methodName)) { 2.100 + foundLambdaMethod = true; 2.101 + break; 2.102 + } 2.103 + seenMethods.append(sep); 2.104 + seenMethods.append(methodName); 2.105 + sep = ", "; 2.106 + } 2.107 + 2.108 + if (!foundLambdaMethod) { 2.109 + throw new AbstractMethodError("Did not find the lambda method, " + 2.110 + "found methods: " + seenMethods.toString()); 2.111 + } 2.112 + } 2.113 + } 2.114 + } 2.115 + } 2.116 + 2.117 + class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { 2.118 + 2.119 + final Map<String, byte[]> name2Content = new HashMap<>(); 2.120 + 2.121 + public MemoryFileManager(JavaFileManager fileManager) { 2.122 + super(fileManager); 2.123 + } 2.124 + 2.125 + @Override 2.126 + public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { 2.127 + try { 2.128 + return new SimpleJavaFileObject(new URI("mem://" + className.replace('.', '/') + kind.extension), kind) { 2.129 + @Override public OutputStream openOutputStream() throws IOException { 2.130 + return new ByteArrayOutputStream() { 2.131 + @Override public void close() throws IOException { 2.132 + super.close(); 2.133 + name2Content.put(className, toByteArray()); 2.134 + } 2.135 + }; 2.136 + } 2.137 + }; 2.138 + } catch (URISyntaxException ex) { 2.139 + throw new AssertionError(ex); 2.140 + } 2.141 + } 2.142 + 2.143 + } 2.144 + 2.145 + class SourceJavaFileObject extends SimpleJavaFileObject { 2.146 + 2.147 + private final String code; 2.148 + 2.149 + public SourceJavaFileObject(String name, String code) throws URISyntaxException { 2.150 + super(new URI("mem:///" + name + ".java"), Kind.SOURCE); 2.151 + this.code = code; 2.152 + } 2.153 + 2.154 + @Override 2.155 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 2.156 + return code; 2.157 + } 2.158 + 2.159 + } 2.160 +}