test/tools/javac/lambda/TestLambdaToMethodStats.java

changeset 1817
3582b62dccb2
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/TestLambdaToMethodStats.java	Mon Jun 10 15:57:32 2013 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 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 8013576
    1.30 + * @summary Add stat support to LambdaToMethod
    1.31 + * @library ../lib
    1.32 + * @build JavacTestingAbstractThreadedTest
    1.33 + * @run main/othervm TestLambdaToMethodStats
    1.34 + */
    1.35 +
    1.36 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
    1.37 +// see JDK-8006746
    1.38 +
    1.39 +import java.net.URI;
    1.40 +import java.util.Arrays;
    1.41 +
    1.42 +import javax.tools.Diagnostic;
    1.43 +import javax.tools.JavaFileObject;
    1.44 +import javax.tools.SimpleJavaFileObject;
    1.45 +
    1.46 +import com.sun.source.util.JavacTask;
    1.47 +import com.sun.tools.javac.api.ClientCodeWrapper;
    1.48 +import com.sun.tools.javac.util.JCDiagnostic;
    1.49 +
    1.50 +public class TestLambdaToMethodStats
    1.51 +    extends JavacTestingAbstractThreadedTest
    1.52 +    implements Runnable {
    1.53 +
    1.54 +    enum ExprKind {
    1.55 +        LAMBDA("()->null"),
    1.56 +        MREF1("this::g"),
    1.57 +        MREF2("this::h");
    1.58 +
    1.59 +        String exprStr;
    1.60 +
    1.61 +        ExprKind(String exprStr) {
    1.62 +            this.exprStr = exprStr;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    enum TargetKind {
    1.67 +        IMPLICIT(""),
    1.68 +        SERIALIZABLE("(A & java.io.Serializable)");
    1.69 +
    1.70 +        String targetStr;
    1.71 +
    1.72 +        TargetKind(String targetStr) {
    1.73 +            this.targetStr = targetStr;
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    public static void main(String... args) throws Exception {
    1.78 +        for (ExprKind ek : ExprKind.values()) {
    1.79 +            for (TargetKind tk : TargetKind.values()) {
    1.80 +                pool.execute(new TestLambdaToMethodStats(ek, tk));
    1.81 +            }
    1.82 +        }
    1.83 +
    1.84 +        checkAfterExec(true);
    1.85 +    }
    1.86 +
    1.87 +    ExprKind ek;
    1.88 +    TargetKind tk;
    1.89 +    JavaSource source;
    1.90 +    DiagnosticChecker diagChecker;
    1.91 +
    1.92 +
    1.93 +    TestLambdaToMethodStats(ExprKind ek, TargetKind tk) {
    1.94 +        this.ek = ek;
    1.95 +        this.tk = tk;
    1.96 +        this.source = new JavaSource();
    1.97 +        this.diagChecker = new DiagnosticChecker();
    1.98 +    }
    1.99 +
   1.100 +    class JavaSource extends SimpleJavaFileObject {
   1.101 +
   1.102 +        String template = "interface A {\n" +
   1.103 +                          "   Object o();\n" +
   1.104 +                          "}\n" +
   1.105 +                          "class Test {\n" +
   1.106 +                          "   A a = #C#E;\n" +
   1.107 +                          "   Object g() { return null; }\n" +
   1.108 +                          "   Object h(Object... o) { return null; }\n" +
   1.109 +                          "}";
   1.110 +
   1.111 +        String source;
   1.112 +
   1.113 +        public JavaSource() {
   1.114 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.115 +            source = template.replaceAll("#E", ek.exprStr)
   1.116 +                    .replaceAll("#C", tk.targetStr);
   1.117 +        }
   1.118 +
   1.119 +        @Override
   1.120 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.121 +            return source;
   1.122 +        }
   1.123 +    }
   1.124 +
   1.125 +    public void run() {
   1.126 +        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
   1.127 +                Arrays.asList("-XDdumpLambdaToMethodStats"),
   1.128 +                null, Arrays.asList(source));
   1.129 +        try {
   1.130 +            ct.generate();
   1.131 +        } catch (Throwable ex) {
   1.132 +            throw new
   1.133 +                AssertionError("Error thron when analyzing the following source:\n" +
   1.134 +                    source.getCharContent(true));
   1.135 +        }
   1.136 +        check();
   1.137 +    }
   1.138 +
   1.139 +    void check() {
   1.140 +        checkCount.incrementAndGet();
   1.141 +
   1.142 +        boolean error = diagChecker.lambda !=
   1.143 +                (ek == ExprKind.LAMBDA);
   1.144 +
   1.145 +        error |= diagChecker.bridge !=
   1.146 +                (ek == ExprKind.MREF2);
   1.147 +
   1.148 +        error |= diagChecker.altMetafactory !=
   1.149 +                (tk == TargetKind.SERIALIZABLE);
   1.150 +
   1.151 +        if (error) {
   1.152 +            throw new AssertionError("Bad stat diagnostic found for source\n" +
   1.153 +                    "lambda = " + diagChecker.lambda + "\n" +
   1.154 +                    "bridge = " + diagChecker.bridge + "\n" +
   1.155 +                    "altMF = " + diagChecker.altMetafactory + "\n" +
   1.156 +                    source.source);
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    static class DiagnosticChecker
   1.161 +        implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.162 +
   1.163 +        boolean altMetafactory;
   1.164 +        boolean bridge;
   1.165 +        boolean lambda;
   1.166 +
   1.167 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.168 +            try {
   1.169 +                if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
   1.170 +                    switch (diagnostic.getCode()) {
   1.171 +                        case "compiler.note.lambda.stat":
   1.172 +                            lambda = true;
   1.173 +                            break;
   1.174 +                        case "compiler.note.mref.stat":
   1.175 +                            lambda = false;
   1.176 +                            bridge = false;
   1.177 +                            break;
   1.178 +                        case "compiler.note.mref.stat.1":
   1.179 +                            lambda = false;
   1.180 +                            bridge = true;
   1.181 +                            break;
   1.182 +                        default:
   1.183 +                            throw new AssertionError("unexpected note: " + diagnostic.getCode());
   1.184 +                    }
   1.185 +                    ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
   1.186 +                        (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
   1.187 +                    altMetafactory = (Boolean)dsu.d.getArgs()[0];
   1.188 +                }
   1.189 +            } catch (RuntimeException t) {
   1.190 +                t.printStackTrace();
   1.191 +                throw t;
   1.192 +            }
   1.193 +        }
   1.194 +    }
   1.195 +}

mercurial