langtools/test/tools/javac/lambda/lambdaNaming/TestNonSerializableLambdaNameStability.java
changeset 28145 365419055307
child 30730 d3ce7619db2c
equal deleted inserted replaced
28144:daec48e77612 28145:365419055307
       
     1 /*
       
     2  * Copyright (c) 2014, 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  * @build ToolBox
       
    30  * @run main TestNonSerializableLambdaNameStability
       
    31  */
       
    32 
       
    33 import com.sun.tools.classfile.ClassFile;
       
    34 import com.sun.tools.classfile.Method;
       
    35 import java.io.ByteArrayInputStream;
       
    36 import java.io.InputStream;
       
    37 import java.util.ArrayList;
       
    38 import java.util.List;
       
    39 import javax.tools.StandardLocation;
       
    40 
       
    41 public class TestNonSerializableLambdaNameStability {
       
    42 
       
    43     public static void main(String... args) throws Exception {
       
    44         new TestNonSerializableLambdaNameStability().run();
       
    45     }
       
    46 
       
    47     String lambdaSource = "public class L%d {\n" +
       
    48                           "    public static class A {\n" +
       
    49                           "        private Runnable r = () -> { };\n" +
       
    50                           "    }\n" +
       
    51                           "    public static class B {\n" +
       
    52                           "        private Runnable r = () -> { };\n" +
       
    53                           "    }\n" +
       
    54                           "    private Runnable r = () -> { };\n" +
       
    55                           "}\n";
       
    56 
       
    57     String expectedLambdaMethodName = "lambda$new$0";
       
    58 
       
    59     void run() throws Exception {
       
    60         List<String> sources = new ArrayList<>();
       
    61 
       
    62         for (int i = 0; i < 3; i++) {
       
    63             sources.add(String.format(lambdaSource, i));
       
    64         }
       
    65 
       
    66         ToolBox tb = new ToolBox();
       
    67 
       
    68         try (ToolBox.MemoryFileManager fm = new ToolBox.MemoryFileManager()) {
       
    69             tb.new JavacTask()
       
    70               .sources(sources.toArray(new String[sources.size()]))
       
    71               .fileManager(fm)
       
    72               .run();
       
    73 
       
    74             for (String file : fm.files.get(StandardLocation.CLASS_OUTPUT).keySet()) {
       
    75                 byte[] fileBytes = fm.getFileBytes(StandardLocation.CLASS_OUTPUT, file);
       
    76                 try (InputStream in = new ByteArrayInputStream(fileBytes)) {
       
    77                     boolean foundLambdaMethod = false;
       
    78                     ClassFile cf = ClassFile.read(in);
       
    79                     StringBuilder seenMethods = new StringBuilder();
       
    80                     String sep = "";
       
    81                     for (Method m : cf.methods) {
       
    82                         String methodName = m.getName(cf.constant_pool);
       
    83                         if (expectedLambdaMethodName.equals(methodName)) {
       
    84                             foundLambdaMethod = true;
       
    85                             break;
       
    86                         }
       
    87                         seenMethods.append(sep);
       
    88                         seenMethods.append(methodName);
       
    89                         sep = ", ";
       
    90                     }
       
    91 
       
    92                     if (!foundLambdaMethod) {
       
    93                         throw new AbstractMethodError("Did not find the lambda method, " +
       
    94                                                       "found methods: " + seenMethods.toString());
       
    95                     }
       
    96                 }
       
    97             }
       
    98         }
       
    99     }
       
   100 }