src/share/classes/com/sun/tools/javadoc/Messager.java

Tue, 11 Aug 2009 01:13:14 +0100

author
mcimadamore
date
Tue, 11 Aug 2009 01:13:14 +0100
changeset 359
8227961c64d3
parent 229
03bcd66bd8e7
child 554
9d9f26857129
permissions
-rw-r--r--

6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg

     1 /*
     2  * Copyright 1997-2009 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.javadoc;
    28 import java.io.PrintWriter;
    29 import java.text.MessageFormat;
    30 import java.util.ResourceBundle;
    31 import java.util.MissingResourceException;
    33 import com.sun.javadoc.*;
    35 import com.sun.tools.javac.util.Context;
    37 import com.sun.tools.javac.util.Log;  // Access to 'javac' output streams
    39 /**
    40  * Utility for integrating with javadoc tools and for localization.
    41  * Handle Resources. Access to error and warning counts.
    42  * Message formatting.
    43  * <br>
    44  * Also provides implementation for DocErrorReporter.
    45  *
    46  * @see java.util.ResourceBundle
    47  * @see java.text.MessageFormat
    48  * @author Neal Gafter (rewrite)
    49  */
    50 public class Messager extends Log implements DocErrorReporter {
    52     /** Get the current messager, which is also the compiler log. */
    53     public static Messager instance0(Context context) {
    54         Log instance = context.get(logKey);
    55         if (instance == null || !(instance instanceof Messager))
    56             throw new InternalError("no messager instance!");
    57         return (Messager)instance;
    58     }
    60     public static void preRegister(final Context context,
    61                                    final String programName) {
    62         context.put(logKey, new Context.Factory<Log>() {
    63             public Log make() {
    64                 return new Messager(context,
    65                                     programName);
    66             }
    67         });
    68     }
    69     public static void preRegister(final Context context,
    70                                    final String programName,
    71                                    final PrintWriter errWriter,
    72                                    final PrintWriter warnWriter,
    73                                    final PrintWriter noticeWriter) {
    74         context.put(logKey, new Context.Factory<Log>() {
    75             public Log make() {
    76                 return new Messager(context,
    77                                     programName,
    78                                     errWriter,
    79                                     warnWriter,
    80                                     noticeWriter);
    81             }
    82         });
    83     }
    85     public class ExitJavadoc extends Error {
    86         private static final long serialVersionUID = 0;
    87     }
    89     private final String programName;
    91     private ResourceBundle messageRB = null;
    93     /** The default writer for diagnostics
    94      */
    95     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
    96     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
    97     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
    99     /**
   100      * Constructor
   101      * @param programName  Name of the program (for error messages).
   102      */
   103     protected Messager(Context context, String programName) {
   104         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   105     }
   107     /**
   108      * Constructor
   109      * @param programName  Name of the program (for error messages).
   110      * @param errWriter    Stream for error messages
   111      * @param warnWriter   Stream for warnings
   112      * @param noticeWriter Stream for other messages
   113      */
   114     @SuppressWarnings("deprecation")
   115     protected Messager(Context context,
   116                        String programName,
   117                        PrintWriter errWriter,
   118                        PrintWriter warnWriter,
   119                        PrintWriter noticeWriter) {
   120         super(context, errWriter, warnWriter, noticeWriter);
   121         this.programName = programName;
   122     }
   124     /**
   125      * Reset resource bundle, eg. locale has changed.
   126      */
   127     public void reset() {
   128         messageRB = null;
   129     }
   131     /**
   132      * Get string from ResourceBundle, initialize ResourceBundle
   133      * if needed.
   134      */
   135     private String getString(String key) {
   136         ResourceBundle messageRB = this.messageRB;
   137         if (messageRB == null) {
   138             try {
   139                 this.messageRB = messageRB =
   140                     ResourceBundle.getBundle(
   141                           "com.sun.tools.javadoc.resources.javadoc");
   142             } catch (MissingResourceException e) {
   143                 throw new Error("Fatal: Resource for javadoc is missing");
   144             }
   145         }
   146         return messageRB.getString(key);
   147     }
   149     /**
   150      * get and format message string from resource
   151      *
   152      * @param key selects message from resource
   153      */
   154     String getText(String key) {
   155         return getText(key, (String)null);
   156     }
   158     /**
   159      * get and format message string from resource
   160      *
   161      * @param key selects message from resource
   162      * @param a1 first argument
   163      */
   164     String getText(String key, String a1) {
   165         return getText(key, a1, null);
   166     }
   168     /**
   169      * get and format message string from resource
   170      *
   171      * @param key selects message from resource
   172      * @param a1 first argument
   173      * @param a2 second argument
   174      */
   175     String getText(String key, String a1, String a2) {
   176         return getText(key, a1, a2, null);
   177     }
   179     /**
   180      * get and format message string from resource
   181      *
   182      * @param key selects message from resource
   183      * @param a1 first argument
   184      * @param a2 second argument
   185      * @param a3 third argument
   186      */
   187     String getText(String key, String a1, String a2, String a3) {
   188         return getText(key, a1, a2, a3, null);
   189     }
   191     /**
   192      * get and format message string from resource
   193      *
   194      * @param key selects message from resource
   195      * @param a1 first argument
   196      * @param a2 second argument
   197      * @param a3 third argument
   198      * @param a4 fourth argument
   199      */
   200     String getText(String key, String a1, String a2, String a3,
   201                           String a4) {
   202         try {
   203             String message = getString(key);
   204             String[] args = new String[4];
   205             args[0] = a1;
   206             args[1] = a2;
   207             args[2] = a3;
   208             args[3] = a4;
   209             return MessageFormat.format(message, (Object[])args);
   210         } catch (MissingResourceException e) {
   211             return "********** Resource for javadoc is broken. There is no " +
   212                 key + " key in resource.";
   213         }
   214     }
   216     /**
   217      * Print error message, increment error count.
   218      * Part of DocErrorReporter.
   219      *
   220      * @param msg message to print
   221      */
   222     public void printError(String msg) {
   223         printError(null, msg);
   224     }
   226     /**
   227      * Print error message, increment error count.
   228      * Part of DocErrorReporter.
   229      *
   230      * @param pos the position where the error occurs
   231      * @param msg message to print
   232      */
   233     public void printError(SourcePosition pos, String msg) {
   234         String prefix = (pos == null) ? programName : pos.toString();
   235         errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   236         errWriter.flush();
   237         prompt();
   238         nerrors++;
   239     }
   241     /**
   242      * Print warning message, increment warning count.
   243      * Part of DocErrorReporter.
   244      *
   245      * @param msg message to print
   246      */
   247     public void printWarning(String msg) {
   248         printWarning(null, msg);
   249     }
   251     /**
   252      * Print warning message, increment warning count.
   253      * Part of DocErrorReporter.
   254      *
   255      * @param pos the position where the error occurs
   256      * @param msg message to print
   257      */
   258     public void printWarning(SourcePosition pos, String msg) {
   259         String prefix = (pos == null) ? programName : pos.toString();
   260         warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   261         warnWriter.flush();
   262         nwarnings++;
   263     }
   265     /**
   266      * Print a message.
   267      * Part of DocErrorReporter.
   268      *
   269      * @param msg message to print
   270      */
   271     public void printNotice(String msg) {
   272         printNotice(null, msg);
   273     }
   275     /**
   276      * Print a message.
   277      * Part of DocErrorReporter.
   278      *
   279      * @param pos the position where the error occurs
   280      * @param msg message to print
   281      */
   282     public void printNotice(SourcePosition pos, String msg) {
   283         if (pos == null)
   284             noticeWriter.println(msg);
   285         else
   286             noticeWriter.println(pos + ": " + msg);
   287         noticeWriter.flush();
   288     }
   290     /**
   291      * Print error message, increment error count.
   292      *
   293      * @param key selects message from resource
   294      */
   295     public void error(SourcePosition pos, String key) {
   296         printError(pos, getText(key));
   297     }
   299     /**
   300      * Print error message, increment error count.
   301      *
   302      * @param key selects message from resource
   303      * @param a1 first argument
   304      */
   305     public void error(SourcePosition pos, String key, String a1) {
   306         printError(pos, getText(key, a1));
   307     }
   309     /**
   310      * Print error message, increment error count.
   311      *
   312      * @param key selects message from resource
   313      * @param a1 first argument
   314      * @param a2 second argument
   315      */
   316     public void error(SourcePosition pos, String key, String a1, String a2) {
   317         printError(pos, getText(key, a1, a2));
   318     }
   320     /**
   321      * Print error message, increment error count.
   322      *
   323      * @param key selects message from resource
   324      * @param a1 first argument
   325      * @param a2 second argument
   326      * @param a3 third argument
   327      */
   328     public void error(SourcePosition pos, String key, String a1, String a2, String a3) {
   329         printError(pos, getText(key, a1, a2, a3));
   330     }
   332     /**
   333      * Print warning message, increment warning count.
   334      *
   335      * @param key selects message from resource
   336      */
   337     public void warning(SourcePosition pos, String key) {
   338         printWarning(pos, getText(key));
   339     }
   341     /**
   342      * Print warning message, increment warning count.
   343      *
   344      * @param key selects message from resource
   345      * @param a1 first argument
   346      */
   347     public void warning(SourcePosition pos, String key, String a1) {
   348         printWarning(pos, getText(key, a1));
   349     }
   351     /**
   352      * Print warning message, increment warning count.
   353      *
   354      * @param key selects message from resource
   355      * @param a1 first argument
   356      * @param a2 second argument
   357      */
   358     public void warning(SourcePosition pos, String key, String a1, String a2) {
   359         printWarning(pos, getText(key, a1, a2));
   360     }
   362     /**
   363      * Print warning message, increment warning count.
   364      *
   365      * @param key selects message from resource
   366      * @param a1 first argument
   367      * @param a2 second argument
   368      * @param a3 third argument
   369      */
   370     public void warning(SourcePosition pos, String key, String a1, String a2, String a3) {
   371         printWarning(pos, getText(key, a1, a2, a3));
   372     }
   374     /**
   375      * Print warning message, increment warning count.
   376      *
   377      * @param key selects message from resource
   378      * @param a1 first argument
   379      * @param a2 second argument
   380      * @param a3 third argument
   381      */
   382     public void warning(SourcePosition pos, String key, String a1, String a2, String a3,
   383                         String a4) {
   384         printWarning(pos, getText(key, a1, a2, a3, a4));
   385     }
   387     /**
   388      * Print a message.
   389      *
   390      * @param key selects message from resource
   391      */
   392     public void notice(String key) {
   393         printNotice(getText(key));
   394     }
   396     /**
   397      * Print a message.
   398      *
   399      * @param key selects message from resource
   400      * @param a1 first argument
   401      */
   402     public void notice(String key, String a1) {
   403         printNotice(getText(key, a1));
   404     }
   406     /**
   407      * Print a message.
   408      *
   409      * @param key selects message from resource
   410      * @param a1 first argument
   411      * @param a2 second argument
   412      */
   413     public void notice(String key, String a1, String a2) {
   414         printNotice(getText(key, a1, a2));
   415     }
   417     /**
   418      * Print a message.
   419      *
   420      * @param key selects message from resource
   421      * @param a1 first argument
   422      * @param a2 second argument
   423      * @param a3 third argument
   424      */
   425     public void notice(String key, String a1, String a2, String a3) {
   426         printNotice(getText(key, a1, a2, a3));
   427     }
   429     /**
   430      * Return total number of errors, including those recorded
   431      * in the compilation log.
   432      */
   433     public int nerrors() { return nerrors; }
   435     /**
   436      * Return total number of warnings, including those recorded
   437      * in the compilation log.
   438      */
   439     public int nwarnings() { return nwarnings; }
   441     /**
   442      * Print exit message.
   443      */
   444     public void exitNotice() {
   445         int nerrors = nerrors();
   446         int nwarnings = nwarnings();
   447         if (nerrors > 0) {
   448             notice((nerrors > 1) ? "main.errors" : "main.error",
   449                    "" + nerrors);
   450         }
   451         if (nwarnings > 0) {
   452             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   453                    "" + nwarnings);
   454         }
   455     }
   457     /**
   458      * Force program exit, e.g., from a fatal error.
   459      * <p>
   460      * TODO: This method does not really belong here.
   461      */
   462     public void exit() {
   463         throw new ExitJavadoc();
   464     }
   466 }

mercurial