langtools/src/share/classes/com/sun/tools/javac/util/AbstractLog.java
changeset 867 1dff24b5f407
child 1591 e5a618442f5f
equal deleted inserted replaced
865:21668f049d28 867:1dff24b5f407
       
     1 /*
       
     2  * Copyright 1999-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  */
       
    25 
       
    26 package com.sun.tools.javac.util;
       
    27 
       
    28 import java.util.HashMap;
       
    29 import java.util.Map;
       
    30 import javax.tools.JavaFileObject;
       
    31 
       
    32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
       
    33 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
       
    34 
       
    35 
       
    36 /**
       
    37  *  A base class for error logs. Reports errors and warnings, and
       
    38  *  keeps track of error numbers and positions.
       
    39  *
       
    40  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
       
    41  *  you write code that depends on this, you do so at your own risk.
       
    42  *  This code and its internal interfaces are subject to change or
       
    43  *  deletion without notice.</b>
       
    44  */
       
    45 public abstract class AbstractLog {
       
    46     AbstractLog(JCDiagnostic.Factory diags) {
       
    47         this.diags = diags;
       
    48         sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
       
    49     }
       
    50 
       
    51     /** Re-assign source, returning previous setting.
       
    52      */
       
    53     public JavaFileObject useSource(JavaFileObject file) {
       
    54         JavaFileObject prev = (source == null ? null : source.getFile());
       
    55         source = getSource(file);
       
    56         return prev;
       
    57     }
       
    58 
       
    59     protected DiagnosticSource getSource(JavaFileObject file) {
       
    60         if (file == null)
       
    61             return null;
       
    62         DiagnosticSource s = sourceMap.get(file);
       
    63         if (s == null) {
       
    64             s = new DiagnosticSource(file, this);
       
    65             sourceMap.put(file, s);
       
    66         }
       
    67         return s;
       
    68     }
       
    69 
       
    70     /** Report an error, unless another error was already reported at same
       
    71      *  source position.
       
    72      *  @param key    The key for the localized error message.
       
    73      *  @param args   Fields of the error message.
       
    74      */
       
    75     public void error(String key, Object ... args) {
       
    76         report(diags.error(source, null, key, args));
       
    77     }
       
    78 
       
    79     /** Report an error, unless another error was already reported at same
       
    80      *  source position.
       
    81      *  @param pos    The source position at which to report the error.
       
    82      *  @param key    The key for the localized error message.
       
    83      *  @param args   Fields of the error message.
       
    84      */
       
    85     public void error(DiagnosticPosition pos, String key, Object ... args) {
       
    86         report(diags.error(source, pos, key, args));
       
    87     }
       
    88 
       
    89     /** Report an error, unless another error was already reported at same
       
    90      *  source position.
       
    91      *  @param pos    The source position at which to report the error.
       
    92      *  @param key    The key for the localized error message.
       
    93      *  @param args   Fields of the error message.
       
    94      */
       
    95     public void error(int pos, String key, Object ... args) {
       
    96         report(diags.error(source, wrap(pos), key, args));
       
    97     }
       
    98 
       
    99     /** Report a warning, unless suppressed by the  -nowarn option or the
       
   100      *  maximum number of warnings has been reached.
       
   101      *  @param pos    The source position at which to report the warning.
       
   102      *  @param key    The key for the localized warning message.
       
   103      *  @param args   Fields of the warning message.
       
   104      */
       
   105     public void warning(String key, Object ... args) {
       
   106         report(diags.warning(source, null, key, args));
       
   107     }
       
   108 
       
   109     /** Report a warning, unless suppressed by the  -nowarn option or the
       
   110      *  maximum number of warnings has been reached.
       
   111      *  @param pos    The source position at which to report the warning.
       
   112      *  @param key    The key for the localized warning message.
       
   113      *  @param args   Fields of the warning message.
       
   114      */
       
   115     public void warning(DiagnosticPosition pos, String key, Object ... args) {
       
   116         report(diags.warning(source, pos, key, args));
       
   117     }
       
   118 
       
   119     /** Report a warning, unless suppressed by the  -nowarn option or the
       
   120      *  maximum number of warnings has been reached.
       
   121      *  @param pos    The source position at which to report the warning.
       
   122      *  @param key    The key for the localized warning message.
       
   123      *  @param args   Fields of the warning message.
       
   124      */
       
   125     public void warning(int pos, String key, Object ... args) {
       
   126         report(diags.warning(source, wrap(pos), key, args));
       
   127     }
       
   128 
       
   129     /** Report a warning.
       
   130      *  @param pos    The source position at which to report the warning.
       
   131      *  @param key    The key for the localized warning message.
       
   132      *  @param args   Fields of the warning message.
       
   133      */
       
   134     public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
       
   135         report(diags.mandatoryWarning(source, pos, key, args));
       
   136     }
       
   137 
       
   138     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
       
   139      *  @param key    The key for the localized notification message.
       
   140      *  @param args   Fields of the notint an error or warning message:
       
   141      */
       
   142     public void note(String key, Object ... args) {
       
   143         report(diags.note(source, null, key, args));
       
   144     }
       
   145 
       
   146     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
       
   147      *  @param key    The key for the localized notification message.
       
   148      *  @param args   Fields of the notification message.
       
   149      */
       
   150     public void note(DiagnosticPosition pos, String key, Object ... args) {
       
   151         report(diags.note(source, pos, key, args));
       
   152     }
       
   153 
       
   154     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
       
   155      *  @param key    The key for the localized notification message.
       
   156      *  @param args   Fields of the notification message.
       
   157      */
       
   158     public void note(int pos, String key, Object ... args) {
       
   159         report(diags.note(source, wrap(pos), key, args));
       
   160     }
       
   161 
       
   162     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
       
   163      *  @param key    The key for the localized notification message.
       
   164      *  @param args   Fields of the notification message.
       
   165      */
       
   166     public void note(JavaFileObject file, String key, Object ... args) {
       
   167         report(diags.note(getSource(file), null, key, args));
       
   168     }
       
   169 
       
   170     /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
       
   171      *  @param key    The key for the localized notification message.
       
   172      *  @param args   Fields of the notification message.
       
   173      */
       
   174     public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
       
   175         report(diags.mandatoryNote(getSource(file), key, args));
       
   176     }
       
   177 
       
   178     protected abstract void report(JCDiagnostic diagnostic);
       
   179 
       
   180     protected abstract void directError(String key, Object... args);
       
   181 
       
   182     private DiagnosticPosition wrap(int pos) {
       
   183         return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
       
   184     }
       
   185 
       
   186     /** Factory for diagnostics
       
   187      */
       
   188     protected JCDiagnostic.Factory diags;
       
   189 
       
   190     /** The file that's currently being translated.
       
   191      */
       
   192     protected DiagnosticSource source;
       
   193 
       
   194     /** A cache of lightweight DiagnosticSource objects.
       
   195      */
       
   196     protected Map<JavaFileObject, DiagnosticSource> sourceMap;
       
   197 }