src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2008, 2012, 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 package com.sun.tools.javac.util;
26
27 import java.util.Collection;
28 import java.util.EnumSet;
29 import java.util.Locale;
30 import javax.tools.JavaFileObject;
31
32 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
33 import com.sun.tools.javac.api.Formattable;
34 import com.sun.tools.javac.file.BaseFileObject;
35 import com.sun.tools.javac.tree.JCTree.*;
36 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
37
38 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
39
40 /**
41 * A raw formatter for diagnostic messages.
42 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
43 * or not the source name and position are set. This formatter provides a standardized, localize-independent
44 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
45 *
46 * <p><b>This is NOT part of any supported API.
47 * If you write code that depends on this, you do so at your own risk.
48 * This code and its internal interfaces are subject to change or
49 * deletion without notice.</b>
50 */
51 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
52
53 /**
54 * Create a formatter based on the supplied options.
55 * @param options
56 */
57 public RawDiagnosticFormatter(Options options) {
58 super(null, new SimpleConfiguration(options,
59 EnumSet.of(DiagnosticPart.SUMMARY,
60 DiagnosticPart.DETAILS,
61 DiagnosticPart.SUBDIAGNOSTICS)));
62 }
63
64 //provide common default formats
65 public String formatDiagnostic(JCDiagnostic d, Locale l) {
66 try {
67 StringBuilder buf = new StringBuilder();
68 if (d.getPosition() != Position.NOPOS) {
69 buf.append(formatSource(d, false, null));
70 buf.append(':');
71 buf.append(formatPosition(d, LINE, null));
72 buf.append(':');
73 buf.append(formatPosition(d, COLUMN, null));
74 buf.append(':');
75 }
76 else if (d.getSource() != null && d.getSource().getKind() == JavaFileObject.Kind.CLASS) {
77 buf.append(formatSource(d, false, null));
78 buf.append(":-:-:");
79 }
80 else
81 buf.append('-');
82 buf.append(' ');
83 buf.append(formatMessage(d, null));
84 if (displaySource(d)) {
85 buf.append("\n");
86 buf.append(formatSourceLine(d, 0));
87 }
88 return buf.toString();
89 }
90 catch (Exception e) {
91 //e.printStackTrace();
92 return null;
93 }
94 }
95
96 public String formatMessage(JCDiagnostic d, Locale l) {
97 StringBuilder buf = new StringBuilder();
98 Collection<String> args = formatArguments(d, l);
99 buf.append(localize(null, d.getCode(), args.toArray()));
100 if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
101 List<String> subDiags = formatSubdiagnostics(d, null);
102 if (subDiags.nonEmpty()) {
103 String sep = "";
104 buf.append(",{");
105 for (String sub : formatSubdiagnostics(d, null)) {
106 buf.append(sep);
107 buf.append("(");
108 buf.append(sub);
109 buf.append(")");
110 sep = ",";
111 }
112 buf.append('}');
113 }
114 }
115 return buf.toString();
116 }
117
118 @Override
119 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
120 String s;
121 if (arg instanceof Formattable) {
122 s = arg.toString();
123 } else if (arg instanceof JCExpression) {
124 JCExpression tree = (JCExpression)arg;
125 s = "@" + tree.getStartPosition();
126 } else if (arg instanceof BaseFileObject) {
127 s = ((BaseFileObject) arg).getShortName();
128 } else {
129 s = super.formatArgument(diag, arg, null);
130 }
131 return (arg instanceof JCDiagnostic) ? "(" + s + ")" : s;
132 }
133
134 @Override
135 protected String localize(Locale l, String key, Object... args) {
136 StringBuilder buf = new StringBuilder();
137 buf.append(key);
138 String sep = ": ";
139 for (Object o : args) {
140 buf.append(sep);
141 buf.append(o);
142 sep = ", ";
143 }
144 return buf.toString();
145 }
146
147 @Override
148 public boolean isRaw() {
149 return true;
150 }
151 }

mercurial