Thu, 09 Oct 2008 16:19:13 +0100
6731573: diagnostic output should optionally include source line
Summary: Added an -XD option to optionally prints out source lines in error messages
Reviewed-by: jjg
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2005 Sun Microsystems, Inc. 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 |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun 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 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.util; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.ResourceBundle; |
duke@1 | 29 | import java.util.MissingResourceException; |
duke@1 | 30 | import java.text.MessageFormat; |
duke@1 | 31 | |
duke@1 | 32 | /** |
duke@1 | 33 | * Support for localized messages. |
duke@1 | 34 | * |
duke@1 | 35 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 36 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 37 | * This code and its internal interfaces are subject to change or |
duke@1 | 38 | * deletion without notice.</b> |
duke@1 | 39 | */ |
duke@1 | 40 | public class Messages { |
duke@1 | 41 | /** The context key for the Messages object. */ |
duke@1 | 42 | protected static final Context.Key<Messages> messagesKey = |
duke@1 | 43 | new Context.Key<Messages>(); |
duke@1 | 44 | |
duke@1 | 45 | /** Get the Messages instance for this context. */ |
duke@1 | 46 | public static Messages instance(Context context) { |
duke@1 | 47 | Messages instance = context.get(messagesKey); |
duke@1 | 48 | if (instance == null) |
duke@1 | 49 | instance = new Messages(context); |
duke@1 | 50 | return instance; |
duke@1 | 51 | } |
duke@1 | 52 | |
duke@1 | 53 | private List<ResourceBundle> bundles = List.nil(); |
duke@1 | 54 | |
duke@1 | 55 | /** Creates a Messages object. |
duke@1 | 56 | */ |
duke@1 | 57 | public Messages(Context context) { |
duke@1 | 58 | context.put(messagesKey, this); |
duke@1 | 59 | add(getDefaultBundle()); |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | /** Creates a Messages object. |
duke@1 | 63 | * @param bundle the name to identify the resource buundle of localized messages. |
duke@1 | 64 | */ |
duke@1 | 65 | public Messages(String bundleName) throws MissingResourceException { |
duke@1 | 66 | add(bundleName); |
duke@1 | 67 | } |
duke@1 | 68 | |
duke@1 | 69 | /** Creates a Messages object. |
duke@1 | 70 | * @param bundle the name to identif the resource buundle of localized messages. |
duke@1 | 71 | */ |
duke@1 | 72 | public Messages(ResourceBundle bundle) throws MissingResourceException { |
duke@1 | 73 | add(bundle); |
duke@1 | 74 | } |
duke@1 | 75 | |
duke@1 | 76 | /** Add a new resource bundle to the list that is searched for localized messages. |
duke@1 | 77 | * @param bundle the name to identify the resource bundle of localized messages. |
duke@1 | 78 | */ |
duke@1 | 79 | public void add(String bundleName) throws MissingResourceException { |
duke@1 | 80 | add(ResourceBundle.getBundle(bundleName)); |
duke@1 | 81 | } |
duke@1 | 82 | |
duke@1 | 83 | /** Add a new resource bundle to the list that is searched for localized messages. |
duke@1 | 84 | * Resource bundles will be searched in reverse order in which they are added. |
duke@1 | 85 | * @param bundle the bundle of localized messages. |
duke@1 | 86 | */ |
duke@1 | 87 | public void add(ResourceBundle bundle) { |
duke@1 | 88 | bundles = bundles.prepend(bundle); |
duke@1 | 89 | } |
duke@1 | 90 | |
duke@1 | 91 | /** Gets the localized string corresponding to a key, formatted with a set of args. |
duke@1 | 92 | */ |
duke@1 | 93 | public String getLocalizedString(String key, Object... args) { |
duke@1 | 94 | return getLocalizedString(bundles, key, args); |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | |
duke@1 | 98 | /* Static access: |
duke@1 | 99 | * javac has a firmly entrenched notion of a default message bundle |
duke@1 | 100 | * which it can access from any static context. This is used to get |
duke@1 | 101 | * easy access to simple localized strings. |
duke@1 | 102 | */ |
duke@1 | 103 | |
duke@1 | 104 | private static final String defaultBundleName = |
duke@1 | 105 | "com.sun.tools.javac.resources.compiler"; |
duke@1 | 106 | private static ResourceBundle defaultBundle; |
duke@1 | 107 | private static Messages defaultMessages; |
duke@1 | 108 | |
duke@1 | 109 | |
duke@1 | 110 | /** |
duke@1 | 111 | * Gets a localized string from the compiler's default bundle. |
duke@1 | 112 | */ |
duke@1 | 113 | // used to support legacy Log.getLocalizedString |
duke@1 | 114 | static String getDefaultLocalizedString(String key, Object... args) { |
duke@1 | 115 | return getLocalizedString(List.of(getDefaultBundle()), key, args); |
duke@1 | 116 | } |
duke@1 | 117 | |
duke@1 | 118 | // used to support legacy static Diagnostic.fragment |
duke@1 | 119 | static Messages getDefaultMessages() { |
duke@1 | 120 | if (defaultMessages == null) |
duke@1 | 121 | defaultMessages = new Messages(getDefaultBundle()); |
duke@1 | 122 | return defaultMessages; |
duke@1 | 123 | } |
duke@1 | 124 | |
duke@1 | 125 | public static ResourceBundle getDefaultBundle() { |
duke@1 | 126 | try { |
duke@1 | 127 | if (defaultBundle == null) |
duke@1 | 128 | defaultBundle = ResourceBundle.getBundle(defaultBundleName); |
duke@1 | 129 | return defaultBundle; |
duke@1 | 130 | } |
duke@1 | 131 | catch (MissingResourceException e) { |
duke@1 | 132 | throw new Error("Fatal: Resource for compiler is missing", e); |
duke@1 | 133 | } |
duke@1 | 134 | } |
duke@1 | 135 | |
duke@1 | 136 | private static String getLocalizedString(List<ResourceBundle> bundles, |
duke@1 | 137 | String key, |
duke@1 | 138 | Object... args) { |
duke@1 | 139 | String msg = null; |
duke@1 | 140 | for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) { |
duke@1 | 141 | ResourceBundle rb = l.head; |
duke@1 | 142 | try { |
duke@1 | 143 | msg = rb.getString(key); |
duke@1 | 144 | } |
duke@1 | 145 | catch (MissingResourceException e) { |
duke@1 | 146 | // ignore, try other bundles in list |
duke@1 | 147 | } |
duke@1 | 148 | } |
duke@1 | 149 | if (msg == null) { |
duke@1 | 150 | msg = "compiler message file broken: key=" + key + |
duke@1 | 151 | " arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"; |
duke@1 | 152 | } |
duke@1 | 153 | return MessageFormat.format(msg, args); |
duke@1 | 154 | } |
duke@1 | 155 | |
duke@1 | 156 | |
duke@1 | 157 | } |