author | jlahoda |
Fri, 01 Aug 2014 11:09:40 -0700 | |
changeset 25848 | 3bc09f4676a9 |
parent 25287 | d2440361b323 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
25287 | 2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
28 |
import com.sun.tools.javac.api.Messages; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
29 |
import java.lang.ref.SoftReference; |
10 | 30 |
import java.util.ResourceBundle; |
31 |
import java.util.MissingResourceException; |
|
32 |
import java.text.MessageFormat; |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
33 |
import java.util.HashMap; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
34 |
import java.util.Locale; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
35 |
import java.util.Map; |
10 | 36 |
|
37 |
/** |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
38 |
* Support for formatted localized messages. |
10 | 39 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
41 |
* If you write code that depends on this, you do so at your own risk. |
10 | 42 |
* This code and its internal interfaces are subject to change or |
43 |
* deletion without notice.</b> |
|
44 |
*/ |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
45 |
public class JavacMessages implements Messages { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
46 |
/** The context key for the JavacMessages object. */ |
22163 | 47 |
public static final Context.Key<JavacMessages> messagesKey = new Context.Key<>(); |
10 | 48 |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
49 |
/** Get the JavacMessages instance for this context. */ |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
50 |
public static JavacMessages instance(Context context) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
51 |
JavacMessages instance = context.get(messagesKey); |
10 | 52 |
if (instance == null) |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
53 |
instance = new JavacMessages(context); |
10 | 54 |
return instance; |
55 |
} |
|
56 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
57 |
private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
58 |
|
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
59 |
private List<String> bundleNames; |
10 | 60 |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
61 |
private Locale currentLocale; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
62 |
private List<ResourceBundle> currentBundles; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
63 |
|
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
64 |
public Locale getCurrentLocale() { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
65 |
return currentLocale; |
10 | 66 |
} |
67 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
68 |
public void setCurrentLocale(Locale locale) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
69 |
if (locale == null) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
70 |
locale = Locale.getDefault(); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
71 |
} |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
72 |
this.currentBundles = getBundles(locale); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
73 |
this.currentLocale = locale; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
74 |
} |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
75 |
|
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
76 |
/** Creates a JavacMessages object. |
10 | 77 |
*/ |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
78 |
public JavacMessages(Context context) { |
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
79 |
this(defaultBundleName, context.get(Locale.class)); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
80 |
context.put(messagesKey, this); |
10 | 81 |
} |
82 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
83 |
/** Creates a JavacMessages object. |
11052 | 84 |
* @param bundleName the name to identify the resource bundle of localized messages. |
10 | 85 |
*/ |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
86 |
public JavacMessages(String bundleName) throws MissingResourceException { |
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
87 |
this(bundleName, null); |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
88 |
} |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
89 |
|
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
90 |
/** Creates a JavacMessages object. |
11052 | 91 |
* @param bundleName the name to identify the resource bundle of localized messages. |
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
92 |
*/ |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
93 |
public JavacMessages(String bundleName, Locale locale) throws MissingResourceException { |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
94 |
bundleNames = List.nil(); |
22163 | 95 |
bundleCache = new HashMap<>(); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
96 |
add(bundleName); |
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
5847
diff
changeset
|
97 |
setCurrentLocale(locale); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
98 |
} |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
99 |
|
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
100 |
public JavacMessages() throws MissingResourceException { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
101 |
this(defaultBundleName); |
10 | 102 |
} |
103 |
||
104 |
public void add(String bundleName) throws MissingResourceException { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
105 |
bundleNames = bundleNames.prepend(bundleName); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
106 |
if (!bundleCache.isEmpty()) |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
107 |
bundleCache.clear(); |
1476
4b54f7c2d3db
6759682: APT: compiler message file broken after refactoring of com.sun.tools.javac.util.Message
mcimadamore
parents:
1471
diff
changeset
|
108 |
currentBundles = null; |
10 | 109 |
} |
110 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
111 |
public List<ResourceBundle> getBundles(Locale locale) { |
1476
4b54f7c2d3db
6759682: APT: compiler message file broken after refactoring of com.sun.tools.javac.util.Message
mcimadamore
parents:
1471
diff
changeset
|
112 |
if (locale == currentLocale && currentBundles != null) |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
113 |
return currentBundles; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
114 |
SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
115 |
List<ResourceBundle> bundleList = bundles == null ? null : bundles.get(); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
116 |
if (bundleList == null) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
117 |
bundleList = List.nil(); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
118 |
for (String bundleName : bundleNames) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
119 |
try { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
120 |
ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
121 |
bundleList = bundleList.prepend(rb); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
122 |
} catch (MissingResourceException e) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
123 |
throw new InternalError("Cannot find javac resource bundle for locale " + locale); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
124 |
} |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
125 |
} |
22163 | 126 |
bundleCache.put(locale, new SoftReference<>(bundleList)); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
127 |
} |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
128 |
return bundleList; |
10 | 129 |
} |
130 |
||
131 |
/** Gets the localized string corresponding to a key, formatted with a set of args. |
|
132 |
*/ |
|
133 |
public String getLocalizedString(String key, Object... args) { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
134 |
return getLocalizedString(currentLocale, key, args); |
10 | 135 |
} |
136 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
137 |
public String getLocalizedString(Locale l, String key, Object... args) { |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
138 |
if (l == null) |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
139 |
l = getCurrentLocale(); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
140 |
return getLocalizedString(getBundles(l), key, args); |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
141 |
} |
10 | 142 |
|
143 |
/* Static access: |
|
144 |
* javac has a firmly entrenched notion of a default message bundle |
|
145 |
* which it can access from any static context. This is used to get |
|
146 |
* easy access to simple localized strings. |
|
147 |
*/ |
|
148 |
||
149 |
private static final String defaultBundleName = |
|
150 |
"com.sun.tools.javac.resources.compiler"; |
|
151 |
private static ResourceBundle defaultBundle; |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
152 |
private static JavacMessages defaultMessages; |
10 | 153 |
|
154 |
||
155 |
/** |
|
25287 | 156 |
* Returns a localized string from the compiler's default bundle. |
10 | 157 |
*/ |
158 |
// used to support legacy Log.getLocalizedString |
|
159 |
static String getDefaultLocalizedString(String key, Object... args) { |
|
160 |
return getLocalizedString(List.of(getDefaultBundle()), key, args); |
|
161 |
} |
|
162 |
||
163 |
// used to support legacy static Diagnostic.fragment |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
164 |
@Deprecated |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
165 |
static JavacMessages getDefaultMessages() { |
10 | 166 |
if (defaultMessages == null) |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
10
diff
changeset
|
167 |
defaultMessages = new JavacMessages(defaultBundleName); |
10 | 168 |
return defaultMessages; |
169 |
} |
|
170 |
||
171 |
public static ResourceBundle getDefaultBundle() { |
|
172 |
try { |
|
173 |
if (defaultBundle == null) |
|
174 |
defaultBundle = ResourceBundle.getBundle(defaultBundleName); |
|
175 |
return defaultBundle; |
|
176 |
} |
|
177 |
catch (MissingResourceException e) { |
|
178 |
throw new Error("Fatal: Resource for compiler is missing", e); |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
private static String getLocalizedString(List<ResourceBundle> bundles, |
|
183 |
String key, |
|
184 |
Object... args) { |
|
185 |
String msg = null; |
|
21488
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
186 |
for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
187 |
ResourceBundle rb = l.head; |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
188 |
try { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
189 |
msg = rb.getString(key); |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
190 |
} |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
191 |
catch (MissingResourceException e) { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
192 |
// ignore, try other bundles in list |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
193 |
} |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
194 |
} |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
195 |
if (msg == null) { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
196 |
msg = "compiler message file broken: key=" + key + |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
197 |
" arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"; |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
198 |
} |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
11052
diff
changeset
|
199 |
return MessageFormat.format(msg, args); |
10 | 200 |
} |
201 |
} |