author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 15384 | 5a8d00abf076 |
child 32454 | b0ac04e0fefe |
permissions | -rw-r--r-- |
11143 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
15384
diff
changeset
|
2 |
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
11143 | 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 |
|
15384
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
26 |
* @bug 7115050 8003280 8005852 8006694 |
14547 | 27 |
* @summary Add lambda tests |
28 |
* Add parser support for lambda expressions |
|
15384
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
29 |
* temporarily workaround combo tests are causing time out in several platforms |
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
30 |
* @library ../lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
15384
diff
changeset
|
31 |
* @modules jdk.compiler |
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
32 |
* @build JavacTestingAbstractThreadedTest |
15384
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
33 |
* @run main/othervm LambdaParserTest |
11143 | 34 |
*/ |
35 |
||
15384
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
36 |
// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
37 |
// see JDK-8006746 |
5a8d00abf076
8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents:
15367
diff
changeset
|
38 |
|
11143 | 39 |
import java.net.URI; |
40 |
import java.util.Arrays; |
|
41 |
import javax.tools.Diagnostic; |
|
42 |
import javax.tools.JavaFileObject; |
|
43 |
import javax.tools.SimpleJavaFileObject; |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
44 |
import com.sun.source.util.JavacTask; |
11143 | 45 |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
46 |
public class LambdaParserTest |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
47 |
extends JavacTestingAbstractThreadedTest |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
48 |
implements Runnable { |
11143 | 49 |
|
50 |
enum LambdaKind { |
|
51 |
NILARY_EXPR("()->x"), |
|
52 |
NILARY_STMT("()->{ return x; }"), |
|
15367 | 53 |
ONEARY_SHORT_EXPR("#PN->x"), |
54 |
ONEARY_SHORT_STMT("#PN->{ return x; }"), |
|
55 |
ONEARY_EXPR("(#M1 #T1 #PN)->x"), |
|
56 |
ONEARY_STMT("(#M1 #T1 #PN)->{ return x; }"), |
|
57 |
TWOARY_EXPR("(#M1 #T1 #PN, #M2 #T2 y)->x"), |
|
58 |
TWOARY_STMT("(#M1 #T1 #PN, #M2 #T2 y)->{ return x; }"); |
|
11143 | 59 |
|
60 |
String lambdaTemplate; |
|
61 |
||
62 |
LambdaKind(String lambdaTemplate) { |
|
63 |
this.lambdaTemplate = lambdaTemplate; |
|
64 |
} |
|
65 |
||
66 |
String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2, |
|
15367 | 67 |
ModifierKind mk1, ModifierKind mk2, LambdaParameterName pn) { |
11143 | 68 |
return lambdaTemplate.replaceAll("#M1", mk1.modifier) |
69 |
.replaceAll("#M2", mk2.modifier) |
|
70 |
.replaceAll("#T1", pk1.parameterType) |
|
15367 | 71 |
.replaceAll("#T2", pk2.parameterType) |
72 |
.replaceAll("#PN", pn.nameStr); |
|
11143 | 73 |
} |
74 |
||
75 |
int arity() { |
|
76 |
switch (this) { |
|
77 |
case NILARY_EXPR: |
|
78 |
case NILARY_STMT: return 0; |
|
79 |
case ONEARY_SHORT_EXPR: |
|
80 |
case ONEARY_SHORT_STMT: |
|
81 |
case ONEARY_EXPR: |
|
82 |
case ONEARY_STMT: return 1; |
|
83 |
case TWOARY_EXPR: |
|
84 |
case TWOARY_STMT: return 2; |
|
85 |
default: throw new AssertionError("Invalid lambda kind " + this); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
boolean isShort() { |
|
90 |
return this == ONEARY_SHORT_EXPR || |
|
91 |
this == ONEARY_SHORT_STMT; |
|
92 |
} |
|
93 |
} |
|
94 |
||
15367 | 95 |
enum LambdaParameterName { |
96 |
IDENT("x"), |
|
97 |
UNDERSCORE("_"); |
|
98 |
||
99 |
String nameStr; |
|
100 |
||
101 |
LambdaParameterName(String nameStr) { |
|
102 |
this.nameStr = nameStr; |
|
103 |
} |
|
104 |
} |
|
105 |
||
11143 | 106 |
enum LambdaParameterKind { |
107 |
IMPLICIT(""), |
|
108 |
EXPLIICT_SIMPLE("A"), |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
109 |
EXPLIICT_SIMPLE_ARR1("A[]"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
110 |
EXPLIICT_SIMPLE_ARR2("A[][]"), |
11143 | 111 |
EXPLICIT_VARARGS("A..."), |
112 |
EXPLICIT_GENERIC1("A<X>"), |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
113 |
EXPLICIT_GENERIC2("A<? extends X, ? super Y>"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
114 |
EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
115 |
EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
116 |
EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]"); |
11143 | 117 |
|
118 |
String parameterType; |
|
119 |
||
120 |
LambdaParameterKind(String parameterType) { |
|
121 |
this.parameterType = parameterType; |
|
122 |
} |
|
123 |
||
124 |
boolean explicit() { |
|
125 |
return this != IMPLICIT; |
|
126 |
} |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
127 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
128 |
boolean isVarargs() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
129 |
return this == EXPLICIT_VARARGS || |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
130 |
this == EXPLICIT_GENERIC2_VARARGS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
131 |
} |
11143 | 132 |
} |
133 |
||
134 |
enum ModifierKind { |
|
135 |
NONE(""), |
|
136 |
FINAL("final"), |
|
137 |
PUBLIC("public"); |
|
138 |
||
139 |
String modifier; |
|
140 |
||
141 |
ModifierKind(String modifier) { |
|
142 |
this.modifier = modifier; |
|
143 |
} |
|
144 |
||
145 |
boolean compatibleWith(LambdaParameterKind pk) { |
|
146 |
switch (this) { |
|
147 |
case PUBLIC: return false; |
|
148 |
case FINAL: return pk != LambdaParameterKind.IMPLICIT; |
|
149 |
case NONE: return true; |
|
150 |
default: throw new AssertionError("Invalid modifier kind " + this); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
enum ExprKind { |
|
156 |
NONE("#L#S"), |
|
157 |
SINGLE_PAREN1("(#L#S)"), |
|
158 |
SINGLE_PAREN2("(#L)#S"), |
|
159 |
DOUBLE_PAREN1("((#L#S))"), |
|
160 |
DOUBLE_PAREN2("((#L)#S)"), |
|
161 |
DOUBLE_PAREN3("((#L))#S"); |
|
162 |
||
163 |
String expressionTemplate; |
|
164 |
||
165 |
ExprKind(String expressionTemplate) { |
|
166 |
this.expressionTemplate = expressionTemplate; |
|
167 |
} |
|
168 |
||
169 |
String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2, |
|
15367 | 170 |
ModifierKind mk1, ModifierKind mk2, LambdaKind lk, LambdaParameterName pn, SubExprKind sk) { |
171 |
return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2, pn)) |
|
11143 | 172 |
.replaceAll("#S", sk.subExpression); |
173 |
} |
|
174 |
} |
|
175 |
||
176 |
enum SubExprKind { |
|
177 |
NONE(""), |
|
178 |
SELECT_FIELD(".f"), |
|
179 |
SELECT_METHOD(".f()"), |
|
180 |
SELECT_NEW(".new Foo()"), |
|
181 |
POSTINC("++"), |
|
182 |
POSTDEC("--"); |
|
183 |
||
184 |
String subExpression; |
|
185 |
||
186 |
SubExprKind(String subExpression) { |
|
187 |
this.subExpression = subExpression; |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
public static void main(String... args) throws Exception { |
|
192 |
for (LambdaKind lk : LambdaKind.values()) { |
|
15367 | 193 |
for (LambdaParameterName pn : LambdaParameterName.values()) { |
194 |
for (LambdaParameterKind pk1 : LambdaParameterKind.values()) { |
|
195 |
if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
196 |
continue; |
15367 | 197 |
for (LambdaParameterKind pk2 : LambdaParameterKind.values()) { |
198 |
if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
199 |
continue; |
15367 | 200 |
for (ModifierKind mk1 : ModifierKind.values()) { |
201 |
if (mk1 != ModifierKind.NONE && lk.isShort()) |
|
202 |
continue; |
|
203 |
if (lk.arity() < 1 && mk1 != ModifierKind.NONE) |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
204 |
continue; |
15367 | 205 |
for (ModifierKind mk2 : ModifierKind.values()) { |
206 |
if (lk.arity() < 2 && mk2 != ModifierKind.NONE) |
|
207 |
continue; |
|
208 |
for (SubExprKind sk : SubExprKind.values()) { |
|
209 |
for (ExprKind ek : ExprKind.values()) { |
|
210 |
pool.execute( |
|
211 |
new LambdaParserTest(pk1, pk2, mk1, |
|
212 |
mk2, lk, sk, ek, pn)); |
|
213 |
} |
|
11143 | 214 |
} |
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
221 |
|
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
222 |
checkAfterExec(); |
11143 | 223 |
} |
224 |
||
225 |
LambdaParameterKind pk1; |
|
226 |
LambdaParameterKind pk2; |
|
227 |
ModifierKind mk1; |
|
228 |
ModifierKind mk2; |
|
229 |
LambdaKind lk; |
|
15367 | 230 |
LambdaParameterName pn; |
11143 | 231 |
SubExprKind sk; |
232 |
ExprKind ek; |
|
233 |
JavaSource source; |
|
234 |
DiagnosticChecker diagChecker; |
|
235 |
||
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
236 |
LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
237 |
ModifierKind mk1, ModifierKind mk2, LambdaKind lk, |
15367 | 238 |
SubExprKind sk, ExprKind ek, LambdaParameterName pn) { |
11143 | 239 |
this.pk1 = pk1; |
240 |
this.pk2 = pk2; |
|
241 |
this.mk1 = mk1; |
|
242 |
this.mk2 = mk2; |
|
243 |
this.lk = lk; |
|
15367 | 244 |
this.pn = pn; |
11143 | 245 |
this.sk = sk; |
246 |
this.ek = ek; |
|
247 |
this.source = new JavaSource(); |
|
248 |
this.diagChecker = new DiagnosticChecker(); |
|
249 |
} |
|
250 |
||
251 |
class JavaSource extends SimpleJavaFileObject { |
|
252 |
||
253 |
String template = "class Test {\n" + |
|
254 |
" SAM s = #E;\n" + |
|
255 |
"}"; |
|
256 |
||
257 |
String source; |
|
258 |
||
259 |
public JavaSource() { |
|
260 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
261 |
source = template.replaceAll("#E", |
15367 | 262 |
ek.expressionString(pk1, pk2, mk1, mk2, lk, pn, sk)); |
11143 | 263 |
} |
264 |
||
265 |
@Override |
|
266 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
267 |
return source; |
|
268 |
} |
|
269 |
} |
|
270 |
||
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
271 |
public void run() { |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
272 |
JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, |
14547 | 273 |
null, null, Arrays.asList(source)); |
11143 | 274 |
try { |
275 |
ct.parse(); |
|
276 |
} catch (Throwable ex) { |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
277 |
processException(ex); |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
278 |
return; |
11143 | 279 |
} |
280 |
check(); |
|
281 |
} |
|
282 |
||
283 |
void check() { |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
284 |
checkCount.incrementAndGet(); |
11143 | 285 |
|
286 |
boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) || |
|
287 |
(lk.arity() > 1 && !mk2.compatibleWith(pk2)); |
|
288 |
||
289 |
if (lk.arity() == 2 && |
|
290 |
(pk1.explicit() != pk2.explicit() || |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
291 |
pk1.isVarargs())) { |
11143 | 292 |
errorExpected = true; |
293 |
} |
|
294 |
||
15367 | 295 |
errorExpected |= pn == LambdaParameterName.UNDERSCORE && |
296 |
lk.arity() > 0; |
|
297 |
||
11143 | 298 |
if (errorExpected != diagChecker.errorFound) { |
299 |
throw new Error("invalid diagnostics for source:\n" + |
|
300 |
source.getCharContent(true) + |
|
301 |
"\nFound error: " + diagChecker.errorFound + |
|
302 |
"\nExpected error: " + errorExpected); |
|
303 |
} |
|
304 |
} |
|
305 |
||
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
306 |
static class DiagnosticChecker |
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
307 |
implements javax.tools.DiagnosticListener<JavaFileObject> { |
11143 | 308 |
|
309 |
boolean errorFound; |
|
310 |
||
311 |
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
312 |
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
|
313 |
errorFound = true; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
15040
99fd9483d3f0
8005167: execution time of combo tests in javac should be improved
vromero
parents:
14725
diff
changeset
|
317 |
|
11143 | 318 |
} |