ResolveHarness.java revision 1874:f559ef7568ce
1251607Sdim/*
2251607Sdim * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3251607Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4251607Sdim *
5251607Sdim * This code is free software; you can redistribute it and/or modify it
6251607Sdim * under the terms of the GNU General Public License version 2 only, as
7251607Sdim * published by the Free Software Foundation.
8251607Sdim *
9251607Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10251607Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11251607Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12251607Sdim * version 2 for more details (a copy is included in the LICENSE file that
13263509Sdim * accompanied this code).
14251607Sdim *
15251607Sdim * You should have received a copy of the GNU General Public License version
16251607Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17251607Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18251607Sdim *
19251607Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20251607Sdim * or visit www.oracle.com if you need additional information or have any
21251607Sdim * questions.
22251607Sdim */
23251607Sdim
24251607Sdim/*
25251607Sdim * @test
26251607Sdim * @bug 7098660
27251607Sdim * @summary Write better overload resolution/inference tests
28251607Sdim * @library /tools/javac/lib
29251607Sdim * @build JavacTestingAbstractProcessor ResolveHarness
30263509Sdim * @run main ResolveHarness
31251607Sdim */
32251607Sdim
33251607Sdimimport com.sun.source.util.JavacTask;
34251607Sdimimport com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper;
35263509Sdimimport com.sun.tools.javac.code.Flags;
36251607Sdimimport com.sun.tools.javac.code.Symbol;
37251607Sdimimport com.sun.tools.javac.code.Type.MethodType;
38263509Sdimimport com.sun.tools.javac.util.JCDiagnostic;
39263509Sdim
40263509Sdimimport java.io.File;
41251607Sdimimport java.util.Set;
42251607Sdimimport java.util.Arrays;
43251607Sdimimport java.util.ArrayList;
44251607Sdimimport java.util.Collections;
45import java.util.HashMap;
46import java.util.HashSet;
47import java.util.List;
48import java.util.Locale;
49import java.util.Map;
50
51import javax.annotation.processing.AbstractProcessor;
52import javax.annotation.processing.RoundEnvironment;
53import javax.annotation.processing.SupportedAnnotationTypes;
54import javax.lang.model.element.Element;
55import javax.lang.model.element.TypeElement;
56import javax.tools.Diagnostic;
57import javax.tools.Diagnostic.Kind;
58import javax.tools.DiagnosticListener;
59import javax.tools.JavaCompiler;
60import javax.tools.JavaFileObject;
61import javax.tools.StandardJavaFileManager;
62import javax.tools.ToolProvider;
63
64import static javax.tools.StandardLocation.*;
65
66public class ResolveHarness implements javax.tools.DiagnosticListener<JavaFileObject> {
67
68    static int nerrors = 0;
69
70    static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
71    static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
72
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    }
83
84
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<>();
92
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    }
125
126    protected void check() throws Exception {
127        String[] options = {
128            "-XDshouldStopPolicy=ATTR",
129            "-XDverboseResolution=success,failure,applicable,inapplicable,deferred-inference,predef"
130        };
131
132        AbstractProcessor[] processors = { new ResolveCandidateFinder(), null };
133
134        @SuppressWarnings("unchecked")
135        DiagnosticListener<? super JavaFileObject>[] diagListeners =
136                new DiagnosticListener[] { new DiagnosticHandler(false), new DiagnosticHandler(true) };
137
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        }
146
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    }
163
164    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
165        diags.add(diagnostic);
166    }
167
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    }
182
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    }
188
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    }
193
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 {
199
200        List<String> codes;
201        Diagnostic.Kind kind;
202
203        public DiagnosticProcessor(Kind kind, String... codes) {
204            this.codes = Arrays.asList(codes);
205            this.kind = kind;
206        }
207
208        abstract void process(Diagnostic<? extends JavaFileObject> diagnostic);
209
210        boolean matches(Diagnostic<? extends JavaFileObject> diagnostic) {
211            return (codes.isEmpty() || codes.contains(diagnostic.getCode())) &&
212                    diagnostic.getKind() == kind;
213        }
214
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        }
224
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    }
234
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 {
244
245        VerboseResolutionNoteProcessor() {
246            super(Kind.NOTE,
247                    "compiler.note.verbose.resolve.multi",
248                    "compiler.note.verbose.resolve.multi.1");
249        }
250
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        }
270
271        Element getSiteSym(Diagnostic<? extends JavaFileObject> diagnostic) {
272            return (Element)asJCDiagnostic(diagnostic).getArgs()[1];
273        }
274
275        int mostSpecific(Diagnostic<? extends JavaFileObject> diagnostic) {
276            return success(diagnostic) ?
277                    (Integer)asJCDiagnostic(diagnostic).getArgs()[2] : -1;
278        }
279
280        boolean success(Diagnostic<? extends JavaFileObject> diagnostic) {
281            return diagnostic.getCode().equals("compiler.note.verbose.resolve.multi");
282        }
283
284        Phase phase(Diagnostic<? extends JavaFileObject> diagnostic) {
285            return Phase.fromString(asJCDiagnostic(diagnostic).getArgs()[3].toString());
286        }
287    }
288
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 {
296
297        boolean mostSpecific;
298        Phase phase;
299        boolean success;
300
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        }
310
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            }
324
325            if (c.applicable().length == 0 && c.mostSpecific()) {
326                error("Inapplicable method cannot be most specific " + methodSym);
327            }
328
329            if (isApplicable(diagnostic) != Arrays.asList(c.applicable()).contains(phase)) {
330                error("Invalid candidate's applicability " + methodSym);
331            }
332
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            }
340
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        }
351
352        boolean isApplicable(Diagnostic<? extends JavaFileObject> diagnostic) {
353            return !diagnostic.getCode().equals("compiler.misc.not.applicable.method.found");
354        }
355
356        Element methodSym(Diagnostic<? extends JavaFileObject> diagnostic) {
357            return (Element)asJCDiagnostic(diagnostic).getArgs()[1];
358        }
359
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    }
372
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 {
379
380        public VerboseDeferredInferenceNoteProcessor() {
381            super(Kind.NOTE, "compiler.note.deferred.method.inst");
382        }
383
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        }
395
396        Element methodSym(Diagnostic<? extends JavaFileObject> diagnostic) {
397            return (Element)asJCDiagnostic(diagnostic).getArgs()[0];
398        }
399
400        MethodType sig(Diagnostic<? extends JavaFileObject> diagnostic) {
401            return (MethodType)asJCDiagnostic(diagnostic).getArgs()[1];
402        }
403    }
404
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 {
410
411        public ErrorProcessor() {
412            super(Diagnostic.Kind.ERROR);
413        }
414
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    }
422
423    @SupportedAnnotationTypes({"Candidate","TraceResolve"})
424    class ResolveCandidateFinder extends JavacTestingAbstractProcessor {
425
426        @Override
427        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
428            if (roundEnv.processingOver())
429                return true;
430
431            TypeElement traceResolveAnno = elements.getTypeElement("TraceResolve");
432            TypeElement candidateAnno = elements.getTypeElement("Candidate");
433
434            if (!annotations.contains(traceResolveAnno)) {
435                error("no @TraceResolve annotation found in test class");
436            }
437
438            if (!annotations.contains(candidateAnno)) {
439                error("no @candidate annotation found in test class");
440            }
441
442            for (Element elem: roundEnv.getElementsAnnotatedWith(traceResolveAnno)) {
443                TraceResolve traceResolve = elem.getAnnotation(TraceResolve.class);
444                declaredKeys.addAll(Arrays.asList(traceResolve.keys()));
445            }
446
447            for (Element elem: roundEnv.getElementsAnnotatedWith(candidateAnno)) {
448                candidatesMap.put(new ElementKey(elem), elem.getAnnotation(Candidate.class));
449            }
450            return true;
451        }
452    }
453
454    class ElementKey {
455
456        String key;
457        Element elem;
458
459        public ElementKey(Element elem) {
460            this.elem = elem;
461            this.key = computeKey(elem);
462        }
463
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        }
472
473        @Override
474        public int hashCode() {
475            return key.hashCode();
476        }
477
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        }
484
485        @Override
486        public String toString() {
487            return "Key{"+key+"}";
488        }
489    }
490
491    class DiagnosticHandler implements DiagnosticListener<JavaFileObject> {
492
493        boolean shouldRecordDiags;
494
495        DiagnosticHandler(boolean shouldRecordDiags) {
496            this.shouldRecordDiags = shouldRecordDiags;
497        }
498
499        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
500            if (shouldRecordDiags)
501                diags.add(diagnostic);
502        }
503
504    }
505}
506