changeset 1816:3582b62dccb2

8013576: Add stat support to LambdaToMethod Summary: LambdaToMethod should emit info to help diagnose/test lambda metafactory problems Reviewed-by: jjg, vromero
author mcimadamore
date Mon, 10 Jun 2013 15:57:32 +0100
parents 105d1f9c1ab8
children bbedff0dc37e
files src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples/LambdaStat.java test/tools/javac/diags/examples/MrefStat.java test/tools/javac/diags/examples/MrefStat.java.rej test/tools/javac/diags/examples/MrefStat1.java test/tools/javac/diags/examples/MrefStat1.java.rej test/tools/javac/lambda/TestLambdaToMethodStats.java
diffstat 8 files changed, 394 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Mon Jun 10 15:18:47 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Mon Jun 10 15:57:32 2013 +0100
@@ -68,6 +68,8 @@
  */
 public class LambdaToMethod extends TreeTranslator {
 
+    private JCDiagnostic.Factory diags;
+    private Log log;
     private Lower lower;
     private Names names;
     private Symtab syms;
@@ -89,6 +91,9 @@
     /** info about the current class being processed */
     private KlassInfo kInfo;
 
+    /** dump statistics about lambda code generation */
+    private boolean dumpLambdaToMethodStats;
+
     /** Flag for alternate metafactories indicating the lambda object is intended to be serializable */
     public static final int FLAG_SERIALIZABLE = 1 << 0;
 
@@ -146,6 +151,8 @@
     }
 
     private LambdaToMethod(Context context) {
+        diags = JCDiagnostic.Factory.instance(context);
+        log = Log.instance(context);
         lower = Lower.instance(context);
         names = Names.instance(context);
         syms = Symtab.instance(context);
@@ -154,6 +161,8 @@
         types = Types.instance(context);
         transTypes = TransTypes.instance(context);
         analyzer = new LambdaAnalyzerPreprocessor();
+        Options options = Options.instance(context);
+        dumpLambdaToMethodStats = options.isSet("dumpLambdaToMethodStats");
     }
     // </editor-fold>
 
@@ -1101,7 +1110,9 @@
             Map<String, Integer> prevSerializableLambdaCount =
                     serializableLambdaCounts;
             Map<ClassSymbol, Symbol> prevClinits = clinits;
