test/tools/javac/lambda/lambdaNaming/TestNonSerializableLambdaNameStability.java

changeset 2701
6906fc8bc514
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/lambdaNaming/TestNonSerializableLambdaNameStability.java	Wed Sep 16 10:56:23 2015 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8067422
    1.30 + * @summary Check that the lambda names are not unnecessarily unstable
    1.31 + * @run main TestNonSerializableLambdaNameStability
    1.32 + */
    1.33 +
    1.34 +import com.sun.tools.classfile.ClassFile;
    1.35 +import com.sun.tools.classfile.Method;
    1.36 +
    1.37 +import java.io.ByteArrayInputStream;
    1.38 +import java.io.ByteArrayOutputStream;
    1.39 +import java.io.IOException;
    1.40 +import java.io.InputStream;
    1.41 +import java.io.OutputStream;
    1.42 +import java.net.URI;
    1.43 +import java.net.URISyntaxException;
    1.44 +import java.util.ArrayList;
    1.45 +import java.util.HashMap;
    1.46 +import java.util.List;
    1.47 +import java.util.Map;
    1.48 +
    1.49 +import javax.tools.FileObject;
    1.50 +import javax.tools.ForwardingJavaFileManager;
    1.51 +import javax.tools.JavaCompiler;
    1.52 +import javax.tools.JavaFileManager;
    1.53 +import javax.tools.JavaFileObject;
    1.54 +import javax.tools.JavaFileObject.Kind;
    1.55 +import javax.tools.SimpleJavaFileObject;
    1.56 +import javax.tools.ToolProvider;
    1.57 +
    1.58 +public class TestNonSerializableLambdaNameStability {
    1.59 +
    1.60 +    public static void main(String... args) throws Exception {
    1.61 +        new TestNonSerializableLambdaNameStability().run();
    1.62 +    }
    1.63 +
    1.64 +    String lambdaSource = "public class L%d {\n" +
    1.65 +                          "    public static class A {\n" +
    1.66 +                          "        private Runnable r = () -> { };\n" +
    1.67 +                          "    }\n" +
    1.68 +                          "    public static class B {\n" +
    1.69 +                          "        private Runnable r = () -> { };\n" +
    1.70 +                          "    }\n" +
    1.71 +                          "    private Runnable r = () -> { };\n" +
    1.72 +                          "}\n";
    1.73 +
    1.74 +    String expectedLambdaMethodName = "lambda$new$0";
    1.75 +
    1.76 +    void run() throws Exception {
    1.77 +        List<JavaFileObject> sources = new ArrayList<>();
    1.78 +
    1.79 +        for (int i = 0; i < 3; i++) {
    1.80 +            sources.add(new SourceJavaFileObject("L" + i, String.format(lambdaSource, i)));
    1.81 +        }
    1.82 +
    1.83 +        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    1.84 +
    1.85 +        try (MemoryFileManager fm = new MemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
    1.86 +            if (!compiler.getTask(null, fm, null, null, null, sources).call()) {
    1.87 +                throw new AssertionError("Compilation failed!");
    1.88 +            }
    1.89 +
    1.90 +            for (String file : fm.name2Content.keySet()) {
    1.91 +                byte[] fileBytes = fm.name2Content.get(file);
    1.92 +                try (InputStream in = new ByteArrayInputStream(fileBytes)) {
    1.93 +                    boolean foundLambdaMethod = false;
    1.94 +                    ClassFile cf = ClassFile.read(in);
    1.95 +                    StringBuilder seenMethods = new StringBuilder();
    1.96 +                    String sep = "";
    1.97 +                    for (Method m : cf.methods) {
    1.98 +                        String methodName = m.getName(cf.constant_pool);
    1.99 +                        if (expectedLambdaMethodName.equals(methodName)) {
   1.100 +                            foundLambdaMethod = true;
   1.101 +                            break;
   1.102 +                        }
   1.103 +                        seenMethods.append(sep);
   1.104 +                        seenMethods.append(methodName);
   1.105 +                        sep = ", ";
   1.106 +                    }
   1.107 +
   1.108 +                    if (!foundLambdaMethod) {
   1.109 +                        throw new AbstractMethodError("Did not find the lambda method, " +
   1.110 +                                                      "found methods: " + seenMethods.toString());
   1.111 +                    }
   1.112 +                }
   1.113 +            }
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
   1.118 +
   1.119 +        final Map<String, byte[]> name2Content = new HashMap<>();
   1.120 +
   1.121 +        public MemoryFileManager(JavaFileManager fileManager) {
   1.122 +            super(fileManager);
   1.123 +        }
   1.124 +
   1.125 +        @Override
   1.126 +        public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.127 +            try {
   1.128 +                return new SimpleJavaFileObject(new URI("mem://" + className.replace('.', '/') + kind.extension), kind) {
   1.129 +                    @Override public OutputStream openOutputStream() throws IOException {
   1.130 +                        return new ByteArrayOutputStream() {
   1.131 +                            @Override public void close() throws IOException {
   1.132 +                                super.close();
   1.133 +                                name2Content.put(className, toByteArray());
   1.134 +                            }
   1.135 +                        };
   1.136 +                    }
   1.137 +                };
   1.138 +            } catch (URISyntaxException ex) {
   1.139 +                throw new AssertionError(ex);
   1.140 +            }
   1.141 +        }
   1.142 +
   1.143 +    }
   1.144 +
   1.145 +    class SourceJavaFileObject extends SimpleJavaFileObject {
   1.146 +
   1.147 +        private final String code;
   1.148 +
   1.149 +        public SourceJavaFileObject(String name, String code) throws URISyntaxException {
   1.150 +            super(new URI("mem:///" + name + ".java"), Kind.SOURCE);
   1.151 +            this.code = code;
   1.152 +        }
   1.153 +
   1.154 +        @Override
   1.155 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.156 +            return code;
   1.157 +        }
   1.158 +
   1.159 +    }
   1.160 +}

mercurial