author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 27388 | d694da45bd7a |
permissions | -rw-r--r-- |
14547 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27388
diff
changeset
|
2 |
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
14547 | 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 8003280 |
|
27 |
* @summary Add lambda tests |
|
28 |
* Test SAM conversion of lambda expressions in combinations of different contexts, |
|
29 |
* lambda body types(statement/expression), explict/implicit target type etc, to verify |
|
30 |
* SAM conversion being conducted successfully as expected. |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27388
diff
changeset
|
31 |
* @modules jdk.compiler |
14547 | 32 |
*/ |
33 |
||
34 |
import com.sun.source.util.JavacTask; |
|
35 |
import java.net.URI; |
|
36 |
import java.util.Arrays; |
|
37 |
import javax.tools.Diagnostic; |
|
38 |
import javax.tools.JavaCompiler; |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
39 |
import javax.tools.JavaFileManager; |
14547 | 40 |
import javax.tools.JavaFileObject; |
41 |
import javax.tools.SimpleJavaFileObject; |
|
42 |
import javax.tools.ToolProvider; |
|
43 |
||
44 |
public class SamConversionComboTest { |
|
45 |
||
46 |
enum FInterface { |
|
47 |
A("A", "interface A { Integer m(int i); }"), |
|
48 |
B("B", "interface B { int m(Integer i); }"), |
|
49 |
C("C", "interface C { int m(Integer i) throws Exception; }"); |
|
50 |
||
51 |
String interfaceType; |
|
52 |
String interfaceDef; |
|
53 |
||
54 |
FInterface(String interfaceType, String interfaceDef) { |
|
55 |
this.interfaceType = interfaceType; |
|
56 |
this.interfaceDef = interfaceDef; |
|
57 |
} |
|
58 |
||
59 |
String getParameterType() { |
|
60 |
switch(this) { |
|
61 |
case A: |
|
62 |
return "int"; |
|
63 |
case B: |
|
64 |
case C: |
|
65 |
return "Integer"; |
|
66 |
default: |
|
67 |
return null; |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
enum Context { |
|
73 |
ASSIGNMENT("#FType f = #LBody;"), |
|
74 |
METHOD_CALL("void method1(#FType f) { }\n" + |
|
75 |
" void method2() {\n" + |
|
76 |
" method1(#LBody);\n" + |
|
77 |
" }"), |
|
78 |
CONSTRUCTOR("X x = new X(#LBody);"), |
|
79 |
RETURN_OF_METHOD("#FType method1() {\n" + |
|
80 |
" return #LBody;\n" + |
|
81 |
"}"), |
|
82 |
ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (#FType)#LBody};"), |
|
83 |
LAMBDA_BODY("#FType f = n -> ((#FType)#LBody).m(n);"), |
|
84 |
CAST("void test() throws Exception { int n = ((#FType)#LBody).m(1); }"), |
|
85 |
CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #LBody : null;"); |
|
86 |
||
87 |
String context; |
|
88 |
||
89 |
Context(String context) { |
|
90 |
this.context = context; |
|
91 |
} |
|
92 |
||
93 |
String getContext(FInterface f, LambdaKind lk, LambdaBody lb, ReturnValue rv) { |
|
94 |
return context.replace("#FType", f.interfaceType).replace("#LBody", lb.getLambdaBody(f, lk, rv)); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
enum LambdaKind { |
|
99 |
EXPRESSION("#VAL"), |
|
100 |
STATEMENT("{return #VAL;}"), |
|
101 |
EXCEPTION_STMT("{throw new Exception();}"); |
|
102 |
||
103 |
String stmt; |
|
104 |
||
105 |
LambdaKind(String stmt) { |
|
106 |
this.stmt = stmt; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
enum ReturnValue { |
|
111 |
INT("i + 1"), |
|
112 |
INTEGER("new Integer(i+1)"), |
|
113 |
INT2("i.intValue() + 1"), |
|
114 |
STRING("i + \"\""), |
|
115 |
DOUBLE("i * 1.0"); |
|
116 |
||
117 |
String rValue; |
|
118 |
||
119 |
ReturnValue(String rValue) { |
|
120 |
this.rValue = rValue; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
enum LambdaBody { |
|
125 |
IMPLICIT("i -> #RET"),//type inferred |
|
126 |
EXPLICIT("(#Type i) -> #RET");//explicit type |
|
127 |
||
128 |
String bodyStr; |
|
129 |
||
130 |
LambdaBody(String bodyStr) { |
|
131 |
this.bodyStr = bodyStr; |
|
132 |
} |
|
133 |
||
134 |
String getLambdaBody(FInterface fi, LambdaKind lk, ReturnValue rv) { |
|
135 |
return bodyStr.replace("#Type", fi.getParameterType()).replace("#RET", lk.stmt.replace("#VAL", rv.rValue)); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
boolean checkSamConversion() { |
|
140 |
if(lambdaKind != LambdaKind.EXCEPTION_STMT && (returnValue == ReturnValue.DOUBLE || returnValue == ReturnValue.STRING)) //return type mismatch |
|
141 |
return false; |
|
142 |
if(context != Context.CONSTRUCTOR) {//context other than construcotr argument |
|
143 |
if(fInterface != FInterface.C && lambdaKind == LambdaKind.EXCEPTION_STMT) |
|
144 |
return false; |
|
145 |
if(fInterface == FInterface.A && returnValue == ReturnValue.INT2) |
|
146 |
return false; |
|
147 |
} |
|
148 |
else { //constructor argument context |
|
149 |
//match X(A a) or X(B b) or X(C c) |
|
150 |
if (lambdaKind == LambdaKind.EXCEPTION_STMT) { |
|
151 |
return false; //ambiguous target type |
|
152 |
} |
|
153 |
else if(lambdaBody == LambdaBody.IMPLICIT) { |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
14547
diff
changeset
|
154 |
return false; |
14547 | 155 |
} |
156 |
else { //explicit parameter type |
|
157 |
if(fInterface.getParameterType().equals("Integer")) //ambiguous target type |
|
158 |
//e.g. X x = new X((Integer i) -> i + 1); |
|
159 |
return false; |
|
160 |
if(returnValue == ReturnValue.INT2) |
|
161 |
//e.g. X x = new X(int i -> i.intValue() + 1); |
|
162 |
return false; |
|
163 |
} |
|
164 |
} |
|
165 |
return true; |
|
166 |
} |
|
167 |
||
168 |
SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") { |
|
169 |
public String toString() { |
|
170 |
String interfaces = ""; |
|
171 |
for(FInterface fi : FInterface.values()) |
|
172 |
interfaces += fi.interfaceDef + "\n"; |
|
173 |
return template.replace("#C", interfaces); |
|
174 |
} |
|
175 |
}; |
|
176 |
||
177 |
String clientTemplate = "class Client {\n" + |
|
178 |
" #Context\n" + |
|
179 |
"}\n\n" + |
|
180 |
||
181 |
"class X {\n" + |
|
182 |
" int value = 0;\n\n" + |
|
183 |
||
184 |
" X(A a) {\n" + |
|
185 |
" value = a.m(6);\n" + |
|
186 |
" }\n\n" + |
|
187 |
||
188 |
" X(B b) {\n" + |
|
189 |
" value = b.m(7);\n" + |
|
190 |
" }\n\n" + |
|
191 |
||
192 |
" X(C c) {\n" + |
|
193 |
" try {\n" + |
|
194 |
" value = c.m(8);\n" + |
|
195 |
" } catch (Exception e){}\n" + |
|
196 |
" }\n" + |
|
197 |
"}"; |
|
198 |
SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) { |
|
199 |
public String toString() { |
|
200 |
return template.replace("#Context", context.getContext(fInterface, lambdaKind, lambdaBody, returnValue)); |
|
201 |
} |
|
202 |
}; |
|
203 |
||
204 |
void test() throws Exception { |
|
205 |
System.out.println("\n===================================="); |
|
206 |
System.out.println(fInterface + ", " + context + ", " + lambdaKind + ", " + lambdaBody + ", " + returnValue); |
|
207 |
System.out.println(samSourceFile + "\n"); |
|
208 |
String clientFileStr = clientSourceFile.toString(); |
|
209 |
System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n"))); |
|
210 |
||
211 |
DiagnosticChecker dc = new DiagnosticChecker(); |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
212 |
JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile)); |
24066
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
22448
diff
changeset
|
213 |
try { |
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
22448
diff
changeset
|
214 |
ct.analyze(); |
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
22448
diff
changeset
|
215 |
} catch (Exception e) { |
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
22448
diff
changeset
|
216 |
throw new AssertionError("failing SAM source file \n" + samSourceFile + "\n\n" + "failing client source file \n"+ clientSourceFile); |
1dfb66929538
8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents:
22448
diff
changeset
|
217 |
} |
14547 | 218 |
if (dc.errorFound == checkSamConversion()) { |
219 |
throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile); |
|
220 |
} |
|
221 |
count++; |
|
222 |
} |
|
223 |
||
224 |
abstract class SourceFile extends SimpleJavaFileObject { |
|
225 |
||
226 |
protected String template; |
|
227 |
||
228 |
public SourceFile(String filename, String template) { |
|
229 |
super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); |
|
230 |
this.template = template; |
|
231 |
} |
|
232 |
||
233 |
@Override |
|
234 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
235 |
return toString(); |
|
236 |
} |
|
237 |
||
238 |
public abstract String toString(); |
|
239 |
} |
|
240 |
||
241 |
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
|
242 |
||
243 |
boolean errorFound = false; |
|
244 |
||
245 |
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
246 |
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
|
247 |
errorFound = true; |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
FInterface fInterface; |
|
253 |
Context context; |
|
254 |
LambdaBody lambdaBody; |
|
255 |
LambdaKind lambdaKind; |
|
256 |
ReturnValue returnValue; |
|
257 |
static int count = 0; |
|
258 |
||
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
259 |
static JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
260 |
static JavaFileManager fm = comp.getStandardFileManager(null, null, null); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
261 |
|
14547 | 262 |
SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) { |
263 |
fInterface = f; |
|
264 |
context = c; |
|
265 |
lambdaKind = lk; |
|
266 |
lambdaBody = lb; |
|
267 |
returnValue = rv; |
|
268 |
} |
|
269 |
||
270 |
public static void main(String[] args) throws Exception { |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
271 |
try { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
272 |
for(Context ct : Context.values()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
273 |
for (FInterface fi : FInterface.values()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
274 |
for (LambdaKind lk: LambdaKind.values()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
275 |
for (LambdaBody lb : LambdaBody.values()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
276 |
for(ReturnValue rv : ReturnValue.values()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
277 |
new SamConversionComboTest(fi, ct, lb, lk, rv).test(); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
278 |
} |
14547 | 279 |
} |
280 |
} |
|
281 |
} |
|
282 |
} |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
283 |
System.out.println("total tests: " + count); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
284 |
} finally { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
24066
diff
changeset
|
285 |
fm.close(); |
14547 | 286 |
} |
287 |
} |
|
288 |
} |