Tue, 13 Jan 2009 13:28:20 +0000
6665356: Cast not allowed when both qualifying type and inner class are parameterized
Summary: Fixed parser and cats conversion in order to allow cast between generic inner classes
Reviewed-by: jjg
duke@1 | 1 | /* |
xdono@54 | 2 | * Copyright 2005-2008 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.api; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.File; |
duke@1 | 29 | import java.io.InputStream; |
duke@1 | 30 | import java.io.OutputStream; |
duke@1 | 31 | import java.io.PrintWriter; |
duke@1 | 32 | import java.io.Writer; |
duke@1 | 33 | import java.util.ArrayList; |
duke@1 | 34 | import java.util.Collections; |
duke@1 | 35 | import java.util.EnumSet; |
duke@1 | 36 | import java.util.Iterator; |
duke@1 | 37 | import java.util.List; |
duke@1 | 38 | import java.util.Locale; |
duke@1 | 39 | import java.util.Set; |
duke@1 | 40 | import javax.lang.model.SourceVersion; |
duke@1 | 41 | import javax.tools.*; |
duke@1 | 42 | |
duke@1 | 43 | import com.sun.source.util.JavacTask; |
jjg@50 | 44 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 45 | import com.sun.tools.javac.main.JavacOption.OptionKind; |
duke@1 | 46 | import com.sun.tools.javac.main.JavacOption; |
duke@1 | 47 | import com.sun.tools.javac.main.Main; |
duke@1 | 48 | import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper; |
duke@1 | 49 | import com.sun.tools.javac.main.RecognizedOptions; |
duke@1 | 50 | import com.sun.tools.javac.util.Context; |
duke@1 | 51 | import com.sun.tools.javac.util.Log; |
mcimadamore@136 | 52 | import com.sun.tools.javac.util.JavacMessages; |
duke@1 | 53 | import com.sun.tools.javac.util.Options; |
duke@1 | 54 | import com.sun.tools.javac.util.Pair; |
duke@1 | 55 | import java.nio.charset.Charset; |
duke@1 | 56 | |
duke@1 | 57 | /** |
duke@1 | 58 | * TODO: describe com.sun.tools.javac.api.Tool |
duke@1 | 59 | * |
duke@1 | 60 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
duke@1 | 61 | * If you write code that depends on this, you do so at your own |
duke@1 | 62 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 63 | * or deletion without notice.</b></p> |
duke@1 | 64 | * |
duke@1 | 65 | * @author Peter von der Ah\u00e9 |
duke@1 | 66 | */ |
duke@1 | 67 | public final class JavacTool implements JavaCompiler { |
duke@1 | 68 | private final List<Pair<String,String>> options |
duke@1 | 69 | = new ArrayList<Pair<String,String>>(); |
duke@1 | 70 | private final Context dummyContext = new Context(); |
duke@1 | 71 | |
duke@1 | 72 | private final PrintWriter silent = new PrintWriter(new OutputStream(){ |
duke@1 | 73 | public void write(int b) {} |
duke@1 | 74 | }); |
duke@1 | 75 | |
duke@1 | 76 | private final Main sharedCompiler = new Main("javac", silent); |
duke@1 | 77 | { |
duke@1 | 78 | sharedCompiler.setOptions(Options.instance(dummyContext)); |
duke@1 | 79 | } |
duke@1 | 80 | |
duke@1 | 81 | /** |
duke@1 | 82 | * Constructor used by service provider mechanism. The correct way to |
duke@1 | 83 | * obtain an instance of this class is using create or the service provider |
duke@1 | 84 | * mechanism. |
duke@1 | 85 | * @see javax.tools.JavaCompilerTool |
duke@1 | 86 | * @see javax.tools.ToolProvider |
duke@1 | 87 | * @see #create |
duke@1 | 88 | */ |
duke@1 | 89 | @Deprecated |
duke@1 | 90 | public JavacTool() {} |
duke@1 | 91 | |
duke@1 | 92 | /** |
duke@1 | 93 | * Static factory method for creating new instances of this tool. |
duke@1 | 94 | * @return new instance of this tool |
duke@1 | 95 | */ |
duke@1 | 96 | public static JavacTool create() { |
duke@1 | 97 | return new JavacTool(); |
duke@1 | 98 | } |
duke@1 | 99 | |
duke@1 | 100 | private String argsToString(Object... args) { |
duke@1 | 101 | String newArgs = null; |
duke@1 | 102 | if (args.length > 0) { |
duke@1 | 103 | StringBuilder sb = new StringBuilder(); |
duke@1 | 104 | String separator = ""; |
duke@1 | 105 | for (Object arg : args) { |
duke@1 | 106 | sb.append(separator).append(arg.toString()); |
duke@1 | 107 | separator = File.pathSeparator; |
duke@1 | 108 | } |
duke@1 | 109 | newArgs = sb.toString(); |
duke@1 | 110 | } |
duke@1 | 111 | return newArgs; |
duke@1 | 112 | } |
duke@1 | 113 | |
duke@1 | 114 | private void setOption1(String name, OptionKind kind, Object... args) { |
duke@1 | 115 | String arg = argsToString(args); |
duke@1 | 116 | JavacOption option = sharedCompiler.getOption(name); |
duke@1 | 117 | if (option == null || !match(kind, option.getKind())) |
duke@1 | 118 | throw new IllegalArgumentException(name); |
duke@1 | 119 | if ((args.length != 0) != option.hasArg()) |
duke@1 | 120 | throw new IllegalArgumentException(name); |
duke@1 | 121 | if (option.hasArg()) { |
duke@1 | 122 | if (option.process(null, name, arg)) // FIXME |
duke@1 | 123 | throw new IllegalArgumentException(name); |
duke@1 | 124 | } else { |
duke@1 | 125 | if (option.process(null, name)) // FIXME |
duke@1 | 126 | throw new IllegalArgumentException(name); |
duke@1 | 127 | } |
duke@1 | 128 | options.add(new Pair<String,String>(name,arg)); |
duke@1 | 129 | } |
duke@1 | 130 | |
duke@1 | 131 | public void setOption(String name, Object... args) { |
duke@1 | 132 | setOption1(name, OptionKind.NORMAL, args); |
duke@1 | 133 | } |
duke@1 | 134 | |
duke@1 | 135 | public void setExtendedOption(String name, Object... args) { |
duke@1 | 136 | setOption1(name, OptionKind.EXTENDED, args); |
duke@1 | 137 | } |
duke@1 | 138 | |
duke@1 | 139 | private static boolean match(OptionKind clientKind, OptionKind optionKind) { |
duke@1 | 140 | return (clientKind == (optionKind == OptionKind.HIDDEN ? optionKind.EXTENDED : optionKind)); |
duke@1 | 141 | } |
duke@1 | 142 | |
duke@1 | 143 | public JavacFileManager getStandardFileManager( |
duke@1 | 144 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 145 | Locale locale, |
duke@1 | 146 | Charset charset) { |
duke@1 | 147 | Context context = new Context(); |
mcimadamore@136 | 148 | JavacMessages.instance(context).setCurrentLocale(locale); |
duke@1 | 149 | if (diagnosticListener != null) |
duke@1 | 150 | context.put(DiagnosticListener.class, diagnosticListener); |
duke@1 | 151 | context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME |
duke@1 | 152 | return new JavacFileManager(context, true, charset); |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | private boolean compilationInProgress = false; |
duke@1 | 156 | |
duke@1 | 157 | /** |
duke@1 | 158 | * Register that a compilation is about to start. |
duke@1 | 159 | */ |
duke@1 | 160 | void beginContext(final Context context) { |
duke@1 | 161 | if (compilationInProgress) |
duke@1 | 162 | throw new IllegalStateException("Compilation in progress"); |
duke@1 | 163 | compilationInProgress = true; |
duke@1 | 164 | final JavaFileManager givenFileManager = context.get(JavaFileManager.class); |
duke@1 | 165 | context.put(JavaFileManager.class, (JavaFileManager)null); |
duke@1 | 166 | context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
duke@1 | 167 | public JavaFileManager make() { |
duke@1 | 168 | if (givenFileManager != null) { |
duke@1 | 169 | context.put(JavaFileManager.class, givenFileManager); |
duke@1 | 170 | return givenFileManager; |
duke@1 | 171 | } else { |
duke@1 | 172 | return new JavacFileManager(context, true, null); |
duke@1 | 173 | } |
duke@1 | 174 | } |
duke@1 | 175 | }); |
duke@1 | 176 | } |
duke@1 | 177 | |
duke@1 | 178 | /** |
duke@1 | 179 | * Register that a compilation is completed. |
duke@1 | 180 | */ |
duke@1 | 181 | void endContext() { |
duke@1 | 182 | compilationInProgress = false; |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | public JavacTask getTask(Writer out, |
duke@1 | 186 | JavaFileManager fileManager, |
duke@1 | 187 | DiagnosticListener<? super JavaFileObject> diagnosticListener, |
duke@1 | 188 | Iterable<String> options, |
duke@1 | 189 | Iterable<String> classes, |
duke@1 | 190 | Iterable<? extends JavaFileObject> compilationUnits) |
duke@1 | 191 | { |
duke@1 | 192 | final String kindMsg = "All compilation units must be of SOURCE kind"; |
duke@1 | 193 | if (options != null) |
duke@1 | 194 | for (String option : options) |
duke@1 | 195 | option.getClass(); // null check |
duke@1 | 196 | if (classes != null) { |
duke@1 | 197 | for (String cls : classes) |
duke@1 | 198 | if (!SourceVersion.isName(cls)) // implicit null check |
duke@1 | 199 | throw new IllegalArgumentException("Not a valid class name: " + cls); |
duke@1 | 200 | } |
duke@1 | 201 | if (compilationUnits != null) { |
duke@1 | 202 | for (JavaFileObject cu : compilationUnits) { |
duke@1 | 203 | if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check |
duke@1 | 204 | throw new IllegalArgumentException(kindMsg); |
duke@1 | 205 | } |
duke@1 | 206 | } |
duke@1 | 207 | |
duke@1 | 208 | Context context = new Context(); |
duke@1 | 209 | |
duke@1 | 210 | if (diagnosticListener != null) |
duke@1 | 211 | context.put(DiagnosticListener.class, diagnosticListener); |
duke@1 | 212 | |
duke@1 | 213 | if (out == null) |
duke@1 | 214 | context.put(Log.outKey, new PrintWriter(System.err, true)); |
duke@1 | 215 | else |
duke@1 | 216 | context.put(Log.outKey, new PrintWriter(out, true)); |
duke@1 | 217 | |
duke@1 | 218 | if (fileManager == null) |
duke@1 | 219 | fileManager = getStandardFileManager(diagnosticListener, null, null); |
duke@1 | 220 | context.put(JavaFileManager.class, fileManager); |
duke@1 | 221 | processOptions(context, fileManager, options); |
duke@1 | 222 | Main compiler = new Main("javacTask", context.get(Log.outKey)); |
duke@1 | 223 | return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits); |
duke@1 | 224 | } |
duke@1 | 225 | |
duke@1 | 226 | private static void processOptions(Context context, |
duke@1 | 227 | JavaFileManager fileManager, |
duke@1 | 228 | Iterable<String> options) |
duke@1 | 229 | { |
duke@1 | 230 | if (options == null) |
duke@1 | 231 | return; |
duke@1 | 232 | |
duke@1 | 233 | Options optionTable = Options.instance(context); |
duke@1 | 234 | |
duke@1 | 235 | JavacOption[] recognizedOptions = |
duke@1 | 236 | RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
duke@1 | 237 | Iterator<String> flags = options.iterator(); |
duke@1 | 238 | while (flags.hasNext()) { |
duke@1 | 239 | String flag = flags.next(); |
duke@1 | 240 | int j; |
duke@1 | 241 | for (j=0; j<recognizedOptions.length; j++) |
duke@1 | 242 | if (recognizedOptions[j].matches(flag)) |
duke@1 | 243 | break; |
duke@1 | 244 | |
duke@1 | 245 | if (j == recognizedOptions.length) { |
duke@1 | 246 | if (fileManager.handleOption(flag, flags)) { |
duke@1 | 247 | continue; |
duke@1 | 248 | } else { |
duke@1 | 249 | String msg = Main.getLocalizedString("err.invalid.flag", flag); |
duke@1 | 250 | throw new IllegalArgumentException(msg); |
duke@1 | 251 | } |
duke@1 | 252 | } |
duke@1 | 253 | |
duke@1 | 254 | JavacOption option = recognizedOptions[j]; |
duke@1 | 255 | if (option.hasArg()) { |
duke@1 | 256 | if (!flags.hasNext()) { |
duke@1 | 257 | String msg = Main.getLocalizedString("err.req.arg", flag); |
duke@1 | 258 | throw new IllegalArgumentException(msg); |
duke@1 | 259 | } |
duke@1 | 260 | String operand = flags.next(); |
duke@1 | 261 | if (option.process(optionTable, flag, operand)) |
duke@1 | 262 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 263 | // in case of errors |
duke@1 | 264 | throw new IllegalArgumentException(flag + " " + operand); |
duke@1 | 265 | } else { |
duke@1 | 266 | if (option.process(optionTable, flag)) |
duke@1 | 267 | // should not happen as the GrumpyHelper will throw exceptions |
duke@1 | 268 | // in case of errors |
duke@1 | 269 | throw new IllegalArgumentException(flag); |
duke@1 | 270 | } |
duke@1 | 271 | } |
duke@1 | 272 | } |
duke@1 | 273 | |
duke@1 | 274 | public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
duke@1 | 275 | if (err == null) |
duke@1 | 276 | err = System.err; |
duke@1 | 277 | for (String argument : arguments) |
duke@1 | 278 | argument.getClass(); // null check |
duke@1 | 279 | return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
duke@1 | 280 | } |
duke@1 | 281 | |
duke@1 | 282 | public Set<SourceVersion> getSourceVersions() { |
duke@1 | 283 | return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
duke@1 | 284 | SourceVersion.latest())); |
duke@1 | 285 | } |
duke@1 | 286 | |
duke@1 | 287 | public int isSupportedOption(String option) { |
duke@1 | 288 | JavacOption[] recognizedOptions = |
duke@1 | 289 | RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
duke@1 | 290 | for (JavacOption o : recognizedOptions) { |
duke@1 | 291 | if (o.matches(option)) |
duke@1 | 292 | return o.hasArg() ? 1 : 0; |
duke@1 | 293 | } |
duke@1 | 294 | return -1; |
duke@1 | 295 | } |
duke@1 | 296 | |
duke@1 | 297 | } |