+            DiagnosticSource prevSource = log.currentSource();
             try {
+                log.useSource(tree.sym.sourcefile);
                 serializableLambdaCounts = new HashMap<String, Integer>();
                 prevClinits = new HashMap<ClassSymbol, Symbol>();
                 if (tree.sym.owner.kind == MTH) {
@@ -1126,6 +1137,7 @@
                 super.visitClassDef(tree);
             }
             finally {
+                log.useSource(prevSource.getFile());
                 frameStack = prevStack;
                 serializableLambdaCounts = prevSerializableLambdaCount;
                 clinits = prevClinits;
@@ -1685,6 +1697,9 @@
                 }
                 Name name = isSerializable() ? serializedLambdaName(owner) : lambdaName();
                 this.translatedSym = makeSyntheticMethod(0, name, null, owner.enclClass());
+                if (dumpLambdaToMethodStats) {
+                    log.note(tree, "lambda.stat", needsAltMetafactory(), translatedSym);
+                }
             }
 
             /**
@@ -1841,6 +1856,11 @@
                                               lambdaName().append(names.fromString("$bridge")), null,
                                               owner.enclClass())
                         : null;
+                if (dumpLambdaToMethodStats) {
+                    String key = bridgeSym == null ?
+                            "mref.stat" : "mref.stat.1";
+                    log.note(tree, key, needsAltMetafactory(), bridgeSym);
+                }
             }
 
             /**
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jun 10 15:18:47 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jun 10 15:57:32 2013 +0100
@@ -1163,6 +1163,23 @@
 compiler.note.potential.lambda.found=\
     This anonymous inner class creation can be turned into a lambda expression.
 
+# 0: boolean, 1: symbol
+compiler.note.lambda.stat=\
+    Translating lambda expression\n\
+    alternate metafactory = {0}\n\
+    synthetic method = {1}
+
+# 0: boolean, 1: unused
+compiler.note.mref.stat=\
+    Translating method reference\n\
+    alternate metafactory = {0}\n\
+
+# 0: boolean, 1: symbol
+compiler.note.mref.stat.1=\
+    Translating method reference\n\
+    alternate metafactory = {0}\n\
+    bridge method = {1}
+
 compiler.note.note=\
     Note:\u0020
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/LambdaStat.java	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.note.lambda.stat
+// options: -XDdumpLambdaToMethodStats
+
+class LambdaStat {
+    Runnable r = ()->{};
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/MrefStat.java	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.note.mref.stat
+// options: -XDdumpLambdaToMethodStats
+
+class MrefStat {
+    Runnable r = MrefStat::m;
+
+    static void m() { }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/MrefStat.java.rej	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,34 @@
+--- MrefStat.java
++++ MrefStat.java
+@@ -0,0 +1,31 @@
++/*
++ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
++ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
++ *
++ * This code is free software; you can redistribute it and/or modify it
++ * under the terms of the GNU General Public License version 2 only, as
++ * published by the Free Software Foundation.
++ *
++ * This code is distributed in the hope that it will be useful, but WITHOUT
++ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
++ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
++ * version 2 for more details (a copy is included in the LICENSE file that
++ * accompanied this code).
++ *
++ * You should have received a copy of the GNU General Public License version
++ * 2 along with this work; if not, write to the Free Software Foundation,
++ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
++ *
++ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
++ * or visit www.oracle.com if you need additional information or have any
++ * questions.
++ */
++
++// key: compiler.note.mref.stat
++// options: -XDdumpLambdaToMethodStats
++
++class MrefStat {
++    Runnable r = MrefStat::m;
++    
++    static void m() { }
++}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/MrefStat1.java	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.note.mref.stat.1
+// options: -XDdumpLambdaToMethodStats
+
+class MrefStat1 {
+
+    void m() { }
+
+    static class Sub extends MrefStat1 {
+        Runnable r = super::m;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/MrefStat1.java.rej	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,37 @@
+--- MrefStat1.java
++++ MrefStat1.java
+@@ -0,0 +1,34 @@
++/*
++ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
++ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
++ *
++ * This code is free software; you can redistribute it and/or modify it
++ * under the terms of the GNU General Public License version 2 only, as
++ * published by the Free Software Foundation.
++ *
++ * This code is distributed in the hope that it will be useful, but WITHOUT
++ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
++ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
++ * version 2 for more details (a copy is included in the LICENSE file that
++ * accompanied this code).
++ *
++ * You should have received a copy of the GNU General Public License version
++ * 2 along with this work; if not, write to the Free Software Foundation,
++ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
++ *
++ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
++ * or visit www.oracle.com if you need additional information or have any
++ * questions.
++ */
++
++// key: compiler.note.mref.stat.1
++// options: -XDdumpLambdaToMethodStats
++
++class MrefStat1 {    
++    
++    void m() { }
++    
++    static class Sub extends MrefStat1 {
++        Runnable r = super::m;
++    }
++}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/TestLambdaToMethodStats.java	Mon Jun 10 15:57:32 2013 +0100
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8013576
+ * @summary Add stat support to LambdaToMethod
+ * @library ../lib
+ * @build JavacTestingAbstractThreadedTest
+ * @run main/othervm TestLambdaToMethodStats
+ */
+
+// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
+// see JDK-8006746
+
+import java.net.URI;
+import java.util.Arrays;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+
+import com.sun.source.util.JavacTask;
+import com.sun.tools.javac.api.ClientCodeWrapper;
+import com.sun.tools.javac.util.JCDiagnostic;
+
+public class TestLambdaToMethodStats
+    extends JavacTestingAbstractThreadedTest
+    implements Runnable {
+
+    enum ExprKind {
+        LAMBDA("()->null"),
+        MREF1("this::g"),
+        MREF2("this::h");
+
+        String exprStr;
+
+        ExprKind(String exprStr) {
+            this.exprStr = exprStr;
+        }
+    }
+
+    enum TargetKind {
+        IMPLICIT(""),
+        SERIALIZABLE("(A & java.io.Serializable)");
+
+        String targetStr;
+
+        TargetKind(String targetStr) {
+            this.targetStr = targetStr;
+        }
+    }
+
+    public static void main(String... args) throws Exception {
+        for (ExprKind ek : ExprKind.values()) {
+            for (TargetKind tk : TargetKind.values()) {
+                pool.execute(new TestLambdaToMethodStats(ek, tk));
+            }
+        }
+
+        checkAfterExec(true);
+    }
+
+    ExprKind ek;
+    TargetKind tk;
+    JavaSource source;
+    DiagnosticChecker diagChecker;
+
+
+    TestLambdaToMethodStats(ExprKind ek, TargetKind tk) {
+        this.ek = ek;
+        this.tk = tk;
+        this.source = new JavaSource();
+        this.diagChecker = new DiagnosticChecker();
+    }
+
+    class JavaSource extends SimpleJavaFileObject {
+
+        String template = "interface A {\n" +
+                          "   Object o();\n" +
+                          "}\n" +
+                          "class Test {\n" +
+                          "   A a = #C#E;\n" +
+                          "   Object g() { return null; }\n" +
+                          "   Object h(Object... o) { return null; }\n" +
+                          "}";
+
+        String source;
+
+        public JavaSource() {
+            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+            source = template.replaceAll("#E", ek.exprStr)
+                    .replaceAll("#C", tk.targetStr);
+        }
+
+        @Override
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return source;
+        }
+    }
+
+    public void run() {
+        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
+                Arrays.asList("-XDdumpLambdaToMethodStats"),
+                null, Arrays.asList(source));
+        try {
+            ct.generate();
+        } catch (Throwable ex) {
+            throw new
+                AssertionError("Error thron when analyzing the following source:\n" +
+                    source.getCharContent(true));
+        }
+        check();
+    }
+
+    void check() {
+        checkCount.incrementAndGet();
+
+        boolean error = diagChecker.lambda !=
+                (ek == ExprKind.LAMBDA);
+
+        error |= diagChecker.bridge !=
+                (ek == ExprKind.MREF2);
+
+        error |= diagChecker.altMetafactory !=
+                (tk == TargetKind.SERIALIZABLE);
+
+        if (error) {
+            throw new AssertionError("Bad stat diagnostic found for source\n" +
+                    "lambda = " + diagChecker.lambda + "\n" +
+                    "bridge = " + diagChecker.bridge + "\n" +
+                    "altMF = " + diagChecker.altMetafactory + "\n" +
+                    source.source);
+        }
+    }
+
+    static class DiagnosticChecker
+        implements javax.tools.DiagnosticListener<JavaFileObject> {
+
+        boolean altMetafactory;
+        boolean bridge;
+        boolean lambda;
+
+        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
+            try {
+                if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
+                    switch (diagnostic.getCode()) {
+                        case "compiler.note.lambda.stat":
+                            lambda = true;
+                            break;
+                        case "compiler.note.mref.stat":
+                            lambda = false;
+                            bridge = false;
+                            break;
+                        case "compiler.note.mref.stat.1":
+                            lambda = false;
+                            bridge = true;
+                            break;
+                        default:
+                            throw new AssertionError("unexpected note: " + diagnostic.getCode());
+                    }
+                    ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
+                        (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
+                    altMetafactory = (Boolean)dsu.d.getArgs()[0];
+                }
+            } catch (RuntimeException t) {
+                t.printStackTrace();
+                throw t;
+            }
+        }
+    }
+}