|
1 /* |
|
2 * Copyright (c) 2012, 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 8003280 |
|
27 * @summary Add lambda tests |
|
28 * Test SAM conversion of method references in combinations of different contexts, |
|
29 * lambda body types(statement/expression), boxing/unboxing etc, to verify |
|
30 * SAM conversion being conducted successfully as expected. |
|
31 */ |
|
32 |
|
33 import com.sun.source.util.JavacTask; |
|
34 import java.net.URI; |
|
35 import java.util.Arrays; |
|
36 import javax.tools.Diagnostic; |
|
37 import javax.tools.JavaCompiler; |
|
38 import javax.tools.JavaFileObject; |
|
39 import javax.tools.SimpleJavaFileObject; |
|
40 import javax.tools.ToolProvider; |
|
41 import javax.tools.StandardJavaFileManager; |
|
42 |
|
43 public class SamConversionComboTest { |
|
44 |
|
45 enum FInterface { |
|
46 A("A", "interface A { Integer m(int i); }"), |
|
47 B("B", "interface B { int m(Integer i); }"), |
|
48 C("C", "interface C { int m(int i) throws Exception; }"); |
|
49 |
|
50 String interfaceType; |
|
51 String interfaceDef; |
|
52 |
|
53 FInterface(String interfaceType, String interfaceDef) { |
|
54 this.interfaceType = interfaceType; |
|
55 this.interfaceDef = interfaceDef; |
|
56 } |
|
57 } |
|
58 |
|
59 enum Context { |
|
60 ASSIGNMENT("#FType f = #MR;"), |
|
61 METHOD_CALL("void method1(#FType f) { }\n" + |
|
62 "void method2() {\n" + |
|
63 " method1(#MR);\n" + |
|
64 "}"), |
|
65 CONSTRUCTOR("X x = new X(#MR);"), |
|
66 RETURN_OF_METHOD("#FType method1() {\n" + |
|
67 " return #MR;\n" + |
|
68 "}"), |
|
69 ARRAY_INITIALIZER("#FType[] oarray = {#MR};"), |
|
70 LAMBDA_BODY("#FType f = n -> ((#FType)#MR).m(n);"), |
|
71 CAST("void test() throws Exception { int n = ((#FType)#MR).m(1); }"), |
|
72 CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #MR : null;"); |
|
73 |
|
74 String context; |
|
75 |
|
76 Context(String context) { |
|
77 this.context = context; |
|
78 } |
|
79 |
|
80 String getContext(FInterface f, MethodReference mr) { |
|
81 return context.replace("#FType", f.interfaceType).replace("#MR", mr.mrValue); |
|
82 } |
|
83 } |
|
84 |
|
85 enum MethodReference { |
|
86 METHOD1("X::method1"), |
|
87 METHOD2("new X()::method2"), |
|
88 METHOD3("X::method3"), |
|
89 METHOD4("new X()::method4"), |
|
90 METHOD5("new X()::method5"), |
|
91 METHOD6("X::method6"), |
|
92 METHOD7("X::method7"), |
|
93 METHOD8("X::method8"); |
|
94 |
|
95 String mrValue; |
|
96 |
|
97 MethodReference(String mr) { |
|
98 mrValue = mr; |
|
99 } |
|
100 } |
|
101 |
|
102 enum MethodDef { |
|
103 METHOD1(" static Integer method1(int n) {\n" + |
|
104 " return n + 1;\n" + |
|
105 " }\n", 0), |
|
106 METHOD2(" int method2(Integer n) {\n" + |
|
107 " return value == 0 ? n + 2 : n + value;\n" + |
|
108 " }\n", 1), |
|
109 METHOD3(" static int method3(int n) {\n" + |
|
110 " return n + 3;\n" + |
|
111 " }\n", 2), |
|
112 METHOD4(" Integer method4(Integer n) {\n" + |
|
113 " return value == 0 ? n + 4 : n + value;\n" + |
|
114 " }\n", 3), |
|
115 METHOD5(" Integer method5(Integer n) {\n" + |
|
116 " return value == 0 ? new Integer(n + 5) : new Integer(n + value);\n" + |
|
117 " }\n", 4), |
|
118 METHOD6(" static int method6(Integer n) throws Exception{\n" + |
|
119 " throw new Exception();\n" + |
|
120 " }\n", 5), |
|
121 METHOD7(" static int method7(String s){\n" + |
|
122 " return s.length();\n" + |
|
123 " }\n", 6), |
|
124 METHOD8(" static String method8(Integer n){\n" + |
|
125 " return n + \"\";\n" + |
|
126 " }\n", 7); |
|
127 |
|
128 String methodStr; |
|
129 int index; |
|
130 |
|
131 MethodDef(String ms, int i) { |
|
132 methodStr = ms; |
|
133 index = i; |
|
134 } |
|
135 |
|
136 MethodReference getMethodReference() { |
|
137 return MethodReference.values()[index]; |
|
138 } |
|
139 } |
|
140 |
|
141 SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") { |
|
142 public String toString() { |
|
143 String interfaces = ""; |
|
144 for(FInterface fi : FInterface.values()) |
|
145 interfaces += fi.interfaceDef + "\n"; |
|
146 return template.replace("#C", interfaces); |
|
147 } |
|
148 }; |
|
149 |
|
150 String clientTemplate = "class Client {\n" + |
|
151 " #Context\n" + |
|
152 "}\n\n" + |
|
153 |
|
154 "class X {\n" + |
|
155 " int value = 0;\n\n" + |
|
156 |
|
157 " X() {\n" + |
|
158 " }\n\n" + |
|
159 |
|
160 " X(A a) {\n" + |
|
161 " value = a.m(9);\n" + |
|
162 " }\n\n" + |
|
163 |
|
164 " X(B b) {\n" + |
|
165 " value = b.m(9);\n" + |
|
166 " }\n\n" + |
|
167 |
|
168 " X(C c) {\n" + |
|
169 " try {\n" + |
|
170 " value = c.m(9);\n" + |
|
171 " } catch (Exception e){}\n" + |
|
172 " }\n\n" + |
|
173 |
|
174 "#MethodDef" + |
|
175 "}"; |
|
176 |
|
177 SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) { |
|
178 public String toString() { |
|
179 return template.replace("#Context", context.getContext(fInterface, methodReference)).replace("#MethodDef", methodDef.methodStr); |
|
180 } |
|
181 }; |
|
182 |
|
183 boolean checkSamConversion() { |
|
184 if(methodDef == MethodDef.METHOD7 || methodDef == MethodDef.METHOD8)//method signature mismatch |
|
185 return false; |
|
186 if(context != Context.CONSTRUCTOR && fInterface != FInterface.C && methodDef == MethodDef.METHOD6) |
|
187 //method that throws exceptions not thrown by the interface method is a mismatch |
|
188 return false; |
|
189 if(context == Context.CONSTRUCTOR && |
|
190 methodReference != MethodReference.METHOD1 && |
|
191 methodReference != MethodReference.METHOD2 && |
|
192 methodReference != MethodReference.METHOD3)//ambiguous reference |
|
193 return false; |
|
194 return true; |
|
195 } |
|
196 |
|
197 void test() throws Exception { |
|
198 System.out.println("\n===================================="); |
|
199 System.out.println(fInterface + ", " + context + ", " + methodReference); |
|
200 System.out.println(samSourceFile + "\n" + clientSourceFile); |
|
201 |
|
202 DiagnosticChecker dc = new DiagnosticChecker(); |
|
203 JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile)); |
|
204 ct.analyze(); |
|
205 if (dc.errorFound == checkSamConversion()) { |
|
206 throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile); |
|
207 } |
|
208 count++; |
|
209 } |
|
210 |
|
211 abstract class SourceFile extends SimpleJavaFileObject { |
|
212 |
|
213 protected String template; |
|
214 |
|
215 public SourceFile(String filename, String template) { |
|
216 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); |
|
217 this.template = template; |
|
218 } |
|
219 |
|
220 @Override |
|
221 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
222 return toString(); |
|
223 } |
|
224 |
|
225 public abstract String toString(); |
|
226 } |
|
227 |
|
228 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
|
229 |
|
230 boolean errorFound = false; |
|
231 |
|
232 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
233 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
|
234 errorFound = true; |
|
235 } |
|
236 } |
|
237 } |
|
238 |
|
239 FInterface fInterface; |
|
240 Context context; |
|
241 MethodDef methodDef; |
|
242 MethodReference methodReference; |
|
243 static int count = 0; |
|
244 |
|
245 static JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
246 static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
|
247 |
|
248 SamConversionComboTest(FInterface f, Context c, MethodDef md) { |
|
249 fInterface = f; |
|
250 context = c; |
|
251 methodDef = md; |
|
252 methodReference = md.getMethodReference(); |
|
253 } |
|
254 |
|
255 public static void main(String[] args) throws Exception { |
|
256 for(Context ct : Context.values()) { |
|
257 for (FInterface fi : FInterface.values()) { |
|
258 for (MethodDef md: MethodDef.values()) { |
|
259 new SamConversionComboTest(fi, ct, md).test(); |
|
260 } |
|
261 } |
|
262 } |
|
263 System.out.println("total tests: " + count); |
|
264 } |
|
265 } |