src/share/classes/com/sun/tools/apt/util/Bark.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 96
938a80a47670
child 136
8eafba4f61be
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 2004-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.apt.util;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.Context;
duke@1 29 import com.sun.tools.javac.util.JCDiagnostic;
duke@1 30 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
duke@1 31 import com.sun.tools.javac.util.Log;
duke@1 32 import com.sun.tools.javac.util.Messages;
duke@1 33 import com.sun.tools.javac.util.Position;
duke@1 34
duke@1 35 /** A subtype of Log for use in APT.
duke@1 36 *
duke@1 37 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 38 * you write code that depends on this, you do so at your own risk.
duke@1 39 * This code and its internal interfaces are subject to change or
duke@1 40 * deletion without notice.</b>
duke@1 41 */
duke@1 42 public class Bark extends Log {
duke@1 43 /** The context key for the bark. */
duke@1 44 protected static final Context.Key<Bark> barkKey =
duke@1 45 new Context.Key<Bark>();
duke@1 46
duke@1 47 /**
duke@1 48 * Preregisters factories to create and use a Bark object for use as
duke@1 49 * both a Log and a Bark.
duke@1 50 */
duke@1 51 public static void preRegister(final Context context) {
duke@1 52 context.put(barkKey, new Context.Factory<Bark>() {
duke@1 53 public Bark make() {
duke@1 54 return new Bark(context);
duke@1 55 }
duke@1 56 });
duke@1 57 context.put(Log.logKey, new Context.Factory<Log>() {
duke@1 58 public Log make() {
duke@1 59 return Bark.instance(context);
duke@1 60 }
duke@1 61 });
duke@1 62 }
duke@1 63
duke@1 64 /** Get the Bark instance for this context. */
duke@1 65 public static Bark instance(Context context) {
duke@1 66 Bark instance = context.get(barkKey);
duke@1 67 if (instance == null)
duke@1 68 instance = new Bark(context);
duke@1 69 return instance;
duke@1 70 }
duke@1 71
duke@1 72 /** Specifies whether or not to ignore any diagnostics that are reported.
duke@1 73 */
duke@1 74 private boolean ignoreDiagnostics;
duke@1 75
duke@1 76 /**
duke@1 77 * Factory for APT-specific diagnostics.
duke@1 78 */
duke@1 79 private JCDiagnostic.Factory aptDiags;
duke@1 80
duke@1 81
duke@1 82 /**
duke@1 83 * Creates a Bark.
duke@1 84 */
duke@1 85 protected Bark(Context context) {
duke@1 86 super(context); // will register this object in context with Log.logKey
duke@1 87 context.put(barkKey, this);
duke@1 88
duke@1 89 // register additional resource bundle for APT messages.
mcimadamore@96 90 Messages aptMessages = Messages.instance(context);
duke@1 91 aptMessages.add("com.sun.tools.apt.resources.apt");
duke@1 92 aptDiags = new JCDiagnostic.Factory(aptMessages, "apt");
duke@1 93
duke@1 94 multipleErrors = true;
duke@1 95 }
duke@1 96
duke@1 97 /**
duke@1 98 * Sets a flag indicating whether or not to ignore all diagnostics.
duke@1 99 * When ignored, they are not reported to the output writers, not are they
duke@1 100 * counted in the various counters.
duke@1 101 * @param b If true, subsequent diagnostics will be ignored.
duke@1 102 * @return the previous state of the flag
duke@1 103 */
duke@1 104 public boolean setDiagnosticsIgnored(boolean b) {
duke@1 105 boolean prev = ignoreDiagnostics;
duke@1 106 ignoreDiagnostics = b;
duke@1 107 return prev;
duke@1 108 }
duke@1 109
duke@1 110 /**
duke@1 111 * Report a diagnostic if they are not currently being ignored.
duke@1 112 */
duke@1 113 @Override
duke@1 114 public void report(JCDiagnostic diagnostic) {
duke@1 115 if (ignoreDiagnostics)
duke@1 116 return;
duke@1 117
duke@1 118 super.report(diagnostic);
duke@1 119 }
duke@1 120
duke@1 121 /** Report an error.
duke@1 122 * @param key The key for the localized error message.
duke@1 123 * @param args Fields of the error message.
duke@1 124 */
duke@1 125 public void aptError(String key, Object... args) {
duke@1 126 aptError(Position.NOPOS, key, args);
duke@1 127 }
duke@1 128
duke@1 129 /** Report an error, unless another error was already reported at same
duke@1 130 * source position.
duke@1 131 * @param pos The source position at which to report the error.
duke@1 132 * @param key The key for the localized error message.
duke@1 133 * @param args Fields of the error message.
duke@1 134 */
duke@1 135 public void aptError(int pos, String key, Object ... args) {
duke@1 136 report(aptDiags.error(source, new SimpleDiagnosticPosition(pos), key, args));
duke@1 137 }
duke@1 138
duke@1 139 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 140 * maximum number of warnings has been reached.
duke@1 141 * @param key The key for the localized warning message.
duke@1 142 * @param args Fields of the warning message.
duke@1 143 */
duke@1 144 public void aptWarning(String key, Object... args) {
duke@1 145 aptWarning(Position.NOPOS, key, args);
duke@1 146 }
duke@1 147
duke@1 148 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 149 * maximum number of warnings has been reached.
duke@1 150 * @param pos The source position at which to report the warning.
duke@1 151 * @param key The key for the localized warning message.
duke@1 152 * @param args Fields of the warning message.
duke@1 153 */
duke@1 154 public void aptWarning(int pos, String key, Object ... args) {
duke@1 155 report(aptDiags.warning(source, new SimpleDiagnosticPosition(pos), key, args));
duke@1 156 }
duke@1 157
duke@1 158 /** Report a note, unless suppressed by the -nowarn option.
duke@1 159 * @param key The key for the localized note message.
duke@1 160 * @param args Fields of the note message.
duke@1 161 */
duke@1 162 public void aptNote(String key, Object... args) {
duke@1 163 aptNote(Position.NOPOS, key, args);
duke@1 164 }
duke@1 165
duke@1 166 /** Report a note, unless suppressed by the -nowarn option.
duke@1 167 * @param pos The source position at which to report the note.
duke@1 168 * @param key The key for the localized note message.
duke@1 169 * @param args Fields of the note message.
duke@1 170 */
duke@1 171 public void aptNote(int pos, String key, Object ... args) {
duke@1 172 report(aptDiags.note(source, new SimpleDiagnosticPosition(pos), key, args));
duke@1 173 }
duke@1 174 }

mercurial