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

Thu, 24 Mar 2011 16:14:30 -0700

author
jjg
date
Thu, 24 Mar 2011 16:14:30 -0700
changeset 944
83260b3305ac
parent 581
f2fdd52e4e87
child 1135
36553cb94345
permissions
-rw-r--r--

6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2005, 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 com.sun.tools.javac.api.Messages;
    29 import java.lang.ref.SoftReference;
    30 import java.util.ResourceBundle;
    31 import java.util.MissingResourceException;
    32 import java.text.MessageFormat;
    33 import java.util.HashMap;
    34 import java.util.Locale;
    35 import java.util.Map;
    37 /**
    38  *  Support for formatted localized messages.
    39  *
    40  *  <p><b>This is NOT part of any supported API.
    41  *  If 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 class JavacMessages implements Messages {
    46     /** The context key for the JavacMessages object. */
    47     public static final Context.Key<JavacMessages> messagesKey =
    48         new Context.Key<JavacMessages>();
    50     /** Get the JavacMessages instance for this context. */
    51     public static JavacMessages instance(Context context) {
    52         JavacMessages instance = context.get(messagesKey);
    53         if (instance == null)
    54             instance = new JavacMessages(context);
    55         return instance;
    56     }
    58     private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache;
    60     private List<String> bundleNames;
    62     private Locale currentLocale;
    63     private List<ResourceBundle> currentBundles;
    65     public Locale getCurrentLocale() {
    66         return currentLocale;
    67     }
    69     public void setCurrentLocale(Locale locale) {
    70         if (locale == null) {
    71             locale = Locale.getDefault();
    72         }
    73         this.currentBundles = getBundles(locale);
    74         this.currentLocale = locale;
    75     }
    77     /** Creates a JavacMessages object.
    78      */
    79     public JavacMessages(Context context) {
    80         this(defaultBundleName, context.get(Locale.class));
    81         context.put(messagesKey, this);
    82     }
    84     /** Creates a JavacMessages object.
    85      * @param bundleName the name to identify the resource buundle of localized messages.
    86      */
    87     public JavacMessages(String bundleName) throws MissingResourceException {
    88         this(bundleName, null);
    89     }
    91     /** Creates a JavacMessages object.
    92      * @param bundleName the name to identify the resource buundle of localized messages.
    93      */
    94     public JavacMessages(String bundleName, Locale locale) throws MissingResourceException {
    95         bundleNames = List.nil();
    96         bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
    97         add(bundleName);
    98         setCurrentLocale(locale);
    99     }
   101     public JavacMessages() throws MissingResourceException {
   102         this(defaultBundleName);
   103     }
   105     public void add(String bundleName) throws MissingResourceException {
   106         bundleNames = bundleNames.prepend(bundleName);
   107         if (!bundleCache.isEmpty())
   108             bundleCache.clear();
   109         currentBundles = null;
   110     }
   112     public List<ResourceBundle> getBundles(Locale locale) {
   113         if (locale == currentLocale && currentBundles != null)
   114             return currentBundles;
   115         SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
   116         List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
   117         if (bundleList == null) {
   118             bundleList = List.nil();
   119             for (String bundleName : bundleNames) {
   120                 try {
   121                     ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
   122                     bundleList = bundleList.prepend(rb);
   123                 } catch (MissingResourceException e) {
   124                     throw new InternalError("Cannot find javac resource bundle for locale " + locale);
   125                 }
   126             }
   127             bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
   128         }
   129         return bundleList;
   130     }
   132     /** Gets the localized string corresponding to a key, formatted with a set of args.
   133      */
   134     public String getLocalizedString(String key, Object... args) {
   135         return getLocalizedString(currentLocale, key, args);
   136     }
   138     public String getLocalizedString(Locale l, String key, Object... args) {
   139         if (l == null)
   140             l = getCurrentLocale();
   141         return getLocalizedString(getBundles(l), key, args);
   142     }
   144     /* Static access:
   145      * javac has a firmly entrenched notion of a default message bundle
   146      * which it can access from any static context. This is used to get
   147      * easy access to simple localized strings.
   148      */
   150     private static final String defaultBundleName =
   151         "com.sun.tools.javac.resources.compiler";
   152     private static ResourceBundle defaultBundle;
   153     private static JavacMessages defaultMessages;
   156     /**
   157      * Gets a localized string from the compiler's default bundle.
   158      */
   159     // used to support legacy Log.getLocalizedString
   160     static String getDefaultLocalizedString(String key, Object... args) {
   161         return getLocalizedString(List.of(getDefaultBundle()), key, args);
   162     }
   164     // used to support legacy static Diagnostic.fragment
   165     @Deprecated
   166     static JavacMessages getDefaultMessages() {
   167         if (defaultMessages == null)
   168             defaultMessages = new JavacMessages(defaultBundleName);
   169         return defaultMessages;
   170     }
   172     public static ResourceBundle getDefaultBundle() {
   173         try {
   174             if (defaultBundle == null)
   175                 defaultBundle = ResourceBundle.getBundle(defaultBundleName);
   176             return defaultBundle;
   177         }
   178         catch (MissingResourceException e) {
   179             throw new Error("Fatal: Resource for compiler is missing", e);
   180         }
   181     }
   183     private static String getLocalizedString(List<ResourceBundle> bundles,
   184                                              String key,
   185                                              Object... args) {
   186        String msg = null;
   187         for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
   188             ResourceBundle rb = l.head;
   189             try {
   190                 msg = rb.getString(key);
   191             }
   192             catch (MissingResourceException e) {
   193                 // ignore, try other bundles in list
   194             }
   195         }
   196         if (msg == null) {
   197             msg = "compiler message file broken: key=" + key +
   198                 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
   199         }
   200         return MessageFormat.format(msg, args);
   201     }
   202 }

mercurial