TestLambdaToMethodStats.java revision 3528:5538ba41cb97
133965Sjdp/*
260484Sobrien * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
333965Sjdp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
433965Sjdp *
533965Sjdp * This code is free software; you can redistribute it and/or modify it
633965Sjdp * under the terms of the GNU General Public License version 2 only, as
733965Sjdp * published by the Free Software Foundation.
833965Sjdp *
933965Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT
1033965Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1133965Sjdp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1233965Sjdp * version 2 for more details (a copy is included in the LICENSE file that
1333965Sjdp * accompanied this code).
1433965Sjdp *
1533965Sjdp * You should have received a copy of the GNU General Public License version
1633965Sjdp * 2 along with this work; if not, write to the Free Software Foundation,
1733965Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1833965Sjdp *
1933965Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2033965Sjdp * or visit www.oracle.com if you need additional information or have any
2133965Sjdp * questions.
2233965Sjdp */
2333965Sjdp
2433965Sjdp/*
2533965Sjdp * @test
2633965Sjdp * @bug 8013576 8129962
2733965Sjdp * @summary Add stat support to LambdaToMethod
2833965Sjdp * @library /tools/javac/lib
2933965Sjdp * @modules jdk.compiler/com.sun.tools.javac.api
3033965Sjdp *          jdk.compiler/com.sun.tools.javac.code
3133965Sjdp *          jdk.compiler/com.sun.tools.javac.comp
3233965Sjdp *          jdk.compiler/com.sun.tools.javac.main
3333965Sjdp *          jdk.compiler/com.sun.tools.javac.tree
3433965Sjdp *          jdk.compiler/com.sun.tools.javac.util
3533965Sjdp * @build combo.ComboTestHelper
3633965Sjdp * @run main TestLambdaToMethodStats
3733965Sjdp */
3833965Sjdp
3933965Sjdpimport java.io.IOException;
4033965Sjdp
4133965Sjdpimport javax.tools.Diagnostic;
4233965Sjdpimport javax.tools.JavaFileObject;
4333965Sjdp
4433965Sjdpimport com.sun.tools.javac.api.ClientCodeWrapper;
4533965Sjdp
4633965Sjdpimport com.sun.tools.javac.util.List;
4733965Sjdpimport combo.ComboInstance;
4833965Sjdpimport combo.ComboParameter;
4933965Sjdpimport combo.ComboTask.Result;
5033965Sjdpimport combo.ComboTestHelper;
5133965Sjdp
5233965Sjdppublic class TestLambdaToMethodStats extends ComboInstance<TestLambdaToMethodStats> {
5333965Sjdp
5433965Sjdp    enum ExprKind implements ComboParameter {
5533965Sjdp        LAMBDA("()->null"),
5633965Sjdp        MREF1("this::g"),
5733965Sjdp        MREF2("this::h");
5833965Sjdp
5933965Sjdp        String exprStr;
6033965Sjdp
6133965Sjdp        ExprKind(String exprStr) {
6233965Sjdp            this.exprStr = exprStr;
6333965Sjdp        }
6433965Sjdp
6533965Sjdp        @Override
6633965Sjdp        public String expand(String optParameter) {
6733965Sjdp            return exprStr;
6833965Sjdp        }
6933965Sjdp    }
7033965Sjdp
7133965Sjdp    enum TargetKind implements ComboParameter {
7233965Sjdp        IMPLICIT(""),
7333965Sjdp        SERIALIZABLE("(A & java.io.Serializable)");
7433965Sjdp
7533965Sjdp        String targetStr;
7633965Sjdp
7733965Sjdp        TargetKind(String targetStr) {
7833965Sjdp            this.targetStr = targetStr;
7933965Sjdp        }
8033965Sjdp
8133965Sjdp        @Override
8233965Sjdp        public String expand(String optParameter) {
8333965Sjdp            return targetStr;
8433965Sjdp        }
8560484Sobrien    }
8660484Sobrien
8733965Sjdp    enum DiagnosticKind {
8833965Sjdp        LAMBDA_STAT("compiler.note.lambda.stat", true, false),
8933965Sjdp        MREF_STAT("compiler.note.mref.stat", false, false),
9033965Sjdp        MREF_STAT1("compiler.note.mref.stat.1", false, true);
9133965Sjdp
9233965Sjdp        String code;
9333965Sjdp        boolean lambda;
9433965Sjdp        boolean bridge;
9533965Sjdp
9633965Sjdp        DiagnosticKind(String code, boolean lambda, boolean bridge) {
9733965Sjdp            this.code = code;
9833965Sjdp            this.lambda = lambda;
9933965Sjdp            this.bridge = bridge;
10033965Sjdp        }
10133965Sjdp    }
102
103    public static void main(String... args) throws Exception {
104        new ComboTestHelper<TestLambdaToMethodStats>()
105                .withDimension("EXPR", (x, expr) -> x.ek = expr, ExprKind.values())
106                .withDimension("CAST", (x, target) -> x.tk = target, TargetKind.values())
107                .run(TestLambdaToMethodStats::new);
108    }
109
110    ExprKind ek;
111    TargetKind tk;
112
113    String template = "interface A {\n" +
114            "   Object o();\n" +
115            "}\n" +
116            "class Test {\n" +
117            "   A a = #{CAST}#{EXPR};\n" +
118            "   Object g() { return null; }\n" +
119            "   Object h(Object... o) { return null; }\n" +
120            "}";
121
122    @Override
123    public void doWork() throws IOException {
124        check(newCompilationTask()
125                .withOption("-Xdebug:dumpLambdaToMethodStats")
126                .withSourceFromTemplate(template)
127                .generate());
128    }
129
130    void check(Result<?> res) {
131        DiagnosticKind diag = null;
132        boolean altMetafactory = false;
133        for (DiagnosticKind dk : DiagnosticKind.values()) {
134            List<Diagnostic<? extends JavaFileObject>> jcDiag = res.diagnosticsForKey(dk.code);
135            if (jcDiag.nonEmpty()) {
136                diag = dk;
137                ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
138                        (ClientCodeWrapper.DiagnosticSourceUnwrapper)jcDiag.head;
139                altMetafactory = (Boolean)dsu.d.getArgs()[0];
140                break;
141            }
142        }
143
144        if (diag == null) {
145            fail("No diagnostic found; " + res.compilationInfo());
146        }
147
148        boolean error = diag.lambda !=
149                (ek == ExprKind.LAMBDA);
150
151        error |= diag.bridge !=
152                (ek == ExprKind.MREF2);
153
154        error |= altMetafactory !=
155                (tk == TargetKind.SERIALIZABLE);
156
157        if (error) {
158            fail("Bad stat diagnostic found for source\n" +
159                    "lambda = " + diag.lambda + "\n" +
160                    "bridge = " + diag.bridge + "\n" +
161                    "altMF = " + altMetafactory + "\n" +
162                    res.compilationInfo());
163        }
164    }
165}
166