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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1074
04f983e3e825
child 1358
fc123bdeddb8
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1999, 2011, 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  */
    26 package com.sun.tools.javac.util;
    28 import java.util.HashMap;
    29 import java.util.Map;
    30 import javax.tools.JavaFileObject;
    32 import com.sun.tools.javac.code.Lint.LintCategory;
    33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    34 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    35 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
    38 /**
    39  *  A base class for error logs. Reports errors and warnings, and
    40  *  keeps track of error numbers and positions.
    41  *
    42  *  <p><b>This is NOT part of any supported API.
    43  *  If you write code that depends on this, you do so at your own risk.
    44  *  This code and its internal interfaces are subject to change or
    45  *  deletion without notice.</b>
    46  */
    47 public abstract class AbstractLog {
    48     AbstractLog(JCDiagnostic.Factory diags) {
    49         this.diags = diags;
    50         sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
    51     }
    53     /** Re-assign source, returning previous setting.
    54      */
    55     public JavaFileObject useSource(JavaFileObject file) {
    56         JavaFileObject prev = (source == null ? null : source.getFile());
    57         source = getSource(file);
    58         return prev;
    59     }
    61     protected DiagnosticSource getSource(JavaFileObject file) {
    62         if (file == null)
    63             return DiagnosticSource.NO_SOURCE;
    64         DiagnosticSource s = sourceMap.get(file);
    65         if (s == null) {
    66             s = new DiagnosticSource(file, this);
    67             sourceMap.put(file, s);
    68         }
    69         return s;
    70     }
    72     /** Return the underlying diagnostic source
    73      */
    74     public DiagnosticSource currentSource() {
    75         return source;
    76     }
    78     /** Report an error, unless another error was already reported at same
    79      *  source position.
    80      *  @param key    The key for the localized error message.
    81      *  @param args   Fields of the error message.
    82      */
    83     public void error(String key, Object ... args) {
    84         report(diags.error(source, null, key, args));
    85     }
    87     /** Report an error, unless another error was already reported at same
    88      *  source position.
    89      *  @param pos    The source position at which to report the error.
    90      *  @param key    The key for the localized error message.
    91      *  @param args   Fields of the error message.
    92      */
    93     public void error(DiagnosticPosition pos, String key, Object ... args) {
    94         report(diags.error(source, pos, key, args));
    95     }
    97     /** Report an error, unless another error was already reported at same
    98      *  source position.
    99      *  @param flag   A flag to set on the diagnostic
   100      *  @param pos    The source position at which to report the error.
   101      *  @param key    The key for the localized error message.
   102      *  @param args   Fields of the error message.
   103      */
   104     public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) {
   105         JCDiagnostic d = diags.error(source, pos, key, args);
   106         d.setFlag(flag);
   107         report(d);
   108     }
   110     /** Report an error, unless another error was already reported at same
   111      *  source position.
   112      *  @param pos    The source position at which to report the error.
   113      *  @param key    The key for the localized error message.
   114      *  @param args   Fields of the error message.
   115      */
   116     public void error(int pos, String key, Object ... args) {
   117         report(diags.error(source, wrap(pos), key, args));
   118     }
   120     /** Report an error, unless another error was already reported at same
   121      *  source position.
   122      *  @param flag   A flag to set on the diagnostic
   123      *  @param pos    The source position at which to report the error.
   124      *  @param key    The key for the localized error message.
   125      *  @param args   Fields of the error message.
   126      */
   127     public void error(DiagnosticFlag flag, int pos, String key, Object ... args) {
   128         JCDiagnostic d = diags.error(source, wrap(pos), key, args);
   129         d.setFlag(flag);
   130         report(d);
   131     }
   133     /** Report a warning, unless suppressed by the  -nowarn option or the
   134      *  maximum number of warnings has been reached.
   135      *  @param pos    The source position at which to report the warning.
   136      *  @param key    The key for the localized warning message.
   137      *  @param args   Fields of the warning message.
   138      */
   139     public void warning(String key, Object ... args) {
   140         report(diags.warning(source, null, key, args));
   141     }
   143     /** Report a lint warning, unless suppressed by the  -nowarn option or the
   144      *  maximum number of warnings has been reached.
   145      *  @param lc     The lint category for the diagnostic
   146      *  @param key    The key for the localized warning message.
   147      *  @param args   Fields of the warning message.
   148      */
   149     public void warning(LintCategory lc, String key, Object ... args) {
   150         report(diags.warning(lc, key, args));
   151     }
   153     /** Report a warning, unless suppressed by the  -nowarn option or the
   154      *  maximum number of warnings has been reached.
   155      *  @param pos    The source position at which to report the warning.
   156      *  @param key    The key for the localized warning message.
   157      *  @param args   Fields of the warning message.
   158      */
   159     public void warning(DiagnosticPosition pos, String key, Object ... args) {
   160         report(diags.warning(source, pos, key, args));
   161     }
   163     /** Report a lint warning, unless suppressed by the  -nowarn option or the
   164      *  maximum number of warnings has been reached.
   165      *  @param lc     The lint category for the diagnostic
   166      *  @param pos    The source position at which to report the warning.
   167      *  @param key    The key for the localized warning message.
   168      *  @param args   Fields of the warning message.
   169      */
   170     public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
   171         report(diags.warning(lc, source, pos, key, args));
   172     }
   174     /** Report a warning, unless suppressed by the  -nowarn option or the
   175      *  maximum number of warnings has been reached.
   176      *  @param pos    The source position at which to report the warning.
   177      *  @param key    The key for the localized warning message.
   178      *  @param args   Fields of the warning message.
   179      */
   180     public void warning(int pos, String key, Object ... args) {
   181         report(diags.warning(source, wrap(pos), key, args));
   182     }
   184     /** Report a warning.
   185      *  @param pos    The source position at which to report the warning.
   186      *  @param key    The key for the localized warning message.
   187      *  @param args   Fields of the warning message.
   188      */
   189     public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
   190         report(diags.mandatoryWarning(source, pos, key, args));
   191     }
   193     /** Report a warning.
   194      *  @param lc     The lint category for the diagnostic
   195      *  @param pos    The source position at which to report the warning.
   196      *  @param key    The key for the localized warning message.
   197      *  @param args   Fields of the warning message.
   198      */
   199     public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
   200         report(diags.mandatoryWarning(lc, source, pos, key, args));
   201     }
   203     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
   204      *  @param key    The key for the localized notification message.
   205      *  @param args   Fields of the notint an error or warning message:
   206      */
   207     public void note(String key, Object ... args) {
   208         report(diags.note(source, null, key, args));
   209     }
   211     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
   212      *  @param key    The key for the localized notification message.
   213      *  @param args   Fields of the notification message.
   214      */
   215     public void note(DiagnosticPosition pos, String key, Object ... args) {
   216         report(diags.note(source, pos, key, args));
   217     }
   219     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
   220      *  @param key    The key for the localized notification message.
   221      *  @param args   Fields of the notification message.
   222      */
   223     public void note(int pos, String key, Object ... args) {
   224         report(diags.note(source, wrap(pos), key, args));
   225     }
   227     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
   228      *  @param key    The key for the localized notification message.
   229      *  @param args   Fields of the notification message.
   230      */
   231     public void note(JavaFileObject file, String key, Object ... args) {
   232         report(diags.note(getSource(file), null, key, args));
   233     }
   235     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
   236      *  @param key    The key for the localized notification message.
   237      *  @param args   Fields of the notification message.
   238      */
   239     public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
   240         report(diags.mandatoryNote(getSource(file), key, args));
   241     }
   243     protected abstract void report(JCDiagnostic diagnostic);
   245     protected abstract void directError(String key, Object... args);
   247     private DiagnosticPosition wrap(int pos) {
   248         return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
   249     }
   251     /** Factory for diagnostics
   252      */
   253     protected JCDiagnostic.Factory diags;
   255     /** The file that's currently being translated.
   256      */
   257     protected DiagnosticSource source;
   259     /** A cache of lightweight DiagnosticSource objects.
   260      */
   261     protected Map<JavaFileObject, DiagnosticSource> sourceMap;
   262 }

mercurial