Mon, 20 Jul 2015 11:41:52 -0700
Added tag jdk8u65-b06 for changeset ae5e31450299
1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 8013576
27 * @summary Add stat support to LambdaToMethod
28 * @library ../lib
29 * @build JavacTestingAbstractThreadedTest
30 * @run main/othervm TestLambdaToMethodStats
31 */
33 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
34 // see JDK-8006746
36 import java.net.URI;
37 import java.util.Arrays;
39 import javax.tools.Diagnostic;
40 import javax.tools.JavaFileObject;
41 import javax.tools.SimpleJavaFileObject;
43 import com.sun.source.util.JavacTask;
44 import com.sun.tools.javac.api.ClientCodeWrapper;
45 import com.sun.tools.javac.util.JCDiagnostic;
47 public class TestLambdaToMethodStats
48 extends JavacTestingAbstractThreadedTest
49 implements Runnable {
51 enum ExprKind {
52 LAMBDA("()->null"),
53 MREF1("this::g"),
54 MREF2("this::h");
56 String exprStr;
58 ExprKind(String exprStr) {
59 this.exprStr = exprStr;
60 }
61 }
63 enum TargetKind {
64 IMPLICIT(""),
65 SERIALIZABLE("(A & java.io.Serializable)");
67 String targetStr;
69 TargetKind(String targetStr) {
70 this.targetStr = targetStr;
71 }
72 }
74 public static void main(String... args) throws Exception {
75 for (ExprKind ek : ExprKind.values()) {
76 for (TargetKind tk : TargetKind.values()) {
77 pool.execute(new TestLambdaToMethodStats(ek, tk));
78 }
79 }
81 checkAfterExec(true);
82 }
84 ExprKind ek;
85 TargetKind tk;
86 JavaSource source;
87 DiagnosticChecker diagChecker;
90 TestLambdaToMethodStats(ExprKind ek, TargetKind tk) {
91 this.ek = ek;
92 this.tk = tk;
93 this.source = new JavaSource();
94 this.diagChecker = new DiagnosticChecker();
95 }
97 class JavaSource extends SimpleJavaFileObject {
99 String template = "interface A {\n" +
100 " Object o();\n" +
101 "}\n" +
102 "class Test {\n" +
103 " A a = #C#E;\n" +
104 " Object g() { return null; }\n" +
105 " Object h(Object... o) { return null; }\n" +
106 "}";
108 String source;
110 public JavaSource() {
111 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
112 source = template.replaceAll("#E", ek.exprStr)
113 .replaceAll("#C", tk.targetStr);
114 }
116 @Override
117 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
118 return source;
119 }
120 }
122 public void run() {
123 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
124 Arrays.asList("-XDdumpLambdaToMethodStats"),
125 null, Arrays.asList(source));
126 try {
127 ct.generate();
128 } catch (Throwable ex) {
129 throw new
130 AssertionError("Error thron when analyzing the following source:\n" +
131 source.getCharContent(true));
132 }
133 check();
134 }
136 void check() {
137 checkCount.incrementAndGet();
139 boolean error = diagChecker.lambda !=
140 (ek == ExprKind.LAMBDA);
142 error |= diagChecker.bridge !=
143 (ek == ExprKind.MREF2);
145 error |= diagChecker.altMetafactory !=
146 (tk == TargetKind.SERIALIZABLE);
148 if (error) {
149 throw new AssertionError("Bad stat diagnostic found for source\n" +
150 "lambda = " + diagChecker.lambda + "\n" +
151 "bridge = " + diagChecker.bridge + "\n" +
152 "altMF = " + diagChecker.altMetafactory + "\n" +
153 source.source);
154 }
155 }
157 static class DiagnosticChecker
158 implements javax.tools.DiagnosticListener<JavaFileObject> {
160 boolean altMetafactory;
161 boolean bridge;
162 boolean lambda;
164 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
165 try {
166 if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
167 switch (diagnostic.getCode()) {
168 case "compiler.note.lambda.stat":
169 lambda = true;
170 break;
171 case "compiler.note.mref.stat":
172 lambda = false;
173 bridge = false;
174 break;
175 case "compiler.note.mref.stat.1":
176 lambda = false;
177 bridge = true;
178 break;
179 default:
180 throw new AssertionError("unexpected note: " + diagnostic.getCode());
181 }
182 ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
183 (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
184 altMetafactory = (Boolean)dsu.d.getArgs()[0];
185 }
186 } catch (RuntimeException t) {
187 t.printStackTrace();
188 throw t;
189 }
190 }
191 }
192 }