AbstractDiagnosticFormatter.java revision 3170:dc017a37aac5
1/*
2 * Copyright (c) 2008, 2015, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.util;
27
28import java.nio.file.Path;
29import java.util.Arrays;
30import java.util.Collection;
31import java.util.EnumSet;
32import java.util.HashMap;
33import java.util.Locale;
34import java.util.Map;
35import java.util.Set;
36
37import javax.tools.JavaFileObject;
38
39import com.sun.tools.javac.api.DiagnosticFormatter;
40import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
41import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.MultilineLimit;
42import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
43import com.sun.tools.javac.api.Formattable;
44import com.sun.tools.javac.code.Lint.LintCategory;
45import com.sun.tools.javac.code.Printer;
46import com.sun.tools.javac.code.Symbol;
47import com.sun.tools.javac.code.Type;
48import com.sun.tools.javac.code.Type.CapturedType;
49import com.sun.tools.javac.file.PathFileObject;
50import com.sun.tools.javac.jvm.Profile;
51import com.sun.tools.javac.tree.JCTree.*;
52import com.sun.tools.javac.tree.Pretty;
53
54import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
55
56/**
57 * This abstract class provides a basic implementation of the functionalities that should be provided
58 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
59 *
60 * <ul>
61 *  <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
62 *  Those implementations are specifically targeting JCDiagnostic objects.
63 *  <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
64 *  <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
65 * </ul>
66 *
67 * <p><b>This is NOT part of any supported API.
68 * If you write code that depends on this, you do so at your own risk.
69 * This code and its internal interfaces are subject to change or
70 * deletion without notice.</b>
71 */
72public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
73
74    /**
75     * JavacMessages object used by this formatter for i18n.
76     */
77    protected JavacMessages messages;
78
79    /**
80     * Configuration object used by this formatter
81     */
82    private SimpleConfiguration config;
83
84    /**
85     * Current depth level of the disgnostic being formatted
86     * (!= 0 for subdiagnostics)
87     */
88    protected int depth = 0;
89
90    /**
91     * All captured types that have been encountered during diagnostic formatting.
92     * This info is used by the FormatterPrinter in order to print friendly unique
93     * ids for captured types
94     */
95    private List<Type> allCaptured = List.nil();
96
97    /**
98     * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object.
99     * @param messages
100     */
101    protected AbstractDiagnosticFormatter(JavacMessages messages, SimpleConfiguration config) {
102        this.messages = messages;
103        this.config = config;
104    }
105
106    public String formatKind(JCDiagnostic d, Locale l) {
107        switch (d.getType()) {
108            case FRAGMENT: return "";
109            case NOTE:     return localize(l, "compiler.note.note");
110            case WARNING:  return localize(l, "compiler.warn.warning");
111            case ERROR:    return localize(l, "compiler.err.error");
112            default:
113                throw new AssertionError("Unknown diagnostic type: " + d.getType());
114        }
115    }
116
117    @Override
118    public String format(JCDiagnostic d, Locale locale) {
119        allCaptured = List.nil();
120        return formatDiagnostic(d, locale);
121    }
122
123    protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
124
125    public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
126        Assert.check(d.getPosition() != Position.NOPOS);
127        return String.valueOf(getPosition(d, pk));
128    }
129    //where
130    private long getPosition(JCDiagnostic d, PositionKind pk) {
131        switch (pk) {
132            case START: return d.getIntStartPosition();
133            case END: return d.getIntEndPosition();
134            case LINE: return d.getLineNumber();
135            case COLUMN: return d.getColumnNumber();
136            case OFFSET: return d.getIntPosition();
137            default:
138                throw new AssertionError("Unknown diagnostic position: " + pk);
139        }
140    }
141
142    public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
143        JavaFileObject fo = d.getSource();
144        if (fo == null)
145            throw new IllegalArgumentException(); // d should have source set
146        if (fullname)
147            return fo.getName();
148        else if (fo instanceof PathFileObject)
149            return ((PathFileObject) fo).getShortName();
150        else
151            return PathFileObject.getSimpleName(fo);
152    }
153
154    /**
155     * Format the arguments of a given diagnostic.
156     *
157     * @param d diagnostic whose arguments are to be formatted
158     * @param l locale object to be used for i18n
159     * @return a Collection whose elements are the formatted arguments of the diagnostic
160     */
161    protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
162        ListBuffer<String> buf = new ListBuffer<>();
163        for (Object o : d.getArgs()) {
164           buf.append(formatArgument(d, o, l));
165        }
166        return buf.toList();
167    }
168
169    /**
170     * Format a single argument of a given diagnostic.
171     *
172     * @param d diagnostic whose argument is to be formatted
173     * @param arg argument to be formatted
174     * @param l locale object to be used for i18n
175     * @return string representation of the diagnostic argument
176     */
177    protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
178        if (arg instanceof JCDiagnostic) {
179            String s = null;
180            depth++;
181            try {
182                s = formatMessage((JCDiagnostic)arg, l);
183            }
184            finally {
185                depth--;
186            }
187            return s;
188        }
189        else if (arg instanceof JCExpression) {
190            return expr2String((JCExpression)arg);
191        }
192        else if (arg instanceof Iterable<?> && !(arg instanceof Path)) {
193            return formatIterable(d, (Iterable<?>)arg, l);
194        }
195        else if (arg instanceof Type) {
196            return printer.visit((Type)arg, l);
197        }
198        else if (arg instanceof Symbol) {
199            return printer.visit((Symbol)arg, l);
200        }
201        else if (arg instanceof JavaFileObject) {
202            return ((JavaFileObject)arg).getName();
203        }
204        else if (arg instanceof Profile) {
205            return ((Profile)arg).name;
206        }
207        else if (arg instanceof Formattable) {
208            return ((Formattable)arg).toString(l, messages);
209        }
210        else {
211            return String.valueOf(arg);
212        }
213    }
214    //where
215            private String expr2String(JCExpression tree) {
216                switch(tree.getTag()) {
217                    case PARENS:
218                        return expr2String(((JCParens)tree).expr);
219                    case LAMBDA:
220                    case REFERENCE:
221                    case CONDEXPR:
222                        return Pretty.toSimpleString(tree);
223                    default:
224                        Assert.error("unexpected tree kind " + tree.getKind());
225                        return null;
226                }
227            }
228
229    /**
230     * Format an iterable argument of a given diagnostic.
231     *
232     * @param d diagnostic whose argument is to be formatted
233     * @param it iterable argument to be formatted
234     * @param l locale object to be used for i18n
235     * @return string representation of the diagnostic iterable argument
236     */
237    protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
238        StringBuilder sbuf = new StringBuilder();
239        String sep = "";
240        for (Object o : it) {
241            sbuf.append(sep);
242            sbuf.append(formatArgument(d, o, l));
243            sep = ",";
244        }
245        return sbuf.toString();
246    }
247
248    /**
249     * Format all the subdiagnostics attached to a given diagnostic.
250     *
251     * @param d diagnostic whose subdiagnostics are to be formatted
252     * @param l locale object to be used for i18n
253     * @return list of all string representations of the subdiagnostics
254     */
255    protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) {
256        List<String> subdiagnostics = List.nil();
257        int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
258        if (maxDepth == -1 || depth < maxDepth) {
259            depth++;
260            try {
261                int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
262                int count = 0;
263                for (JCDiagnostic d2 : d.getSubdiagnostics()) {
264                    if (maxCount == -1 || count < maxCount) {
265                        subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
266                        count++;
267                    }
268                    else
269                        break;
270                }
271            }
272            finally {
273                depth--;
274            }
275        }
276        return subdiagnostics;
277    }
278
279    /**
280     * Format a subdiagnostics attached to a given diagnostic.
281     *
282     * @param parent multiline diagnostic whose subdiagnostics is to be formatted
283     * @param sub subdiagnostic to be formatted
284     * @param l locale object to be used for i18n
285     * @return string representation of the subdiagnostics
286     */
287    protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) {
288        return formatMessage(sub, l);
289    }
290
291    /** Format the faulty source code line and point to the error.
292     *  @param d The diagnostic for which the error line should be printed
293     */
294    protected String formatSourceLine(JCDiagnostic d, int nSpaces) {
295        StringBuilder buf = new StringBuilder();
296        DiagnosticSource source = d.getDiagnosticSource();
297        int pos = d.getIntPosition();
298        if (d.getIntPosition() == Position.NOPOS)
299            throw new AssertionError();
300        String line = (source == null ? null : source.getLine(pos));
301        if (line == null)
302            return "";
303        buf.append(indent(line, nSpaces));
304        int col = source.getColumnNumber(pos, false);
305        if (config.isCaretEnabled()) {
306            buf.append("\n");
307            for (int i = 0; i < col - 1; i++)  {
308                buf.append((line.charAt(i) == '\t') ? "\t" : " ");
309            }
310            buf.append(indent("^", nSpaces));
311        }
312        return buf.toString();
313    }
314
315    protected String formatLintCategory(JCDiagnostic d, Locale l) {
316        LintCategory lc = d.getLintCategory();
317        if (lc == null)
318            return "";
319        return localize(l, "compiler.warn.lintOption", lc.option);
320    }
321
322    /**
323     * Converts a String into a locale-dependent representation accordingly to a given locale.
324     *
325     * @param l locale object to be used for i18n
326     * @param key locale-independent key used for looking up in a resource file
327     * @param args localization arguments
328     * @return a locale-dependent string
329     */
330    protected String localize(Locale l, String key, Object... args) {
331        return messages.getLocalizedString(l, key, args);
332    }
333
334    public boolean displaySource(JCDiagnostic d) {
335        return config.getVisible().contains(DiagnosticPart.SOURCE) &&
336                d.getType() != FRAGMENT &&
337                d.getIntPosition() != Position.NOPOS;
338    }
339
340    public boolean isRaw() {
341        return false;
342    }
343
344    /**
345     * Creates a string with a given amount of empty spaces. Useful for
346     * indenting the text of a diagnostic message.
347     *
348     * @param nSpaces the amount of spaces to be added to the result string
349     * @return the indentation string
350     */
351    protected String indentString(int nSpaces) {
352        String spaces = "                        ";
353        if (nSpaces <= spaces.length())
354            return spaces.substring(0, nSpaces);
355        else {
356            StringBuilder buf = new StringBuilder();
357            for (int i = 0 ; i < nSpaces ; i++)
358                buf.append(" ");
359            return buf.toString();
360        }
361    }
362
363    /**
364     * Indent a string by prepending a given amount of empty spaces to each line
365     * of the string.
366     *
367     * @param s the string to be indented
368     * @param nSpaces the amount of spaces that should be prepended to each line
369     * of the string
370     * @return an indented string
371     */
372    protected String indent(String s, int nSpaces) {
373        String indent = indentString(nSpaces);
374        StringBuilder buf = new StringBuilder();
375        String nl = "";
376        for (String line : s.split("\n")) {
377            buf.append(nl);
378            buf.append(indent + line);
379            nl = "\n";
380        }
381        return buf.toString();
382    }
383
384    public SimpleConfiguration getConfiguration() {
385        return config;
386    }
387
388    static public class SimpleConfiguration implements Configuration {
389
390        protected Map<MultilineLimit, Integer> multilineLimits;
391        protected EnumSet<DiagnosticPart> visibleParts;
392        protected boolean caretEnabled;
393
394        public SimpleConfiguration(Set<DiagnosticPart> parts) {
395            multilineLimits = new HashMap<>();
396            setVisible(parts);
397            setMultilineLimit(MultilineLimit.DEPTH, -1);
398            setMultilineLimit(MultilineLimit.LENGTH, -1);
399            setCaretEnabled(true);
400        }
401
402        @SuppressWarnings("fallthrough")
403        public SimpleConfiguration(Options options, Set<DiagnosticPart> parts) {
404            this(parts);
405            String showSource = null;
406            if ((showSource = options.get("showSource")) != null) {
407                if (showSource.equals("true"))
408                    setVisiblePart(DiagnosticPart.SOURCE, true);
409                else if (showSource.equals("false"))
410                    setVisiblePart(DiagnosticPart.SOURCE, false);
411            }
412            String diagOpts = options.get("diags");
413            if (diagOpts != null) {//override -XDshowSource
414                Collection<String> args = Arrays.asList(diagOpts.split(","));
415                if (args.contains("short")) {
416                    setVisiblePart(DiagnosticPart.DETAILS, false);
417                    setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
418                }
419                if (args.contains("source"))
420                    setVisiblePart(DiagnosticPart.SOURCE, true);
421                if (args.contains("-source"))
422                    setVisiblePart(DiagnosticPart.SOURCE, false);
423            }
424            String multiPolicy = null;
425            if ((multiPolicy = options.get("multilinePolicy")) != null) {
426                if (multiPolicy.equals("disabled"))
427                    setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
428                else if (multiPolicy.startsWith("limit:")) {
429                    String limitString = multiPolicy.substring("limit:".length());
430                    String[] limits = limitString.split(":");
431                    try {
432                        switch (limits.length) {
433                            case 2: {
434                                if (!limits[1].equals("*"))
435                                    setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1]));
436                            }
437                            case 1: {
438                                if (!limits[0].equals("*"))
439                                    setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0]));
440                            }
441                        }
442                    }
443                    catch(NumberFormatException ex) {
444                        setMultilineLimit(MultilineLimit.DEPTH, -1);
445                        setMultilineLimit(MultilineLimit.LENGTH, -1);
446                    }
447                }
448            }
449            String showCaret = null;
450            if (((showCaret = options.get("showCaret")) != null) &&
451                showCaret.equals("false"))
452                    setCaretEnabled(false);
453            else
454                setCaretEnabled(true);
455        }
456
457        public int getMultilineLimit(MultilineLimit limit) {
458            return multilineLimits.get(limit);
459        }
460
461        public EnumSet<DiagnosticPart> getVisible() {
462            return EnumSet.copyOf(visibleParts);
463        }
464
465        public void setMultilineLimit(MultilineLimit limit, int value) {
466            multilineLimits.put(limit, value < -1 ? -1 : value);
467        }
468
469
470        public void setVisible(Set<DiagnosticPart> diagParts) {
471            visibleParts = EnumSet.copyOf(diagParts);
472        }
473
474        public void setVisiblePart(DiagnosticPart diagParts, boolean enabled) {
475            if (enabled)
476                visibleParts.add(diagParts);
477            else
478                visibleParts.remove(diagParts);
479        }
480
481        /**
482         * Shows a '^' sign under the source line displayed by the formatter
483         * (if applicable).
484         *
485         * @param caretEnabled if true enables caret
486         */
487        public void setCaretEnabled(boolean caretEnabled) {
488            this.caretEnabled = caretEnabled;
489        }
490
491        /**
492         * Tells whether the caret display is active or not.
493         *
494         * @return true if the caret is enabled
495         */
496        public boolean isCaretEnabled() {
497            return caretEnabled;
498        }
499    }
500
501    public Printer getPrinter() {
502        return printer;
503    }
504
505    public void setPrinter(Printer printer) {
506        this.printer = printer;
507    }
508
509    /**
510     * An enhanced printer for formatting types/symbols used by
511     * AbstractDiagnosticFormatter. Provides alternate numbering of captured
512     * types (they are numbered starting from 1 on each new diagnostic, instead
513     * of relying on the underlying hashcode() method which generates unstable
514     * output). Also detects cycles in wildcard messages (e.g. if the wildcard
515     * type referred by a given captured type C contains C itself) which might
516     * lead to infinite loops.
517     */
518    protected Printer printer = new Printer() {
519
520        @Override
521        protected String localize(Locale locale, String key, Object... args) {
522            return AbstractDiagnosticFormatter.this.localize(locale, key, args);
523        }
524        @Override
525        protected String capturedVarId(CapturedType t, Locale locale) {
526            return "" + (allCaptured.indexOf(t) + 1);
527        }
528        @Override
529        public String visitCapturedType(CapturedType t, Locale locale) {
530            if (!allCaptured.contains(t)) {
531                allCaptured = allCaptured.append(t);
532            }
533            return super.visitCapturedType(t, locale);
534        }
535    };
536}
537