src/share/classes/com/sun/tools/javac/api/JavacTool.java

Tue, 04 Mar 2008 15:45:20 +0000

author
mcimadamore
date
Tue, 04 Mar 2008 15:45:20 +0000
changeset 8
38bd6375f37d
parent 1
9a66ca7c79fa
child 50
b9bcea8bbe24
permissions
-rw-r--r--

6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
Summary: interplay between cyclic inheritance and tvar bounds hangs javac
Reviewed-by: jjg

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 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.Arrays;
duke@1 35 import java.util.Collections;
duke@1 36 import java.util.EnumSet;
duke@1 37 import java.util.Iterator;
duke@1 38 import java.util.List;
duke@1 39 import java.util.Locale;
duke@1 40 import java.util.Set;
duke@1 41 import javax.lang.model.SourceVersion;
duke@1 42 import javax.tools.*;
duke@1 43
duke@1 44 import com.sun.source.util.JavacTask;
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.JavacFileManager;
duke@1 52 import com.sun.tools.javac.util.Log;
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();
duke@1 148 if (diagnosticListener != null)
duke@1 149 context.put(DiagnosticListener.class, diagnosticListener);
duke@1 150 context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
duke@1 151 return new JavacFileManager(context, true, charset);
duke@1 152 }
duke@1 153
duke@1 154 private boolean compilationInProgress = false;
duke@1 155
duke@1 156 /**
duke@1 157 * Register that a compilation is about to start.
duke@1 158 */
duke@1 159 void beginContext(final Context context) {
duke@1 160 if (compilationInProgress)
duke@1 161 throw new IllegalStateException("Compilation in progress");
duke@1 162 compilationInProgress = true;
duke@1 163 final JavaFileManager givenFileManager = context.get(JavaFileManager.class);
duke@1 164 context.put(JavaFileManager.class, (JavaFileManager)null);
duke@1 165 context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
duke@1 166 public JavaFileManager make() {
duke@1 167 if (givenFileManager != null) {
duke@1 168 context.put(JavaFileManager.class, givenFileManager);
duke@1 169 return givenFileManager;
duke@1 170 } else {
duke@1 171 return new JavacFileManager(context, true, null);
duke@1 172 }
duke@1 173 }
duke@1 174 });
duke@1 175 }
duke@1 176
duke@1 177 /**
duke@1 178 * Register that a compilation is completed.
duke@1 179 */
duke@1 180 void endContext() {
duke@1 181 compilationInProgress = false;
duke@1 182 }
duke@1 183
duke@1 184 public JavacTask getTask(Writer out,
duke@1 185 JavaFileManager fileManager,
duke@1 186 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 187 Iterable<String> options,
duke@1 188 Iterable<String> classes,
duke@1 189 Iterable<? extends JavaFileObject> compilationUnits)
duke@1 190 {
duke@1 191 final String kindMsg = "All compilation units must be of SOURCE kind";
duke@1 192 if (options != null)
duke@1 193 for (String option : options)
duke@1 194 option.getClass(); // null check
duke@1 195 if (classes != null) {
duke@1 196 for (String cls : classes)
duke@1 197 if (!SourceVersion.isName(cls)) // implicit null check
duke@1 198 throw new IllegalArgumentException("Not a valid class name: " + cls);
duke@1 199 }
duke@1 200 if (compilationUnits != null) {
duke@1 201 for (JavaFileObject cu : compilationUnits) {
duke@1 202 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
duke@1 203 throw new IllegalArgumentException(kindMsg);
duke@1 204 }
duke@1 205 }
duke@1 206
duke@1 207 Context context = new Context();
duke@1 208
duke@1 209 if (diagnosticListener != null)
duke@1 210 context.put(DiagnosticListener.class, diagnosticListener);
duke@1 211
duke@1 212 if (out == null)
duke@1 213 context.put(Log.outKey, new PrintWriter(System.err, true));
duke@1 214 else
duke@1 215 context.put(Log.outKey, new PrintWriter(out, true));
duke@1 216
duke@1 217 if (fileManager == null)
duke@1 218 fileManager = getStandardFileManager(diagnosticListener, null, null);
duke@1 219 context.put(JavaFileManager.class, fileManager);
duke@1 220 processOptions(context, fileManager, options);
duke@1 221 Main compiler = new Main("javacTask", context.get(Log.outKey));
duke@1 222 return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
duke@1 223 }
duke@1 224
duke@1 225 private static void processOptions(Context context,
duke@1 226 JavaFileManager fileManager,
duke@1 227 Iterable<String> options)
duke@1 228 {
duke@1 229 if (options == null)
duke@1 230 return;
duke@1 231
duke@1 232 Options optionTable = Options.instance(context);
duke@1 233
duke@1 234 JavacOption[] recognizedOptions =
duke@1 235 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
duke@1 236 Iterator<String> flags = options.iterator();
duke@1 237 while (flags.hasNext()) {
duke@1 238 String flag = flags.next();
duke@1 239 int j;
duke@1 240 for (j=0; j<recognizedOptions.length; j++)
duke@1 241 if (recognizedOptions[j].matches(flag))
duke@1 242 break;
duke@1 243
duke@1 244 if (j == recognizedOptions.length) {
duke@1 245 if (fileManager.handleOption(flag, flags)) {
duke@1 246 continue;
duke@1 247 } else {
duke@1 248 String msg = Main.getLocalizedString("err.invalid.flag", flag);
duke@1 249 throw new IllegalArgumentException(msg);
duke@1 250 }
duke@1 251 }
duke@1 252
duke@1 253 JavacOption option = recognizedOptions[j];
duke@1 254 if (option.hasArg()) {
duke@1 255 if (!flags.hasNext()) {
duke@1 256 String msg = Main.getLocalizedString("err.req.arg", flag);
duke@1 257 throw new IllegalArgumentException(msg);
duke@1 258 }
duke@1 259 String operand = flags.next();
duke@1 260 if (option.process(optionTable, flag, operand))
duke@1 261 // should not happen as the GrumpyHelper will throw exceptions
duke@1 262 // in case of errors
duke@1 263 throw new IllegalArgumentException(flag + " " + operand);
duke@1 264 } else {
duke@1 265 if (option.process(optionTable, flag))
duke@1 266 // should not happen as the GrumpyHelper will throw exceptions
duke@1 267 // in case of errors
duke@1 268 throw new IllegalArgumentException(flag);
duke@1 269 }
duke@1 270 }
duke@1 271 }
duke@1 272
duke@1 273 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
duke@1 274 if (err == null)
duke@1 275 err = System.err;
duke@1 276 for (String argument : arguments)
duke@1 277 argument.getClass(); // null check
duke@1 278 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
duke@1 279 }
duke@1 280
duke@1 281 public Set<SourceVersion> getSourceVersions() {
duke@1 282 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
duke@1 283 SourceVersion.latest()));
duke@1 284 }
duke@1 285
duke@1 286 public int isSupportedOption(String option) {
duke@1 287 JavacOption[] recognizedOptions =
duke@1 288 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
duke@1 289 for (JavacOption o : recognizedOptions) {
duke@1 290 if (o.matches(option))
duke@1 291 return o.hasArg() ? 1 : 0;
duke@1 292 }
duke@1 293 return -1;
duke@1 294 }
duke@1 295
duke@1 296 }

mercurial