|
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 */ |
|
23 |
|
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 */ |
|
32 |
|
33 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
|
34 // see JDK-8006746 |
|
35 |
|
36 import java.net.URI; |
|
37 import java.util.Arrays; |
|
38 |
|
39 import javax.tools.Diagnostic; |
|
40 import javax.tools.JavaFileObject; |
|
41 import javax.tools.SimpleJavaFileObject; |
|
42 |
|
43 import com.sun.source.util.JavacTask; |
|
44 import com.sun.tools.javac.api.ClientCodeWrapper; |
|
45 import com.sun.tools.javac.util.JCDiagnostic; |
|
46 |
|
47 public class TestLambdaToMethodStats |
|
48 extends JavacTestingAbstractThreadedTest |
|
49 implements Runnable { |
|
50 |
|
51 enum ExprKind { |
|
52 LAMBDA("()->null"), |
|
53 MREF1("this::g"), |
|
54 MREF2("this::h"); |
|
55 |
|
56 String exprStr; |
|
57 |
|
58 ExprKind(String exprStr) { |
|
59 this.exprStr = exprStr; |
|
60 } |
|
61 } |
|
62 |
|
63 enum TargetKind { |
|
64 IMPLICIT(""), |
|
65 SERIALIZABLE("(A & java.io.Serializable)"); |
|
66 |
|
67 String targetStr; |
|
68 |
|
69 TargetKind(String targetStr) { |
|
70 this.targetStr = targetStr; |
|
71 } |
|
72 } |
|
73 |
|
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 } |
|
80 |
|
81 checkAfterExec(true); |
|
82 } |
|
83 |
|
84 ExprKind ek; |
|
85 TargetKind tk; |
|
86 JavaSource source; |
|
87 DiagnosticChecker diagChecker; |
|
88 |
|
89 |
|
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 } |
|
96 |
|
97 class JavaSource extends SimpleJavaFileObject { |
|
98 |
|
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 "}"; |
|
107 |
|
108 String source; |
|
109 |
|
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 } |
|
115 |
|
116 @Override |
|
117 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
118 return source; |
|
119 } |
|
120 } |
|
121 |
|
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 } |
|
135 |
|
136 void check() { |
|
137 checkCount.incrementAndGet(); |
|
138 |
|
139 boolean error = diagChecker.lambda != |
|
140 (ek == ExprKind.LAMBDA); |
|
141 |
|
142 error |= diagChecker.bridge != |
|
143 (ek == ExprKind.MREF2); |
|
144 |
|
145 error |= diagChecker.altMetafactory != |
|
146 (tk == TargetKind.SERIALIZABLE); |
|
147 |
|
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 } |
|
156 |
|
157 static class DiagnosticChecker |
|
158 implements javax.tools.DiagnosticListener<JavaFileObject> { |
|
159 |
|
160 boolean altMetafactory; |
|
161 boolean bridge; |
|
162 boolean lambda; |
|
163 |
|
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 } |