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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 776
3c32c90031fd
child 1348
573ceb23beeb
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

mcimadamore@83 1 /*
ohair@798 2 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
mcimadamore@83 24 */
mcimadamore@83 25 package com.sun.tools.javac.util;
mcimadamore@83 26
mcimadamore@221 27 import java.util.Collection;
mcimadamore@221 28 import java.util.EnumSet;
mcimadamore@83 29 import java.util.Locale;
jjg@776 30 import javax.tools.JavaFileObject;
mcimadamore@83 31
mcimadamore@221 32 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
mcimadamore@83 33 import com.sun.tools.javac.api.Formattable;
jjg@415 34 import com.sun.tools.javac.file.BaseFileObject;
mcimadamore@221 35 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
mcimadamore@221 36
mcimadamore@83 37 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
mcimadamore@83 38
mcimadamore@83 39 /**
mcimadamore@83 40 * A raw formatter for diagnostic messages.
mcimadamore@83 41 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
mcimadamore@83 42 * or not the source name and position are set. This formatter provides a standardized, localize-independent
mcimadamore@83 43 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
jjg@333 44 *
jjg@581 45 * <p><b>This is NOT part of any supported API.
jjg@333 46 * If you write code that depends on this, you do so at your own risk.
jjg@333 47 * This code and its internal interfaces are subject to change or
jjg@333 48 * deletion without notice.</b>
mcimadamore@83 49 */
mcimadamore@221 50 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
mcimadamore@83 51
mcimadamore@83 52 /**
mcimadamore@83 53 * Create a formatter based on the supplied options.
mcimadamore@83 54 * @param msgs
mcimadamore@83 55 */
mcimadamore@221 56 public RawDiagnosticFormatter(Options options) {
mcimadamore@221 57 super(null, new SimpleConfiguration(options,
mcimadamore@221 58 EnumSet.of(DiagnosticPart.SUMMARY,
mcimadamore@221 59 DiagnosticPart.DETAILS,
mcimadamore@221 60 DiagnosticPart.SUBDIAGNOSTICS)));
mcimadamore@83 61 }
mcimadamore@83 62
mcimadamore@83 63 //provide common default formats
mcimadamore@238 64 public String formatDiagnostic(JCDiagnostic d, Locale l) {
mcimadamore@83 65 try {
jjg@776 66 StringBuilder buf = new StringBuilder();
mcimadamore@83 67 if (d.getPosition() != Position.NOPOS) {
mcimadamore@100 68 buf.append(formatSource(d, false, null));
mcimadamore@83 69 buf.append(':');
mcimadamore@83 70 buf.append(formatPosition(d, LINE, null));
mcimadamore@83 71 buf.append(':');
mcimadamore@83 72 buf.append(formatPosition(d, COLUMN, null));
mcimadamore@83 73 buf.append(':');
mcimadamore@83 74 }
jjg@776 75 else if (d.getSource() != null && d.getSource().getKind() == JavaFileObject.Kind.CLASS) {
jjg@776 76 buf.append(formatSource(d, false, null));
jjg@776 77 buf.append(":-:-:");
jjg@776 78 }
mcimadamore@83 79 else
mcimadamore@83 80 buf.append('-');
mcimadamore@83 81 buf.append(' ');
mcimadamore@83 82 buf.append(formatMessage(d, null));
jjg@776 83 if (displaySource(d)) {
jjg@776 84 buf.append("\n");
jjg@776 85 buf.append(formatSourceLine(d, 0));
jjg@776 86 }
mcimadamore@83 87 return buf.toString();
mcimadamore@83 88 }
mcimadamore@83 89 catch (Exception e) {
jjg@776 90 //e.printStackTrace();
mcimadamore@83 91 return null;
mcimadamore@83 92 }
mcimadamore@83 93 }
mcimadamore@83 94
mcimadamore@221 95 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@221 96 StringBuilder buf = new StringBuilder();
mcimadamore@221 97 Collection<String> args = formatArguments(d, l);
mcimadamore@238 98 buf.append(localize(null, d.getCode(), args.toArray()));
mcimadamore@221 99 if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
mcimadamore@221 100 List<String> subDiags = formatSubdiagnostics(d, null);
mcimadamore@221 101 if (subDiags.nonEmpty()) {
mcimadamore@238 102 String sep = "";
mcimadamore@221 103 buf.append(",{");
mcimadamore@221 104 for (String sub : formatSubdiagnostics(d, null)) {
mcimadamore@221 105 buf.append(sep);
jjg@776 106 buf.append("(");
jjg@776 107 buf.append(sub);
jjg@776 108 buf.append(")");
mcimadamore@221 109 sep = ",";
mcimadamore@221 110 }
mcimadamore@221 111 buf.append('}');
mcimadamore@221 112 }
mcimadamore@221 113 }
mcimadamore@221 114 return buf.toString();
mcimadamore@221 115 }
mcimadamore@221 116
mcimadamore@83 117 @Override
mcimadamore@83 118 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
mcimadamore@83 119 String s;
mcimadamore@83 120 if (arg instanceof Formattable)
mcimadamore@83 121 s = arg.toString();
jjg@415 122 else if (arg instanceof BaseFileObject)
jjg@415 123 s = ((BaseFileObject) arg).getShortName();
mcimadamore@83 124 else
mcimadamore@83 125 s = super.formatArgument(diag, arg, null);
mcimadamore@83 126 if (arg instanceof JCDiagnostic)
mcimadamore@83 127 return "(" + s + ")";
mcimadamore@83 128 else
mcimadamore@83 129 return s;
mcimadamore@83 130 }
mcimadamore@238 131
mcimadamore@238 132 @Override
mcimadamore@238 133 protected String localize(Locale l, String key, Object... args) {
mcimadamore@238 134 StringBuilder buf = new StringBuilder();
mcimadamore@238 135 buf.append(key);
mcimadamore@238 136 String sep = ": ";
mcimadamore@238 137 for (Object o : args) {
mcimadamore@238 138 buf.append(sep);
mcimadamore@238 139 buf.append(o);
mcimadamore@238 140 sep = ", ";
mcimadamore@238 141 }
mcimadamore@238 142 return buf.toString();
mcimadamore@238 143 }
mcimadamore@288 144
mcimadamore@288 145 @Override
mcimadamore@288 146 public boolean isRaw() {
mcimadamore@288 147 return true;
mcimadamore@288 148 }
mcimadamore@83 149 }

mercurial