author | mcimadamore |
Fri, 30 Nov 2012 15:14:48 +0000 | |
changeset 14725 | 65836e833f59 |
parent 14547 | 86d8d242b0c4 |
child 15040 | 99fd9483d3f0 |
permissions | -rw-r--r-- |
11143 | 1 |
/* |
2 |
* Copyright (c) 2011, 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 7115050 |
|
14547 | 27 |
* @bug 8003280 |
28 |
* @summary Add lambda tests |
|
29 |
* Add parser support for lambda expressions |
|
11143 | 30 |
*/ |
31 |
||
32 |
import com.sun.source.util.JavacTask; |
|
33 |
import java.net.URI; |
|
34 |
import java.util.Arrays; |
|
35 |
import javax.tools.Diagnostic; |
|
36 |
import javax.tools.JavaCompiler; |
|
37 |
import javax.tools.JavaFileObject; |
|
38 |
import javax.tools.SimpleJavaFileObject; |
|
39 |
import javax.tools.StandardJavaFileManager; |
|
40 |
import javax.tools.ToolProvider; |
|
41 |
||
42 |
public class LambdaParserTest { |
|
43 |
||
44 |
static int checkCount = 0; |
|
45 |
||
46 |
enum LambdaKind { |
|
47 |
NILARY_EXPR("()->x"), |
|
48 |
NILARY_STMT("()->{ return x; }"), |
|
49 |
ONEARY_SHORT_EXPR("x->x"), |
|
50 |
ONEARY_SHORT_STMT("x->{ return x; }"), |
|
51 |
ONEARY_EXPR("(#M1 #T1 x)->x"), |
|
52 |
ONEARY_STMT("(#M1 #T1 x)->{ return x; }"), |
|
53 |
TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"), |
|
54 |
TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }"); |
|
55 |
||
56 |
String lambdaTemplate; |
|
57 |
||
58 |
LambdaKind(String lambdaTemplate) { |
|
59 |
this.lambdaTemplate = lambdaTemplate; |
|
60 |
} |
|
61 |
||
62 |
String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2, |
|
63 |
ModifierKind mk1, ModifierKind mk2) { |
|
64 |
return lambdaTemplate.replaceAll("#M1", mk1.modifier) |
|
65 |
.replaceAll("#M2", mk2.modifier) |
|
66 |
.replaceAll("#T1", pk1.parameterType) |
|
67 |
.replaceAll("#T2", pk2.parameterType); |
|
68 |
} |
|
69 |
||
70 |
int arity() { |
|
71 |
switch (this) { |
|
72 |
case NILARY_EXPR: |
|
73 |
case NILARY_STMT: return 0; |
|
74 |
case ONEARY_SHORT_EXPR: |
|
75 |
case ONEARY_SHORT_STMT: |
|
76 |
case ONEARY_EXPR: |
|
77 |
case ONEARY_STMT: return 1; |
|
78 |
case TWOARY_EXPR: |
|
79 |
case TWOARY_STMT: return 2; |
|
80 |
default: throw new AssertionError("Invalid lambda kind " + this); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
boolean isShort() { |
|
85 |
return this == ONEARY_SHORT_EXPR || |
|
86 |
this == ONEARY_SHORT_STMT; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
enum LambdaParameterKind { |
|
91 |
IMPLICIT(""), |
|
92 |
EXPLIICT_SIMPLE("A"), |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
93 |
EXPLIICT_SIMPLE_ARR1("A[]"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
94 |
EXPLIICT_SIMPLE_ARR2("A[][]"), |
11143 | 95 |
EXPLICIT_VARARGS("A..."), |
96 |
EXPLICIT_GENERIC1("A<X>"), |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
97 |
EXPLICIT_GENERIC2("A<? extends X, ? super Y>"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
98 |
EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
99 |
EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"), |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
100 |
EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]"); |
11143 | 101 |
|
102 |
String parameterType; |
|
103 |
||
104 |
LambdaParameterKind(String parameterType) { |
|
105 |
this.parameterType = parameterType; |
|
106 |
} |
|
107 |
||
108 |
boolean explicit() { |
|
109 |
return this != IMPLICIT; |
|
110 |
} |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
111 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
112 |
boolean isVarargs() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
113 |
return this == EXPLICIT_VARARGS || |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
114 |
this == EXPLICIT_GENERIC2_VARARGS; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
115 |
} |
11143 | 116 |
} |
117 |
||
118 |
enum ModifierKind { |
|
119 |
NONE(""), |
|
120 |
FINAL("final"), |
|
121 |
PUBLIC("public"); |
|
122 |
||
123 |
String modifier; |
|
124 |
||
125 |
ModifierKind(String modifier) { |
|
126 |
this.modifier = modifier; |
|
127 |
} |
|
128 |
||
129 |
boolean compatibleWith(LambdaParameterKind pk) { |
|
130 |
switch (this) { |
|
131 |
case PUBLIC: return false; |
|
132 |
case FINAL: return pk != LambdaParameterKind.IMPLICIT; |
|
133 |
case NONE: return true; |
|
134 |
default: throw new AssertionError("Invalid modifier kind " + this); |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
enum ExprKind { |
|
140 |
NONE("#L#S"), |
|
141 |
SINGLE_PAREN1("(#L#S)"), |
|
142 |
SINGLE_PAREN2("(#L)#S"), |
|
143 |
DOUBLE_PAREN1("((#L#S))"), |
|
144 |
DOUBLE_PAREN2("((#L)#S)"), |
|
145 |
DOUBLE_PAREN3("((#L))#S"); |
|
146 |
||
147 |
String expressionTemplate; |
|
148 |
||
149 |
ExprKind(String expressionTemplate) { |
|
150 |
this.expressionTemplate = expressionTemplate; |
|
151 |
} |
|
152 |
||
153 |
String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2, |
|
154 |
ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) { |
|
155 |
return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2)) |
|
156 |
.replaceAll("#S", sk.subExpression); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
enum SubExprKind { |
|
161 |
NONE(""), |
|
162 |
SELECT_FIELD(".f"), |
|
163 |
SELECT_METHOD(".f()"), |
|
164 |
SELECT_NEW(".new Foo()"), |
|
165 |
POSTINC("++"), |
|
166 |
POSTDEC("--"); |
|
167 |
||
168 |
String subExpression; |
|
169 |
||
170 |
SubExprKind(String subExpression) { |
|
171 |
this.subExpression = subExpression; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
public static void main(String... args) throws Exception { |
|
176 |
||
177 |
//create default shared JavaCompiler - reused across multiple compilations |
|
178 |
JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
179 |
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
|
180 |
||
181 |
for (LambdaKind lk : LambdaKind.values()) { |
|
182 |
for (LambdaParameterKind pk1 : LambdaParameterKind.values()) { |
|
183 |
if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue; |
|
184 |
for (LambdaParameterKind pk2 : LambdaParameterKind.values()) { |
|
185 |
if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue; |
|
186 |
for (ModifierKind mk1 : ModifierKind.values()) { |
|
187 |
if (mk1 != ModifierKind.NONE && lk.isShort()) continue; |
|
188 |
if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue; |
|
189 |
for (ModifierKind mk2 : ModifierKind.values()) { |
|
190 |
if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue; |
|
191 |
for (SubExprKind sk : SubExprKind.values()) { |
|
192 |
for (ExprKind ek : ExprKind.values()) { |
|
193 |
new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek) |
|
194 |
.run(comp, fm); |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
System.out.println("Total check executed: " + checkCount); |
|
203 |
} |
|
204 |
||
205 |
LambdaParameterKind pk1; |
|
206 |
LambdaParameterKind pk2; |
|
207 |
ModifierKind mk1; |
|
208 |
ModifierKind mk2; |
|
209 |
LambdaKind lk; |
|
210 |
SubExprKind sk; |
|
211 |
ExprKind ek; |
|
212 |
JavaSource source; |
|
213 |
DiagnosticChecker diagChecker; |
|
214 |
||
215 |
LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1, |
|
216 |
ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) { |
|
217 |
this.pk1 = pk1; |
|
218 |
this.pk2 = pk2; |
|
219 |
this.mk1 = mk1; |
|
220 |
this.mk2 = mk2; |
|
221 |
this.lk = lk; |
|
222 |
this.sk = sk; |
|
223 |
this.ek = ek; |
|
224 |
this.source = new JavaSource(); |
|
225 |
this.diagChecker = new DiagnosticChecker(); |
|
226 |
} |
|
227 |
||
228 |
class JavaSource extends SimpleJavaFileObject { |
|
229 |
||
230 |
String template = "class Test {\n" + |
|
231 |
" SAM s = #E;\n" + |
|
232 |
"}"; |
|
233 |
||
234 |
String source; |
|
235 |
||
236 |
public JavaSource() { |
|
237 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
238 |
source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk)); |
|
239 |
} |
|
240 |
||
241 |
@Override |
|
242 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
243 |
return source; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
|
248 |
JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
|
14547 | 249 |
null, null, Arrays.asList(source)); |
11143 | 250 |
try { |
251 |
ct.parse(); |
|
252 |
} catch (Throwable ex) { |
|
11381 | 253 |
throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true)); |
11143 | 254 |
} |
255 |
check(); |
|
256 |
} |
|
257 |
||
258 |
void check() { |
|
259 |
checkCount++; |
|
260 |
||
261 |
boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) || |
|
262 |
(lk.arity() > 1 && !mk2.compatibleWith(pk2)); |
|
263 |
||
264 |
if (lk.arity() == 2 && |
|
265 |
(pk1.explicit() != pk2.explicit() || |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14547
diff
changeset
|
266 |
pk1.isVarargs())) { |
11143 | 267 |
errorExpected = true; |
268 |
} |
|
269 |
||
270 |
if (errorExpected != diagChecker.errorFound) { |
|
271 |
throw new Error("invalid diagnostics for source:\n" + |
|
272 |
source.getCharContent(true) + |
|
273 |
"\nFound error: " + diagChecker.errorFound + |
|
274 |
"\nExpected error: " + errorExpected); |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
|
279 |
||
280 |
boolean errorFound; |
|
281 |
||
282 |
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
283 |
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
|
284 |
errorFound = true; |
|
285 |
} |
|
286 |
} |
|
287 |
} |
|
288 |
} |