Thu, 10 Jun 2010 09:29:23 +0100
6945418: Project Coin: Simplified Varargs Method Invocation
Summary: Add new mandatory warning for unsafe vararg method declaration. Warning can be suppressed as usual (@SuppressWarnings("varargs")/-Xlint:-varargs)
Reviewed-by: jjg, darcy
jjg@73 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
jjg@73 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@73 | 4 | * |
jjg@73 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@73 | 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 |
jjg@73 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@73 | 10 | * |
jjg@73 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@73 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@73 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@73 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@73 | 15 | * accompanied this code). |
jjg@73 | 16 | * |
jjg@73 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@73 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@73 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@73 | 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. |
jjg@73 | 24 | */ |
jjg@73 | 25 | |
jjg@73 | 26 | package com.sun.tools.javac.util; |
jjg@73 | 27 | |
jjg@73 | 28 | import java.util.HashMap; |
jjg@73 | 29 | import java.util.Map; |
jjg@73 | 30 | import javax.tools.JavaFileObject; |
jjg@73 | 31 | |
jjg@73 | 32 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
jjg@73 | 33 | import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
jjg@73 | 34 | |
jjg@73 | 35 | |
jjg@73 | 36 | /** |
jjg@73 | 37 | * A base class for error logs. Reports errors and warnings, and |
jjg@73 | 38 | * keeps track of error numbers and positions. |
jjg@73 | 39 | * |
jjg@73 | 40 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
jjg@73 | 41 | * you write code that depends on this, you do so at your own risk. |
jjg@73 | 42 | * This code and its internal interfaces are subject to change or |
jjg@73 | 43 | * deletion without notice.</b> |
jjg@73 | 44 | */ |
jjg@73 | 45 | public abstract class AbstractLog { |
jjg@73 | 46 | AbstractLog(JCDiagnostic.Factory diags) { |
jjg@73 | 47 | this.diags = diags; |
jjg@73 | 48 | sourceMap = new HashMap<JavaFileObject, DiagnosticSource>(); |
jjg@73 | 49 | } |
jjg@73 | 50 | |
jjg@73 | 51 | /** Re-assign source, returning previous setting. |
jjg@73 | 52 | */ |
jjg@73 | 53 | public JavaFileObject useSource(JavaFileObject file) { |
jjg@73 | 54 | JavaFileObject prev = (source == null ? null : source.getFile()); |
jjg@73 | 55 | source = getSource(file); |
jjg@73 | 56 | return prev; |
jjg@73 | 57 | } |
jjg@73 | 58 | |
jjg@73 | 59 | protected DiagnosticSource getSource(JavaFileObject file) { |
jjg@73 | 60 | if (file == null) |
mcimadamore@303 | 61 | return DiagnosticSource.NO_SOURCE; |
jjg@73 | 62 | DiagnosticSource s = sourceMap.get(file); |
jjg@73 | 63 | if (s == null) { |
jjg@73 | 64 | s = new DiagnosticSource(file, this); |
jjg@73 | 65 | sourceMap.put(file, s); |
jjg@73 | 66 | } |
jjg@73 | 67 | return s; |
jjg@73 | 68 | } |
jjg@73 | 69 | |
mcimadamore@168 | 70 | /** Return the underlying diagnostic source |
mcimadamore@168 | 71 | */ |
mcimadamore@168 | 72 | public DiagnosticSource currentSource() { |
mcimadamore@168 | 73 | return source; |
mcimadamore@168 | 74 | } |
mcimadamore@168 | 75 | |
jjg@73 | 76 | /** Report an error, unless another error was already reported at same |
jjg@73 | 77 | * source position. |
jjg@73 | 78 | * @param key The key for the localized error message. |
jjg@73 | 79 | * @param args Fields of the error message. |
jjg@73 | 80 | */ |
jjg@73 | 81 | public void error(String key, Object ... args) { |
jjg@73 | 82 | report(diags.error(source, null, key, args)); |
jjg@73 | 83 | } |
jjg@73 | 84 | |
jjg@73 | 85 | /** Report an error, unless another error was already reported at same |
jjg@73 | 86 | * source position. |
jjg@73 | 87 | * @param pos The source position at which to report the error. |
jjg@73 | 88 | * @param key The key for the localized error message. |
jjg@73 | 89 | * @param args Fields of the error message. |
jjg@73 | 90 | */ |
jjg@73 | 91 | public void error(DiagnosticPosition pos, String key, Object ... args) { |
jjg@73 | 92 | report(diags.error(source, pos, key, args)); |
jjg@73 | 93 | } |
jjg@73 | 94 | |
jjg@73 | 95 | /** Report an error, unless another error was already reported at same |
jjg@73 | 96 | * source position. |
jjg@73 | 97 | * @param pos The source position at which to report the error. |
jjg@73 | 98 | * @param key The key for the localized error message. |
jjg@73 | 99 | * @param args Fields of the error message. |
jjg@73 | 100 | */ |
jjg@73 | 101 | public void error(int pos, String key, Object ... args) { |
jjg@73 | 102 | report(diags.error(source, wrap(pos), key, args)); |
jjg@73 | 103 | } |
jjg@73 | 104 | |
jjg@73 | 105 | /** Report a warning, unless suppressed by the -nowarn option or the |
jjg@73 | 106 | * maximum number of warnings has been reached. |
jjg@73 | 107 | * @param pos The source position at which to report the warning. |
jjg@73 | 108 | * @param key The key for the localized warning message. |
jjg@73 | 109 | * @param args Fields of the warning message. |
jjg@73 | 110 | */ |
jjg@73 | 111 | public void warning(String key, Object ... args) { |
jjg@73 | 112 | report(diags.warning(source, null, key, args)); |
jjg@73 | 113 | } |
jjg@73 | 114 | |
jjg@73 | 115 | /** Report a warning, unless suppressed by the -nowarn option or the |
jjg@73 | 116 | * maximum number of warnings has been reached. |
jjg@73 | 117 | * @param pos The source position at which to report the warning. |
jjg@73 | 118 | * @param key The key for the localized warning message. |
jjg@73 | 119 | * @param args Fields of the warning message. |
jjg@73 | 120 | */ |
jjg@73 | 121 | public void warning(DiagnosticPosition pos, String key, Object ... args) { |
jjg@73 | 122 | report(diags.warning(source, pos, key, args)); |
jjg@73 | 123 | } |
jjg@73 | 124 | |
jjg@73 | 125 | /** Report a warning, unless suppressed by the -nowarn option or the |
jjg@73 | 126 | * maximum number of warnings has been reached. |
jjg@73 | 127 | * @param pos The source position at which to report the warning. |
jjg@73 | 128 | * @param key The key for the localized warning message. |
jjg@73 | 129 | * @param args Fields of the warning message. |
jjg@73 | 130 | */ |
jjg@73 | 131 | public void warning(int pos, String key, Object ... args) { |
jjg@73 | 132 | report(diags.warning(source, wrap(pos), key, args)); |
jjg@73 | 133 | } |
jjg@73 | 134 | |
jjg@73 | 135 | /** Report a warning. |
jjg@73 | 136 | * @param pos The source position at which to report the warning. |
jjg@73 | 137 | * @param key The key for the localized warning message. |
jjg@73 | 138 | * @param args Fields of the warning message. |
jjg@73 | 139 | */ |
jjg@73 | 140 | public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { |
jjg@73 | 141 | report(diags.mandatoryWarning(source, pos, key, args)); |
jjg@73 | 142 | } |
jjg@73 | 143 | |
jjg@73 | 144 | /** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
jjg@73 | 145 | * @param key The key for the localized notification message. |
jjg@73 | 146 | * @param args Fields of the notint an error or warning message: |
jjg@73 | 147 | */ |
jjg@73 | 148 | public void note(String key, Object ... args) { |
jjg@73 | 149 | report(diags.note(source, null, key, args)); |
jjg@73 | 150 | } |
jjg@73 | 151 | |
jjg@73 | 152 | /** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
jjg@73 | 153 | * @param key The key for the localized notification message. |
jjg@73 | 154 | * @param args Fields of the notification message. |
jjg@73 | 155 | */ |
jjg@73 | 156 | public void note(DiagnosticPosition pos, String key, Object ... args) { |
jjg@73 | 157 | report(diags.note(source, pos, key, args)); |
jjg@73 | 158 | } |
jjg@73 | 159 | |
jjg@73 | 160 | /** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
jjg@73 | 161 | * @param key The key for the localized notification message. |
jjg@73 | 162 | * @param args Fields of the notification message. |
jjg@73 | 163 | */ |
jjg@73 | 164 | public void note(int pos, String key, Object ... args) { |
jjg@73 | 165 | report(diags.note(source, wrap(pos), key, args)); |
jjg@73 | 166 | } |
jjg@73 | 167 | |
jjg@73 | 168 | /** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
jjg@73 | 169 | * @param key The key for the localized notification message. |
jjg@73 | 170 | * @param args Fields of the notification message. |
jjg@73 | 171 | */ |
jjg@73 | 172 | public void note(JavaFileObject file, String key, Object ... args) { |
jjg@73 | 173 | report(diags.note(getSource(file), null, key, args)); |
jjg@73 | 174 | } |
jjg@73 | 175 | |
jjg@73 | 176 | /** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
jjg@73 | 177 | * @param key The key for the localized notification message. |
jjg@73 | 178 | * @param args Fields of the notification message. |
jjg@73 | 179 | */ |
jjg@73 | 180 | public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { |
jjg@73 | 181 | report(diags.mandatoryNote(getSource(file), key, args)); |
jjg@73 | 182 | } |
jjg@73 | 183 | |
jjg@73 | 184 | protected abstract void report(JCDiagnostic diagnostic); |
jjg@73 | 185 | |
jjg@73 | 186 | protected abstract void directError(String key, Object... args); |
jjg@73 | 187 | |
jjg@73 | 188 | private DiagnosticPosition wrap(int pos) { |
jjg@73 | 189 | return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); |
jjg@73 | 190 | } |
jjg@73 | 191 | |
jjg@73 | 192 | /** Factory for diagnostics |
jjg@73 | 193 | */ |
jjg@73 | 194 | protected JCDiagnostic.Factory diags; |
jjg@73 | 195 | |
jjg@73 | 196 | /** The file that's currently being translated. |
jjg@73 | 197 | */ |
jjg@73 | 198 | protected DiagnosticSource source; |
jjg@73 | 199 | |
jjg@73 | 200 | /** A cache of lightweight DiagnosticSource objects. |
jjg@73 | 201 | */ |
jjg@73 | 202 | protected Map<JavaFileObject, DiagnosticSource> sourceMap; |
jjg@73 | 203 | } |