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