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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.util;
aoqi@0 27
aoqi@0 28 import com.sun.tools.javac.api.Messages;
aoqi@0 29 import java.lang.ref.SoftReference;
aoqi@0 30 import java.util.ResourceBundle;
aoqi@0 31 import java.util.MissingResourceException;
aoqi@0 32 import java.text.MessageFormat;
aoqi@0 33 import java.util.HashMap;
aoqi@0 34 import java.util.Locale;
aoqi@0 35 import java.util.Map;
aoqi@0 36
aoqi@0 37 /**
aoqi@0 38 * Support for formatted localized messages.
aoqi@0 39 *
aoqi@0 40 * <p><b>This is NOT part of any supported API.
aoqi@0 41 * If you write code that depends on this, you do so at your own risk.
aoqi@0 42 * This code and its internal interfaces are subject to change or
aoqi@0 43 * deletion without notice.</b>
aoqi@0 44 */
aoqi@0 45 public class JavacMessages implements Messages {
aoqi@0 46 /** The context key for the JavacMessages object. */
aoqi@0 47 public static final Context.Key<JavacMessages> messagesKey =
aoqi@0 48 new Context.Key<JavacMessages>();
aoqi@0 49
aoqi@0 50 /** Get the JavacMessages instance for this context. */
aoqi@0 51 public static JavacMessages instance(Context context) {
aoqi@0 52 JavacMessages instance = context.get(messagesKey);
aoqi@0 53 if (instance == null)
aoqi@0 54 instance = new JavacMessages(context);
aoqi@0 55 return instance;
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache;
aoqi@0 59
aoqi@0 60 private List<String> bundleNames;
aoqi@0 61
aoqi@0 62 private Locale currentLocale;
aoqi@0 63 private List<ResourceBundle> currentBundles;
aoqi@0 64
aoqi@0 65 public Locale getCurrentLocale() {
aoqi@0 66 return currentLocale;
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 public void setCurrentLocale(Locale locale) {
aoqi@0 70 if (locale == null) {
aoqi@0 71 locale = Locale.getDefault();
aoqi@0 72 }
aoqi@0 73 this.currentBundles = getBundles(locale);
aoqi@0 74 this.currentLocale = locale;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 /** Creates a JavacMessages object.
aoqi@0 78 */
aoqi@0 79 public JavacMessages(Context context) {
aoqi@0 80 this(defaultBundleName, context.get(Locale.class));
aoqi@0 81 context.put(messagesKey, this);
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 /** Creates a JavacMessages object.
aoqi@0 85 * @param bundleName the name to identify the resource bundle of localized messages.
aoqi@0 86 */
aoqi@0 87 public JavacMessages(String bundleName) throws MissingResourceException {
aoqi@0 88 this(bundleName, null);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 /** Creates a JavacMessages object.
aoqi@0 92 * @param bundleName the name to identify the resource bundle of localized messages.
aoqi@0 93 */
aoqi@0 94 public JavacMessages(String bundleName, Locale locale) throws MissingResourceException {
aoqi@0 95 bundleNames = List.nil();
aoqi@0 96 bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
aoqi@0 97 add(bundleName);
aoqi@0 98 setCurrentLocale(locale);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 public JavacMessages() throws MissingResourceException {
aoqi@0 102 this(defaultBundleName);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public void add(String bundleName) throws MissingResourceException {
aoqi@0 106 bundleNames = bundleNames.prepend(bundleName);
aoqi@0 107 if (!bundleCache.isEmpty())
aoqi@0 108 bundleCache.clear();
aoqi@0 109 currentBundles = null;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public List<ResourceBundle> getBundles(Locale locale) {
aoqi@0 113 if (locale == currentLocale && currentBundles != null)
aoqi@0 114 return currentBundles;
aoqi@0 115 SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
aoqi@0 116 List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
aoqi@0 117 if (bundleList == null) {
aoqi@0 118 bundleList = List.nil();
aoqi@0 119 for (String bundleName : bundleNames) {
aoqi@0 120 try {
aoqi@0 121 ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
aoqi@0 122 bundleList = bundleList.prepend(rb);
aoqi@0 123 } catch (MissingResourceException e) {
aoqi@0 124 throw new InternalError("Cannot find javac resource bundle for locale " + locale);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127 bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
aoqi@0 128 }
aoqi@0 129 return bundleList;
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 /** Gets the localized string corresponding to a key, formatted with a set of args.
aoqi@0 133 */
aoqi@0 134 public String getLocalizedString(String key, Object... args) {
aoqi@0 135 return getLocalizedString(currentLocale, key, args);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public String getLocalizedString(Locale l, String key, Object... args) {
aoqi@0 139 if (l == null)
aoqi@0 140 l = getCurrentLocale();
aoqi@0 141 return getLocalizedString(getBundles(l), key, args);
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 /* Static access:
aoqi@0 145 * javac has a firmly entrenched notion of a default message bundle
aoqi@0 146 * which it can access from any static context. This is used to get
aoqi@0 147 * easy access to simple localized strings.
aoqi@0 148 */
aoqi@0 149
aoqi@0 150 private static final String defaultBundleName =
aoqi@0 151 "com.sun.tools.javac.resources.compiler";
aoqi@0 152 private static ResourceBundle defaultBundle;
aoqi@0 153 private static JavacMessages defaultMessages;
aoqi@0 154
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 * Gets a localized string from the compiler's default bundle.
aoqi@0 158 */
aoqi@0 159 // used to support legacy Log.getLocalizedString
aoqi@0 160 static String getDefaultLocalizedString(String key, Object... args) {
aoqi@0 161 return getLocalizedString(List.of(getDefaultBundle()), key, args);
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 // used to support legacy static Diagnostic.fragment
aoqi@0 165 @Deprecated
aoqi@0 166 static JavacMessages getDefaultMessages() {
aoqi@0 167 if (defaultMessages == null)
aoqi@0 168 defaultMessages = new JavacMessages(defaultBundleName);
aoqi@0 169 return defaultMessages;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 public static ResourceBundle getDefaultBundle() {
aoqi@0 173 try {
aoqi@0 174 if (defaultBundle == null)
aoqi@0 175 defaultBundle = ResourceBundle.getBundle(defaultBundleName);
aoqi@0 176 return defaultBundle;
aoqi@0 177 }
aoqi@0 178 catch (MissingResourceException e) {
aoqi@0 179 throw new Error("Fatal: Resource for compiler is missing", e);
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 private static String getLocalizedString(List<ResourceBundle> bundles,
aoqi@0 184 String key,
aoqi@0 185 Object... args) {
aoqi@0 186 String msg = null;
aoqi@0 187 for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
aoqi@0 188 ResourceBundle rb = l.head;
aoqi@0 189 try {
aoqi@0 190 msg = rb.getString(key);
aoqi@0 191 }
aoqi@0 192 catch (MissingResourceException e) {
aoqi@0 193 // ignore, try other bundles in list
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 if (msg == null) {
aoqi@0 197 msg = "compiler message file broken: key=" + key +
aoqi@0 198 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
aoqi@0 199 }
aoqi@0 200 return MessageFormat.format(msg, args);
aoqi@0 201 }
aoqi@0 202 }

mercurial