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

     1 /*
     2  * Copyright 2004-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.apt.util;
    28 import com.sun.tools.javac.util.Context;
    29 import com.sun.tools.javac.util.JCDiagnostic;
    30 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
    31 import com.sun.tools.javac.util.Log;
    32 import com.sun.tools.javac.util.Messages;
    33 import com.sun.tools.javac.util.Position;
    35 /** A subtype of Log for use in APT.
    36  *
    37  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    38  *  you write code that depends on this, you do so at your own risk.
    39  *  This code and its internal interfaces are subject to change or
    40  *  deletion without notice.</b>
    41  */
    42 public class Bark extends Log {
    43     /** The context key for the bark. */
    44     protected static final Context.Key<Bark> barkKey =
    45         new Context.Key<Bark>();
    47     /**
    48      * Preregisters factories to create and use a Bark object for use as
    49      * both a Log and a Bark.
    50      */
    51     public static void preRegister(final Context context) {
    52         context.put(barkKey, new Context.Factory<Bark>() {
    53             public Bark make() {
    54                 return new Bark(context);
    55             }
    56         });
    57         context.put(Log.logKey, new Context.Factory<Log>() {
    58             public Log make() {
    59                 return Bark.instance(context);
    60             }
    61         });
    62     }
    64     /** Get the Bark instance for this context. */
    65     public static Bark instance(Context context) {
    66         Bark instance = context.get(barkKey);
    67         if (instance == null)
    68             instance = new Bark(context);
    69         return instance;
    70     }
    72     /** Specifies whether or not to ignore any diagnostics that are reported.
    73      */
    74     private boolean ignoreDiagnostics;
    76     /**
    77      * Factory for APT-specific diagnostics.
    78      */
    79     private JCDiagnostic.Factory aptDiags;
    82     /**
    83      * Creates a Bark.
    84      */
    85     protected Bark(Context context) {
    86         super(context); // will register this object in context with Log.logKey
    87         context.put(barkKey, this);
    89         // register additional resource bundle for APT messages.
    90         Messages aptMessages = Messages.instance(context);
    91         aptMessages.add("com.sun.tools.apt.resources.apt");
    92         aptDiags = new JCDiagnostic.Factory(aptMessages, "apt");
    94         multipleErrors = true;
    95     }
    97     /**
    98      * Sets a flag indicating whether or not to ignore all diagnostics.
    99      * When ignored, they are not reported to the output writers, not are they
   100      * counted in the various counters.
   101      * @param b If true, subsequent diagnostics will be ignored.
   102      * @return the previous state of the flag
   103      */
   104     public boolean setDiagnosticsIgnored(boolean b) {
   105         boolean prev = ignoreDiagnostics;
   106         ignoreDiagnostics = b;
   107         return prev;
   108     }
   110     /**
   111      * Report a diagnostic if they are not currently being ignored.
   112      */
   113     @Override
   114     public void report(JCDiagnostic diagnostic) {
   115         if (ignoreDiagnostics)
   116             return;
   118         super.report(diagnostic);
   119     }
   121     /** Report an error.
   122      *  @param key    The key for the localized error message.
   123      *  @param args   Fields of the error message.
   124      */
   125     public void aptError(String key, Object... args) {
   126         aptError(Position.NOPOS, key, args);
   127     }
   129     /** Report an error, unless another error was already reported at same
   130      *  source position.
   131      *  @param pos    The source position at which to report the error.
   132      *  @param key    The key for the localized error message.
   133      *  @param args   Fields of the error message.
   134      */
   135     public void aptError(int pos, String key, Object ... args) {
   136         report(aptDiags.error(source, new SimpleDiagnosticPosition(pos), key, args));
   137     }
   139     /** Report a warning, unless suppressed by the  -nowarn option or the
   140      *  maximum number of warnings has been reached.
   141      *  @param key    The key for the localized warning message.
   142      *  @param args   Fields of the warning message.
   143      */
   144     public void aptWarning(String key, Object... args) {
   145         aptWarning(Position.NOPOS, key, args);
   146     }
   148     /** Report a warning, unless suppressed by the  -nowarn option or the
   149      *  maximum number of warnings has been reached.
   150      *  @param pos    The source position at which to report the warning.
   151      *  @param key    The key for the localized warning message.
   152      *  @param args   Fields of the warning message.
   153      */
   154     public void aptWarning(int pos, String key, Object ... args) {
   155         report(aptDiags.warning(source, new SimpleDiagnosticPosition(pos), key, args));
   156     }
   158     /** Report a note, unless suppressed by the  -nowarn option.
   159      *  @param key    The key for the localized note message.
   160      *  @param args   Fields of the note message.
   161      */
   162     public void aptNote(String key, Object... args) {
   163         aptNote(Position.NOPOS, key, args);
   164     }
   166     /** Report a note, unless suppressed by the  -nowarn option.
   167      *  @param pos    The source position at which to report the note.
   168      *  @param key    The key for the localized note message.
   169      *  @param args   Fields of the note message.
   170      */
   171     public void aptNote(int pos, String key, Object ... args) {
   172         report(aptDiags.note(source, new SimpleDiagnosticPosition(pos), key, args));
   173     }
   174 }

mercurial