aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8013576 aoqi@0: * @summary Add stat support to LambdaToMethod aoqi@0: * @library ../lib aoqi@0: * @build JavacTestingAbstractThreadedTest aoqi@0: * @run main/othervm TestLambdaToMethodStats aoqi@0: */ aoqi@0: aoqi@0: // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) aoqi@0: // see JDK-8006746 aoqi@0: aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.tools.javac.api.ClientCodeWrapper; aoqi@0: import com.sun.tools.javac.util.JCDiagnostic; aoqi@0: aoqi@0: public class TestLambdaToMethodStats aoqi@0: extends JavacTestingAbstractThreadedTest aoqi@0: implements Runnable { aoqi@0: aoqi@0: enum ExprKind { aoqi@0: LAMBDA("()->null"), aoqi@0: MREF1("this::g"), aoqi@0: MREF2("this::h"); aoqi@0: aoqi@0: String exprStr; aoqi@0: aoqi@0: ExprKind(String exprStr) { aoqi@0: this.exprStr = exprStr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: enum TargetKind { aoqi@0: IMPLICIT(""), aoqi@0: SERIALIZABLE("(A & java.io.Serializable)"); aoqi@0: aoqi@0: String targetStr; aoqi@0: aoqi@0: TargetKind(String targetStr) { aoqi@0: this.targetStr = targetStr; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: for (ExprKind ek : ExprKind.values()) { aoqi@0: for (TargetKind tk : TargetKind.values()) { aoqi@0: pool.execute(new TestLambdaToMethodStats(ek, tk)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: checkAfterExec(true); aoqi@0: } aoqi@0: aoqi@0: ExprKind ek; aoqi@0: TargetKind tk; aoqi@0: JavaSource source; aoqi@0: DiagnosticChecker diagChecker; aoqi@0: aoqi@0: aoqi@0: TestLambdaToMethodStats(ExprKind ek, TargetKind tk) { aoqi@0: this.ek = ek; aoqi@0: this.tk = tk; aoqi@0: this.source = new JavaSource(); aoqi@0: this.diagChecker = new DiagnosticChecker(); aoqi@0: } aoqi@0: aoqi@0: class JavaSource extends SimpleJavaFileObject { aoqi@0: aoqi@0: String template = "interface A {\n" + aoqi@0: " Object o();\n" + aoqi@0: "}\n" + aoqi@0: "class Test {\n" + aoqi@0: " A a = #C#E;\n" + aoqi@0: " Object g() { return null; }\n" + aoqi@0: " Object h(Object... o) { return null; }\n" + aoqi@0: "}"; aoqi@0: aoqi@0: String source; aoqi@0: aoqi@0: public JavaSource() { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: source = template.replaceAll("#E", ek.exprStr) aoqi@0: .replaceAll("#C", tk.targetStr); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void run() { aoqi@0: JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, aoqi@0: Arrays.asList("-XDdumpLambdaToMethodStats"), aoqi@0: null, Arrays.asList(source)); aoqi@0: try { aoqi@0: ct.generate(); aoqi@0: } catch (Throwable ex) { aoqi@0: throw new aoqi@0: AssertionError("Error thron when analyzing the following source:\n" + aoqi@0: source.getCharContent(true)); aoqi@0: } aoqi@0: check(); aoqi@0: } aoqi@0: aoqi@0: void check() { aoqi@0: checkCount.incrementAndGet(); aoqi@0: aoqi@0: boolean error = diagChecker.lambda != aoqi@0: (ek == ExprKind.LAMBDA); aoqi@0: aoqi@0: error |= diagChecker.bridge != aoqi@0: (ek == ExprKind.MREF2); aoqi@0: aoqi@0: error |= diagChecker.altMetafactory != aoqi@0: (tk == TargetKind.SERIALIZABLE); aoqi@0: aoqi@0: if (error) { aoqi@0: throw new AssertionError("Bad stat diagnostic found for source\n" + aoqi@0: "lambda = " + diagChecker.lambda + "\n" + aoqi@0: "bridge = " + diagChecker.bridge + "\n" + aoqi@0: "altMF = " + diagChecker.altMetafactory + "\n" + aoqi@0: source.source); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class DiagnosticChecker aoqi@0: implements javax.tools.DiagnosticListener { aoqi@0: aoqi@0: boolean altMetafactory; aoqi@0: boolean bridge; aoqi@0: boolean lambda; aoqi@0: aoqi@0: public void report(Diagnostic diagnostic) { aoqi@0: try { aoqi@0: if (diagnostic.getKind() == Diagnostic.Kind.NOTE) { aoqi@0: switch (diagnostic.getCode()) { aoqi@0: case "compiler.note.lambda.stat": aoqi@0: lambda = true; aoqi@0: break; aoqi@0: case "compiler.note.mref.stat": aoqi@0: lambda = false; aoqi@0: bridge = false; aoqi@0: break; aoqi@0: case "compiler.note.mref.stat.1": aoqi@0: lambda = false; aoqi@0: bridge = true; aoqi@0: break; aoqi@0: default: aoqi@0: throw new AssertionError("unexpected note: " + diagnostic.getCode()); aoqi@0: } aoqi@0: ClientCodeWrapper.DiagnosticSourceUnwrapper dsu = aoqi@0: (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic; aoqi@0: altMetafactory = (Boolean)dsu.d.getArgs()[0]; aoqi@0: } aoqi@0: } catch (RuntimeException t) { aoqi@0: t.printStackTrace(); aoqi@0: throw t; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }