Wed, 25 Mar 2009 10:29:28 +0000
6400189: raw types and inference
Summary: Fixed resolution problem with raw overriding (CCC)
Reviewed-by: jjg
1 /*
2 * Copyright 2005-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 */
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 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 class JavacMessages implements Messages {
46 /** The context key for the JavacMessages object. */
47 protected 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);
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 bundleNames = List.nil();
89 bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
90 add(bundleName);
91 setCurrentLocale(Locale.getDefault());
92 }
94 public JavacMessages() throws MissingResourceException {
95 this(defaultBundleName);
96 }
98 public void add(String bundleName) throws MissingResourceException {
99 bundleNames = bundleNames.prepend(bundleName);
100 if (!bundleCache.isEmpty())
101 bundleCache.clear();
102 currentBundles = null;
103 }
105 public List<ResourceBundle> getBundles(Locale locale) {
106 if (locale == currentLocale && currentBundles != null)
107 return currentBundles;
108 SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
109 List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
110 if (bundleList == null) {
111 bundleList = List.nil();
112 for (String bundleName : bundleNames) {
113 try {
114 ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
115 bundleList = bundleList.prepend(rb);
116 } catch (MissingResourceException e) {
117 throw new InternalError("Cannot find javac resource bundle for locale " + locale);
118 }
119 }
120 bundleCache.put(locale, new SoftReference<List<ResourceBundle>>(bundleList));
121 }
122 return bundleList;
123 }
125 /** Gets the localized string corresponding to a key, formatted with a set of args.
126 */
127 public String getLocalizedString(String key, Object... args) {
128 return getLocalizedString(currentLocale, key, args);
129 }
131 public String getLocalizedString(Locale l, String key, Object... args) {
132 if (l == null)
133 l = getCurrentLocale();
134 return getLocalizedString(getBundles(l), key, args);
135 }
137 /* Static access:
138 * javac has a firmly entrenched notion of a default message bundle
139 * which it can access from any static context. This is used to get
140 * easy access to simple localized strings.
141 */
143 private static final String defaultBundleName =
144 "com.sun.tools.javac.resources.compiler";
145 private static ResourceBundle defaultBundle;
146 private static JavacMessages defaultMessages;
149 /**
150 * Gets a localized string from the compiler's default bundle.
151 */
152 // used to support legacy Log.getLocalizedString
153 static String getDefaultLocalizedString(String key, Object... args) {
154 return getLocalizedString(List.of(getDefaultBundle()), key, args);
155 }
157 // used to support legacy static Diagnostic.fragment
158 @Deprecated
159 static JavacMessages getDefaultMessages() {
160 if (defaultMessages == null)
161 defaultMessages = new JavacMessages(defaultBundleName);
162 return defaultMessages;
163 }
165 public static ResourceBundle getDefaultBundle() {
166 try {
167 if (defaultBundle == null)
168 defaultBundle = ResourceBundle.getBundle(defaultBundleName);
169 return defaultBundle;
170 }
171 catch (MissingResourceException e) {
172 throw new Error("Fatal: Resource for compiler is missing", e);
173 }
174 }
176 private static String getLocalizedString(List<ResourceBundle> bundles,
177 String key,
178 Object... args) {
179 String msg = null;
180 for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
181 ResourceBundle rb = l.head;
182 try {
183 msg = rb.getString(key);
184 }
185 catch (MissingResourceException e) {
186 // ignore, try other bundles in list
187 }
188 }
189 if (msg == null) {
190 msg = "compiler message file broken: key=" + key +
191 " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
192 }
193 return MessageFormat.format(msg, args);
194 }
195 }