Mon, 17 Oct 2016 21:20:17 -0700
8168115: Remove and retag jdk8u122-b03 in 8u repo
Reviewed-by: coffeys
1 /*
2 * Copyright (c) 2011, 2014, 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 */
24 /*
25 * @test
26 * @bug 7098660 8014649 8034223
27 * @summary Test harness for overload resolution/inference tests
28 * @library /tools/javac/lib
29 * @build JavacTestingAbstractProcessor ResolveHarness
30 * @run main ResolveHarness
31 */
33 import com.sun.source.util.JavacTask;
34 import com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper;
35 import com.sun.tools.javac.code.Flags;
36 import com.sun.tools.javac.code.Symbol;
37 import com.sun.tools.javac.code.Type.MethodType;
38 import com.sun.tools.javac.util.JCDiagnostic;
40 import java.io.File;
41 import java.util.Set;
42 import java.util.Arrays;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.Map;
51 import javax.annotation.processing.AbstractProcessor;
52 import javax.annotation.processing.RoundEnvironment;
53 import javax.annotation.processing.SupportedAnnotationTypes;
54 import javax.lang.model.element.Element;
55 import javax.lang.model.element.TypeElement;
56 import javax.tools.Diagnostic;
57 import javax.tools.Diagnostic.Kind;
58 import javax.tools.DiagnosticListener;
59 import javax.tools.JavaCompiler;
60 import javax.tools.JavaFileObject;
61 import javax.tools.StandardJavaFileManager;
62 import javax.tools.ToolProvider;
64 import static javax.tools.StandardLocation.*;
66 public class ResolveHarness implements javax.tools.DiagnosticListener<JavaFileObject> {
68 static int nerrors = 0;
70 static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
71 static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
73 public static void main(String[] args) throws Exception {
74 fm.setLocation(SOURCE_PATH,
75 Arrays.asList(new File(System.getProperty("test.src"), "tests")));
76 for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(JavaFileObject.Kind.SOURCE), true)) {
77 new ResolveHarness(jfo).check();
78 }
79 if (nerrors > 0) {
80 throw new AssertionError("Errors were found");
81 }
82 }
85 JavaFileObject jfo;
86 DiagnosticProcessor[] diagProcessors;
87 Map<ElementKey, Candidate> candidatesMap = new HashMap<ElementKey, Candidate>();
88 Set<String> declaredKeys = new HashSet<>();
89 List<Diagnostic<? extends JavaFileObject>> diags = new ArrayList<>();
90 List<ElementKey> seenCandidates = new ArrayList<>();
91 Map<String, String> predefTranslationMap = new HashMap<>();
93 protected ResolveHarness(JavaFileObject jfo) {
94 this.jfo = jfo;
95 this.diagProcessors = new DiagnosticProcessor[] {
96 new VerboseResolutionNoteProcessor(),
97 new VerboseDeferredInferenceNoteProcessor(),
98 new ErrorProcessor()
99 };
100 predefTranslationMap.put("+", "_plus");
101 predefTranslationMap.put("-", "_minus");
102 predefTranslationMap.put("~", "_not");
103 predefTranslationMap.put("++", "_plusplus");
104 predefTranslationMap.put("--", "_minusminus");
105 predefTranslationMap.put("!", "_bang");
106 predefTranslationMap.put("*", "_mul");
107 predefTranslationMap.put("/", "_div");
108 predefTranslationMap.put("%", "_mod");
109 predefTranslationMap.put("&", "_and");
110 predefTranslationMap.put("|", "_or");
111 predefTranslationMap.put("^", "_xor");
112 predefTranslationMap.put("<<", "_lshift");
113 predefTranslationMap.put(">>", "_rshift");
114 predefTranslationMap.put("<<<", "_lshiftshift");
115 predefTranslationMap.put(">>>", "_rshiftshift");
116 predefTranslationMap.put("<", "_lt");
117 predefTranslationMap.put(">", "_gt");
118 predefTranslationMap.put("<=", "_lteq");
119 predefTranslationMap.put(">=", "_gteq");
120 predefTranslationMap.put("==", "_eq");
121 predefTranslationMap.put("!=", "_neq");
122 predefTranslationMap.put("&&", "_andand");
123 predefTranslationMap.put("||", "_oror");
124 }
126 protected void check() throws Exception {
127 String[] options = {
128 "-XDshouldStopPolicy=ATTR",
129 "-XDverboseResolution=success,failure,applicable,inapplicable,deferred-inference,predef"
130 };
132 AbstractProcessor[] processors = { new ResolveCandidateFinder(), null };
134 @SuppressWarnings("unchecked")
135 DiagnosticListener<? super JavaFileObject>[] diagListeners =
136 new DiagnosticListener[] { new DiagnosticHandler(false), new DiagnosticHandler(true) };
138 for (int i = 0 ; i < options.length ; i ++) {
139 JavacTask ct = (JavacTask)comp.getTask(null, fm, diagListeners[i],
140 Arrays.asList(options[i]), null, Arrays.asList(jfo));
141 if (processors[i] != null) {
142 ct.setProcessors(Collections.singleton(processors[i]));
143 }
144 ct.analyze();
145 }
147 //check diags
148 for (Diagnostic<? extends JavaFileObject> diag : diags) {
149 for (DiagnosticProcessor proc : diagProcessors) {
150 if (proc.matches(diag)) {
151 proc.process(diag);
152 break;
153 }
154 }
155 }
156 //check all candidates have been used up
157 for (Map.Entry<ElementKey, Candidate> entry : candidatesMap.entrySet()) {
158 if (!seenCandidates.contains(entry.getKey())) {
159 error("Redundant @Candidate annotation on method " + entry.getKey().elem + " sig = " + entry.getKey().elem.asType());
160 }
161 }
162 }
164 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
165 diags.add(diagnostic);
166 }
168 Candidate getCandidateAtPos(Element methodSym, long line, long col) {
169 Candidate c = candidatesMap.get(new ElementKey(methodSym));
170 if (c != null) {
171 Pos pos = c.pos();
172 if (!pos.userDefined() ||
173 (pos.line() == line && pos.col() == col)) {
174 seenCandidates.add(new ElementKey(methodSym));
175 return c;
176 }
177 } else {
178 error("Missing @Candidate annotation on method " + methodSym);
179 }
180 return null;
181 }
183 void checkSig(Candidate c, Element methodSym, MethodType mtype) {
184 if (c.sig().length() > 0 && !c.sig().equals(mtype.toString())) {
185 error("Inferred type mismatch for method: " + methodSym);
186 }
187 }
189 protected void error(String msg) {
190 nerrors++;
191 System.err.printf("Error occurred while checking file: %s\nreason: %s\n", jfo.getName(), msg);
192 }
194 /**
195 * Base class for diagnostic processor. It provides methods for matching and
196 * processing a given diagnostic object (overridden by subclasses).
197 */
198 abstract class DiagnosticProcessor {
200 List<String> codes;
201 Diagnostic.Kind kind;
203 public DiagnosticProcessor(Kind kind, String... codes) {
204 this.codes = Arrays.asList(codes);
205 this.kind = kind;
206 }
208 abstract void process(Diagnostic<? extends JavaFileObject> diagnostic);
210 boolean matches(Diagnostic<? extends JavaFileObject> diagnostic) {
211 return (codes.isEmpty() || codes.contains(diagnostic.getCode())) &&
212 diagnostic.getKind() == kind;
213 }
215 JCDiagnostic asJCDiagnostic(Diagnostic<? extends JavaFileObject> diagnostic) {
216 if (diagnostic instanceof JCDiagnostic) {
217 return (JCDiagnostic)diagnostic;
218 } else if (diagnostic instanceof DiagnosticSourceUnwrapper) {
219 return ((DiagnosticSourceUnwrapper)diagnostic).d;
220 } else {
221 throw new AssertionError("Cannot convert diagnostic to JCDiagnostic: " + diagnostic.getClass().getName());
222 }
223 }
225 List<JCDiagnostic> subDiagnostics(Diagnostic<? extends JavaFileObject> diagnostic) {
226 JCDiagnostic diag = asJCDiagnostic(diagnostic);
227 if (diag instanceof JCDiagnostic.MultilineDiagnostic) {
228 return ((JCDiagnostic.MultilineDiagnostic)diag).getSubdiagnostics();
229 } else {
230 throw new AssertionError("Cannot extract subdiagnostics: " + diag.getClass().getName());
231 }
232 }
233 }
235 /**
236 * Processor for verbose resolution notes generated by javac. The processor
237 * checks that the diagnostic is associated with a method declared by
238 * a class annotated with the special @TraceResolve marker annotation. If
239 * that's the case, all subdiagnostics (one for each resolution candidate)
240 * are checked against the corresponding @Candidate annotations, using
241 * a VerboseCandidateSubdiagProcessor.
242 */
243 class VerboseResolutionNoteProcessor extends DiagnosticProcessor {
245 VerboseResolutionNoteProcessor() {
246 super(Kind.NOTE,
247 "compiler.note.verbose.resolve.multi",
248 "compiler.note.verbose.resolve.multi.1");
249 }
251 @Override
252 void process(Diagnostic<? extends JavaFileObject> diagnostic) {
253 Element siteSym = getSiteSym(diagnostic);
254 if (siteSym.getSimpleName().length() != 0 &&
255 ((Symbol)siteSym).outermostClass().getAnnotation(TraceResolve.class) == null) {
256 return;
257 }
258 int candidateIdx = 0;
259 for (JCDiagnostic d : subDiagnostics(diagnostic)) {
260 boolean isMostSpecific = candidateIdx++ == mostSpecific(diagnostic);
261 VerboseCandidateSubdiagProcessor subProc =
262 new VerboseCandidateSubdiagProcessor(isMostSpecific, phase(diagnostic), success(diagnostic));
263 if (subProc.matches(d)) {
264 subProc.process(d);
265 } else {
266 throw new AssertionError("Bad subdiagnostic: " + d.getCode());
267 }
268 }
269 }
271 Element getSiteSym(Diagnostic<? extends JavaFileObject> diagnostic) {
272 return (Element)asJCDiagnostic(diagnostic).getArgs()[1];
273 }
275 int mostSpecific(Diagnostic<? extends JavaFileObject> diagnostic) {
276 return success(diagnostic) ?
277 (Integer)asJCDiagnostic(diagnostic).getArgs()[2] : -1;
278 }
280 boolean success(Diagnostic<? extends JavaFileObject> diagnostic) {
281 return diagnostic.getCode().equals("compiler.note.verbose.resolve.multi");
282 }
284 Phase phase(Diagnostic<? extends JavaFileObject> diagnostic) {
285 return Phase.fromString(asJCDiagnostic(diagnostic).getArgs()[3].toString());
286 }
287 }
289 /**
290 * Processor for verbose resolution subdiagnostic notes generated by javac.
291 * The processor checks that the details of the overload candidate
292 * match against the info contained in the corresponding @Candidate
293 * annotation (if any).
294 */
295 class VerboseCandidateSubdiagProcessor extends DiagnosticProcessor {
297 boolean mostSpecific;
298 Phase phase;
299 boolean success;
301 public VerboseCandidateSubdiagProcessor(boolean mostSpecific, Phase phase, boolean success) {
302 super(Kind.OTHER,
303 "compiler.misc.applicable.method.found",
304 "compiler.misc.applicable.method.found.1",
305 "compiler.misc.not.applicable.method.found");
306 this.mostSpecific = mostSpecific;
307 this.phase = phase;
308 this.success = success;
309 }
311 @Override
312 void process(Diagnostic<? extends JavaFileObject> diagnostic) {
313 Symbol methodSym = (Symbol)methodSym(diagnostic);
314 if ((methodSym.flags() & Flags.GENERATEDCONSTR) != 0) {
315 //skip resolution of default constructor (put there by javac)
316 return;
317 }
318 Candidate c = getCandidateAtPos(methodSym,
319 asJCDiagnostic(diagnostic).getLineNumber(),
320 asJCDiagnostic(diagnostic).getColumnNumber());
321 if (c == null) {
322 return; //nothing to check
323 }
325 if (c.applicable().length == 0 && c.mostSpecific()) {
326 error("Inapplicable method cannot be most specific " + methodSym);
327 }
329 if (isApplicable(diagnostic) != Arrays.asList(c.applicable()).contains(phase)) {
330 error("Invalid candidate's applicability " + methodSym);
331 }
333 if (success) {
334 for (Phase p : c.applicable()) {
335 if (phase.ordinal() < p.ordinal()) {
336 error("Invalid phase " + p + " on method " + methodSym);
337 }
338 }
339 }
341 if (Arrays.asList(c.applicable()).contains(phase)) { //applicable
342 if (c.mostSpecific() != mostSpecific) {
343 error("Invalid most specific value for method " + methodSym + " " + new ElementKey(methodSym).key);
344 }
345 MethodType mtype = getSig(diagnostic);
346 if (mtype != null) {
347 checkSig(c, methodSym, mtype);
348 }
349 }
350 }
352 boolean isApplicable(Diagnostic<? extends JavaFileObject> diagnostic) {
353 return !diagnostic.getCode().equals("compiler.misc.not.applicable.method.found");
354 }
356 Element methodSym(Diagnostic<? extends JavaFileObject> diagnostic) {
357 return (Element)asJCDiagnostic(diagnostic).getArgs()[1];
358 }
360 MethodType getSig(Diagnostic<? extends JavaFileObject> diagnostic) {
361 JCDiagnostic details = (JCDiagnostic)asJCDiagnostic(diagnostic).getArgs()[2];
362 if (details == null) {
363 return null;
364 } else if (details instanceof JCDiagnostic) {
365 return details.getCode().equals("compiler.misc.full.inst.sig") ?
366 (MethodType)details.getArgs()[0] : null;
367 } else {
368 throw new AssertionError("Bad diagnostic arg: " + details);
369 }
370 }
371 }
373 /**
374 * Processor for verbose deferred inference notes generated by javac. The
375 * processor checks that the inferred signature for a given generic method
376 * call corresponds to the one (if any) declared in the @Candidate annotation.
377 */
378 class VerboseDeferredInferenceNoteProcessor extends DiagnosticProcessor {
380 public VerboseDeferredInferenceNoteProcessor() {
381 super(Kind.NOTE, "compiler.note.deferred.method.inst");
382 }
384 @Override
385 void process(Diagnostic<? extends JavaFileObject> diagnostic) {
386 Element methodSym = methodSym(diagnostic);
387 Candidate c = getCandidateAtPos(methodSym,
388 asJCDiagnostic(diagnostic).getLineNumber(),
389 asJCDiagnostic(diagnostic).getColumnNumber());
390 MethodType sig = sig(diagnostic);
391 if (c != null && sig != null) {
392 checkSig(c, methodSym, sig);
393 }
394 }
396 Element methodSym(Diagnostic<? extends JavaFileObject> diagnostic) {
397 return (Element)asJCDiagnostic(diagnostic).getArgs()[0];
398 }
400 MethodType sig(Diagnostic<? extends JavaFileObject> diagnostic) {
401 return (MethodType)asJCDiagnostic(diagnostic).getArgs()[1];
402 }
403 }
405 /**
406 * Processor for all error diagnostics; if the error key is not declared in
407 * the test file header, the processor reports an error.
408 */
409 class ErrorProcessor extends DiagnosticProcessor {
411 public ErrorProcessor() {
412 super(Diagnostic.Kind.ERROR);
413 }
415 @Override
416 void process(Diagnostic<? extends JavaFileObject> diagnostic) {
417 if (!declaredKeys.contains(diagnostic.getCode())) {
418 error("Unexpected compilation error key '" + diagnostic.getCode() + "'");
419 }
420 }
421 }
423 @SupportedAnnotationTypes({"Candidate","TraceResolve"})
424 class ResolveCandidateFinder extends JavacTestingAbstractProcessor {
426 @Override
427 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
428 if (roundEnv.processingOver())
429 return true;
431 TypeElement traceResolveAnno = elements.getTypeElement("TraceResolve");
432 TypeElement candidateAnno = elements.getTypeElement("Candidate");
434 if (!annotations.contains(traceResolveAnno)) {
435 error("no @TraceResolve annotation found in test class");
436 }
438 if (!annotations.contains(candidateAnno)) {
439 error("no @candidate annotation found in test class");
440 }
442 for (Element elem: roundEnv.getElementsAnnotatedWith(traceResolveAnno)) {
443 TraceResolve traceResolve = elem.getAnnotation(TraceResolve.class);
444 declaredKeys.addAll(Arrays.asList(traceResolve.keys()));
445 }
447 for (Element elem: roundEnv.getElementsAnnotatedWith(candidateAnno)) {
448 candidatesMap.put(new ElementKey(elem), elem.getAnnotation(Candidate.class));
449 }
450 return true;
451 }
452 }
454 class ElementKey {
456 String key;
457 Element elem;
459 public ElementKey(Element elem) {
460 this.elem = elem;
461 this.key = computeKey(elem);
462 }
464 @Override
465 public boolean equals(Object obj) {
466 if (obj instanceof ElementKey) {
467 ElementKey other = (ElementKey)obj;
468 return other.key.equals(key);
469 }
470 return false;
471 }
473 @Override
474 public int hashCode() {
475 return key.hashCode();
476 }
478 String computeKey(Element e) {
479 String simpleName = e.getSimpleName().toString();
480 String opName = predefTranslationMap.get(simpleName);
481 String name = opName != null ? opName : simpleName;
482 return name + e.asType();
483 }
485 @Override
486 public String toString() {
487 return "Key{"+key+"}";
488 }
489 }
491 class DiagnosticHandler implements DiagnosticListener<JavaFileObject> {
493 boolean shouldRecordDiags;
495 DiagnosticHandler(boolean shouldRecordDiags) {
496 this.shouldRecordDiags = shouldRecordDiags;
497 }
499 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
500 if (shouldRecordDiags)
501 diags.add(diagnostic);
502 }
504 }
505 }