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

Mon, 14 Mar 2011 11:48:41 -0700

author
jjg
date
Mon, 14 Mar 2011 11:48:41 -0700
changeset 930
cb119107aeea
parent 893
8f0dcb9499db
child 944
83260b3305ac
permissions
-rw-r--r--

7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
Reviewed-by: mcimadamore

duke@1 1 /*
jjg@893 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.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 *
jjg@581 60 * <p><b>This is NOT part of any supported API.
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) {
jjg@507 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 public JavacTask getTask(Writer out,
duke@1 156 JavaFileManager fileManager,
duke@1 157 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 158 Iterable<String> options,
duke@1 159 Iterable<String> classes,
duke@1 160 Iterable<? extends JavaFileObject> compilationUnits)
duke@1 161 {
duke@1 162 final String kindMsg = "All compilation units must be of SOURCE kind";
duke@1 163 if (options != null)
duke@1 164 for (String option : options)
duke@1 165 option.getClass(); // null check
duke@1 166 if (classes != null) {
duke@1 167 for (String cls : classes)
duke@1 168 if (!SourceVersion.isName(cls)) // implicit null check
duke@1 169 throw new IllegalArgumentException("Not a valid class name: " + cls);
duke@1 170 }
duke@1 171 if (compilationUnits != null) {
duke@1 172 for (JavaFileObject cu : compilationUnits) {
duke@1 173 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
duke@1 174 throw new IllegalArgumentException(kindMsg);
duke@1 175 }
duke@1 176 }
duke@1 177
duke@1 178 Context context = new Context();
duke@1 179
duke@1 180 if (diagnosticListener != null)
duke@1 181 context.put(DiagnosticListener.class, diagnosticListener);
duke@1 182
duke@1 183 if (out == null)
duke@1 184 context.put(Log.outKey, new PrintWriter(System.err, true));
duke@1 185 else
duke@1 186 context.put(Log.outKey, new PrintWriter(out, true));
duke@1 187
duke@1 188 if (fileManager == null)
duke@1 189 fileManager = getStandardFileManager(diagnosticListener, null, null);
duke@1 190 context.put(JavaFileManager.class, fileManager);
duke@1 191 processOptions(context, fileManager, options);
duke@1 192 Main compiler = new Main("javacTask", context.get(Log.outKey));
duke@1 193 return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
duke@1 194 }
duke@1 195
duke@1 196 private static void processOptions(Context context,
duke@1 197 JavaFileManager fileManager,
duke@1 198 Iterable<String> options)
duke@1 199 {
duke@1 200 if (options == null)
duke@1 201 return;
duke@1 202
duke@1 203 Options optionTable = Options.instance(context);
duke@1 204
duke@1 205 JavacOption[] recognizedOptions =
duke@1 206 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
duke@1 207 Iterator<String> flags = options.iterator();
duke@1 208 while (flags.hasNext()) {
duke@1 209 String flag = flags.next();
duke@1 210 int j;
duke@1 211 for (j=0; j<recognizedOptions.length; j++)
duke@1 212 if (recognizedOptions[j].matches(flag))
duke@1 213 break;
duke@1 214
duke@1 215 if (j == recognizedOptions.length) {
duke@1 216 if (fileManager.handleOption(flag, flags)) {
duke@1 217 continue;
duke@1 218 } else {
duke@1 219 String msg = Main.getLocalizedString("err.invalid.flag", flag);
duke@1 220 throw new IllegalArgumentException(msg);
duke@1 221 }
duke@1 222 }
duke@1 223
duke@1 224 JavacOption option = recognizedOptions[j];
duke@1 225 if (option.hasArg()) {
duke@1 226 if (!flags.hasNext()) {
duke@1 227 String msg = Main.getLocalizedString("err.req.arg", flag);
duke@1 228 throw new IllegalArgumentException(msg);
duke@1 229 }
duke@1 230 String operand = flags.next();
duke@1 231 if (option.process(optionTable, flag, operand))
duke@1 232 // should not happen as the GrumpyHelper will throw exceptions
duke@1 233 // in case of errors
duke@1 234 throw new IllegalArgumentException(flag + " " + operand);
duke@1 235 } else {
duke@1 236 if (option.process(optionTable, flag))
duke@1 237 // should not happen as the GrumpyHelper will throw exceptions
duke@1 238 // in case of errors
duke@1 239 throw new IllegalArgumentException(flag);
duke@1 240 }
duke@1 241 }
duke@1 242 }
duke@1 243
duke@1 244 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
duke@1 245 if (err == null)
duke@1 246 err = System.err;
duke@1 247 for (String argument : arguments)
duke@1 248 argument.getClass(); // null check
duke@1 249 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
duke@1 250 }
duke@1 251
duke@1 252 public Set<SourceVersion> getSourceVersions() {
duke@1 253 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
duke@1 254 SourceVersion.latest()));
duke@1 255 }
duke@1 256
duke@1 257 public int isSupportedOption(String option) {
duke@1 258 JavacOption[] recognizedOptions =
duke@1 259 RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
duke@1 260 for (JavacOption o : recognizedOptions) {
duke@1 261 if (o.matches(option))
duke@1 262 return o.hasArg() ? 1 : 0;
duke@1 263 }
duke@1 264 return -1;
duke@1 265 }
duke@1 266
duke@1 267 }

mercurial