24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 6945418 6993978 |
26 * @bug 6945418 6993978 |
27 * @summary Project Coin: Simplified Varargs Method Invocation |
27 * @summary Project Coin: Simplified Varargs Method Invocation |
28 * @author mcimadamore |
28 * @author mcimadamore |
|
29 * @library ../../lib |
|
30 * @build JavacTestingAbstractThreadedTest |
29 * @run main Warn4 |
31 * @run main Warn4 |
30 */ |
32 */ |
31 import com.sun.source.util.JavacTask; |
33 |
32 import com.sun.tools.javac.api.JavacTool; |
|
33 import java.net.URI; |
34 import java.net.URI; |
34 import java.util.Arrays; |
35 import java.util.Arrays; |
35 import java.util.Set; |
36 import java.util.Set; |
36 import java.util.HashSet; |
37 import java.util.HashSet; |
37 import javax.tools.Diagnostic; |
38 import javax.tools.Diagnostic; |
38 import javax.tools.JavaCompiler; |
39 import javax.tools.JavaCompiler; |
39 import javax.tools.JavaFileObject; |
40 import javax.tools.JavaFileObject; |
40 import javax.tools.SimpleJavaFileObject; |
41 import javax.tools.SimpleJavaFileObject; |
41 import javax.tools.StandardJavaFileManager; |
|
42 import javax.tools.ToolProvider; |
42 import javax.tools.ToolProvider; |
43 |
43 import com.sun.source.util.JavacTask; |
44 public class Warn4 { |
44 |
|
45 public class Warn4 |
|
46 extends JavacTestingAbstractThreadedTest |
|
47 implements Runnable { |
45 |
48 |
46 final static Warning[] error = null; |
49 final static Warning[] error = null; |
47 final static Warning[] none = new Warning[] {}; |
50 final static Warning[] none = new Warning[] {}; |
48 final static Warning[] vararg = new Warning[] { Warning.VARARGS }; |
51 final static Warning[] vararg = new Warning[] { Warning.VARARGS }; |
49 final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED }; |
52 final static Warning[] unchecked = new Warning[] { Warning.UNCHECKED }; |
50 final static Warning[] both = new Warning[] { Warning.VARARGS, Warning.UNCHECKED }; |
53 final static Warning[] both = |
|
54 new Warning[] { Warning.VARARGS, Warning.UNCHECKED }; |
51 |
55 |
52 enum Warning { |
56 enum Warning { |
53 UNCHECKED("generic.array.creation"), |
57 UNCHECKED("generic.array.creation"), |
54 VARARGS("varargs.non.reifiable.type"); |
58 VARARGS("varargs.non.reifiable.type"); |
55 |
59 |
170 for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) { |
177 for (SuppressLevel suppressLevelDecl : SuppressLevel.values()) { |
171 for (ModifierKind modKind : ModifierKind.values()) { |
178 for (ModifierKind modKind : ModifierKind.values()) { |
172 for (Signature vararg_meth : Signature.values()) { |
179 for (Signature vararg_meth : Signature.values()) { |
173 for (Signature client_meth : Signature.values()) { |
180 for (Signature client_meth : Signature.values()) { |
174 if (vararg_meth.isApplicableTo(client_meth)) { |
181 if (vararg_meth.isApplicableTo(client_meth)) { |
175 test(sourceLevel, |
182 pool.execute(new Warn4(sourceLevel, |
176 trustMe, |
183 trustMe, |
177 suppressLevelClient, |
184 suppressLevelClient, |
178 suppressLevelDecl, |
185 suppressLevelDecl, |
179 modKind, |
186 modKind, |
180 vararg_meth, |
187 vararg_meth, |
181 client_meth); |
188 client_meth)); |
182 } |
189 } |
183 } |
190 } |
184 } |
191 } |
185 } |
192 } |
186 } |
193 } |
187 } |
194 } |
188 } |
195 } |
189 } |
196 } |
190 } |
197 |
191 |
198 checkAfterExec(); |
192 // Create a single file manager and reuse it for each compile to save time. |
199 } |
193 static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
200 |
194 |
201 SourceLevel sourceLevel; |
195 static void test(SourceLevel sourceLevel, TrustMe trustMe, SuppressLevel suppressLevelClient, |
202 TrustMe trustMe; |
196 SuppressLevel suppressLevelDecl, ModifierKind modKind, Signature vararg_meth, Signature client_meth) throws Exception { |
203 SuppressLevel suppressLevelClient; |
|
204 SuppressLevel suppressLevelDecl; |
|
205 ModifierKind modKind; |
|
206 Signature vararg_meth; |
|
207 Signature client_meth; |
|
208 DiagnosticChecker diagChecker; |
|
209 |
|
210 public Warn4(SourceLevel sourceLevel, TrustMe trustMe, |
|
211 SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, |
|
212 ModifierKind modKind, Signature vararg_meth, Signature client_meth) { |
|
213 this.sourceLevel = sourceLevel; |
|
214 this.trustMe = trustMe; |
|
215 this.suppressLevelClient = suppressLevelClient; |
|
216 this.suppressLevelDecl = suppressLevelDecl; |
|
217 this.modKind = modKind; |
|
218 this.vararg_meth = vararg_meth; |
|
219 this.client_meth = client_meth; |
|
220 this.diagChecker = new DiagnosticChecker(); |
|
221 } |
|
222 |
|
223 @Override |
|
224 public void run() { |
|
225 int id = checkCount.incrementAndGet(); |
197 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
226 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
198 JavaSource source = new JavaSource(trustMe, suppressLevelClient, suppressLevelDecl, modKind, vararg_meth, client_meth); |
227 JavaSource source = new JavaSource(id); |
199 DiagnosticChecker dc = new DiagnosticChecker(); |
228 JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), diagChecker, |
200 JavacTask ct = (JavacTask)tool.getTask(null, fm, dc, |
|
201 Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey), |
229 Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey), |
202 null, Arrays.asList(source)); |
230 null, Arrays.asList(source)); |
203 ct.generate(); //to get mandatory notes |
231 ct.call(); //to get mandatory notes |
204 check(dc.warnings, sourceLevel, |
232 check(source, new boolean[] {vararg_meth.giveUnchecked(client_meth), |
205 new boolean[] {vararg_meth.giveUnchecked(client_meth), |
233 vararg_meth.giveVarargs(client_meth)}); |
206 vararg_meth.giveVarargs(client_meth)}, |
234 } |
207 source, trustMe, suppressLevelClient, suppressLevelDecl, modKind); |
235 |
208 } |
236 void check(JavaSource source, boolean[] warnArr) { |
209 |
|
210 static void check(Set<Warning> warnings, SourceLevel sourceLevel, boolean[] warnArr, JavaSource source, |
|
211 TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, ModifierKind modKind) { |
|
212 boolean badOutput = false; |
237 boolean badOutput = false; |
213 for (Warning wkind : Warning.values()) { |
238 for (Warning wkind : Warning.values()) { |
214 boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel, |
239 boolean isSuppressed = wkind.isSuppressed(trustMe, sourceLevel, |
215 suppressLevelClient, suppressLevelDecl, modKind); |
240 suppressLevelClient, suppressLevelDecl, modKind); |
216 System.out.println("SUPPRESSED = " + isSuppressed); |
241 System.out.println("SUPPRESSED = " + isSuppressed); |
217 badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != warnings.contains(wkind); |
242 badOutput |= (warnArr[wkind.ordinal()] && !isSuppressed) != |
|
243 diagChecker.warnings.contains(wkind); |
218 } |
244 } |
219 if (badOutput) { |
245 if (badOutput) { |
220 throw new Error("invalid diagnostics for source:\n" + |
246 throw new Error("invalid diagnostics for source:\n" + |
221 source.getCharContent(true) + |
247 source.getCharContent(true) + |
222 "\nExpected unchecked warning: " + warnArr[0] + |
248 "\nExpected unchecked warning: " + warnArr[0] + |
223 "\nExpected unsafe vararg warning: " + warnArr[1] + |
249 "\nExpected unsafe vararg warning: " + warnArr[1] + |
224 "\nWarnings: " + warnings + |
250 "\nWarnings: " + diagChecker.warnings + |
225 "\nSource level: " + sourceLevel); |
251 "\nSource level: " + sourceLevel); |
226 } |
252 } |
227 } |
253 } |
228 |
254 |
229 static class JavaSource extends SimpleJavaFileObject { |
255 class JavaSource extends SimpleJavaFileObject { |
230 |
256 |
231 String source; |
257 String source; |
232 |
258 |
233 public JavaSource(TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, |
259 public JavaSource(int id) { |
234 ModifierKind modKind, Signature vararg_meth, Signature client_meth) { |
260 super(URI.create(String.format("myfo:/Test%d.java", id)), |
235 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
261 JavaFileObject.Kind.SOURCE); |
236 String meth1 = vararg_meth.template.replace("#arity", "..."); |
262 String meth1 = vararg_meth.template.replace("#arity", "..."); |
237 meth1 = meth1.replace("#name", "m"); |
263 meth1 = meth1.replace("#name", "m"); |
238 meth1 = meth1.replace("#body", ""); |
264 meth1 = meth1.replace("#body", ""); |
239 meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + modKind.mod + meth1; |
265 meth1 = trustMe.anno + "\n" + suppressLevelDecl.getSuppressAnno() + |
|
266 modKind.mod + meth1; |
240 String meth2 = client_meth.template.replace("#arity", ""); |
267 String meth2 = client_meth.template.replace("#arity", ""); |
241 meth2 = meth2.replace("#name", "test"); |
268 meth2 = meth2.replace("#name", "test"); |
242 meth2 = meth2.replace("#body", "m(arg);"); |
269 meth2 = meth2.replace("#body", "m(arg);"); |
243 meth2 = suppressLevelClient.getSuppressAnno() + meth2; |
270 meth2 = suppressLevelClient.getSuppressAnno() + meth2; |
244 source = "import java.util.List;\n" + |
271 source = String.format("import java.util.List;\n" + |
245 "class Test {\n" + meth1 + |
272 "class Test%s {\n %s \n %s \n } \n", id, meth1, meth2); |
246 "\n" + meth2 + "\n}\n"; |
|
247 } |
273 } |
248 |
274 |
249 @Override |
275 @Override |
250 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
276 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
251 return source; |
277 return source; |
252 } |
278 } |
253 } |
279 } |
254 |
280 |
255 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
281 static class DiagnosticChecker |
|
282 implements javax.tools.DiagnosticListener<JavaFileObject> { |
256 |
283 |
257 Set<Warning> warnings = new HashSet<>(); |
284 Set<Warning> warnings = new HashSet<>(); |
258 |
285 |
259 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
286 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
260 if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING || |
287 if (diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING || |