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