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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2075
82044fe8c7f7
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.api;
aoqi@0 27
aoqi@0 28 import java.io.InputStream;
aoqi@0 29 import java.io.OutputStream;
aoqi@0 30 import java.io.OutputStreamWriter;
aoqi@0 31 import java.io.PrintWriter;
aoqi@0 32 import java.io.Writer;
aoqi@0 33 import java.nio.charset.Charset;
aoqi@0 34 import java.util.Collections;
aoqi@0 35 import java.util.EnumSet;
aoqi@0 36 import java.util.Iterator;
aoqi@0 37 import java.util.Locale;
aoqi@0 38 import java.util.Set;
aoqi@0 39 import javax.lang.model.SourceVersion;
aoqi@0 40 import javax.tools.*;
aoqi@0 41
aoqi@0 42 import com.sun.source.util.JavacTask;
aoqi@0 43 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 44 import com.sun.tools.javac.main.Main;
aoqi@0 45 import com.sun.tools.javac.main.Option;
aoqi@0 46 import com.sun.tools.javac.main.OptionHelper;
aoqi@0 47 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
aoqi@0 48 import com.sun.tools.javac.util.ClientCodeException;
aoqi@0 49 import com.sun.tools.javac.util.Context;
aoqi@0 50 import com.sun.tools.javac.util.Log;
aoqi@0 51 import com.sun.tools.javac.util.Log.PrefixKind;
aoqi@0 52 import com.sun.tools.javac.util.Options;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * TODO: describe com.sun.tools.javac.api.Tool
aoqi@0 56 *
aoqi@0 57 * <p><b>This is NOT part of any supported API.
aoqi@0 58 * If you write code that depends on this, you do so at your own
aoqi@0 59 * risk. This code and its internal interfaces are subject to change
aoqi@0 60 * or deletion without notice.</b></p>
aoqi@0 61 *
aoqi@0 62 * @author Peter von der Ah\u00e9
aoqi@0 63 */
aoqi@0 64 public final class JavacTool implements JavaCompiler {
aoqi@0 65 /**
aoqi@0 66 * Constructor used by service provider mechanism. The recommended way to
aoqi@0 67 * obtain an instance of this class is by using {@link #create} or the
aoqi@0 68 * service provider mechanism.
aoqi@0 69 * @see javax.tools.JavaCompiler
aoqi@0 70 * @see javax.tools.ToolProvider
aoqi@0 71 * @see #create
aoqi@0 72 */
aoqi@0 73 @Deprecated
aoqi@0 74 public JavacTool() {}
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Static factory method for creating new instances of this tool.
aoqi@0 78 * @return new instance of this tool
aoqi@0 79 */
aoqi@0 80 public static JavacTool create() {
aoqi@0 81 return new JavacTool();
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 public JavacFileManager getStandardFileManager(
aoqi@0 85 DiagnosticListener<? super JavaFileObject> diagnosticListener,
aoqi@0 86 Locale locale,
aoqi@0 87 Charset charset) {
aoqi@0 88 Context context = new Context();
aoqi@0 89 context.put(Locale.class, locale);
aoqi@0 90 if (diagnosticListener != null)
aoqi@0 91 context.put(DiagnosticListener.class, diagnosticListener);
aoqi@0 92 PrintWriter pw = (charset == null)
aoqi@0 93 ? new PrintWriter(System.err, true)
aoqi@0 94 : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
aoqi@0 95 context.put(Log.outKey, pw);
aoqi@0 96 return new JavacFileManager(context, true, charset);
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 @Override
aoqi@0 100 public JavacTask getTask(Writer out,
aoqi@0 101 JavaFileManager fileManager,
aoqi@0 102 DiagnosticListener<? super JavaFileObject> diagnosticListener,
aoqi@0 103 Iterable<String> options,
aoqi@0 104 Iterable<String> classes,
aoqi@0 105 Iterable<? extends JavaFileObject> compilationUnits) {
aoqi@0 106 Context context = new Context();
aoqi@0 107 return getTask(out, fileManager, diagnosticListener,
aoqi@0 108 options, classes, compilationUnits,
aoqi@0 109 context);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public JavacTask getTask(Writer out,
aoqi@0 113 JavaFileManager fileManager,
aoqi@0 114 DiagnosticListener<? super JavaFileObject> diagnosticListener,
aoqi@0 115 Iterable<String> options,
aoqi@0 116 Iterable<String> classes,
aoqi@0 117 Iterable<? extends JavaFileObject> compilationUnits,
aoqi@0 118 Context context)
aoqi@0 119 {
aoqi@0 120 try {
aoqi@0 121 ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
aoqi@0 122
aoqi@0 123 if (options != null)
aoqi@0 124 for (String option : options)
aoqi@0 125 option.getClass(); // null check
aoqi@0 126 if (classes != null) {
aoqi@0 127 for (String cls : classes)
aoqi@0 128 if (!SourceVersion.isName(cls)) // implicit null check
aoqi@0 129 throw new IllegalArgumentException("Not a valid class name: " + cls);
aoqi@0 130 }
aoqi@0 131 if (compilationUnits != null) {
aoqi@0 132 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
aoqi@0 133 for (JavaFileObject cu : compilationUnits) {
aoqi@0 134 if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
aoqi@0 135 String kindMsg = "Compilation unit is not of SOURCE kind: "
aoqi@0 136 + "\"" + cu.getName() + "\"";
aoqi@0 137 throw new IllegalArgumentException(kindMsg);
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 if (diagnosticListener != null)
aoqi@0 143 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
aoqi@0 144
aoqi@0 145 if (out == null)
aoqi@0 146 context.put(Log.outKey, new PrintWriter(System.err, true));
aoqi@0 147 else
aoqi@0 148 context.put(Log.outKey, new PrintWriter(out, true));
aoqi@0 149
aoqi@0 150 if (fileManager == null)
aoqi@0 151 fileManager = getStandardFileManager(diagnosticListener, null, null);
aoqi@0 152 fileManager = ccw.wrap(fileManager);
aoqi@0 153
aoqi@0 154 context.put(JavaFileManager.class, fileManager);
aoqi@0 155
aoqi@0 156 processOptions(context, fileManager, options);
aoqi@0 157 Main compiler = new Main("javacTask", context.get(Log.outKey));
aoqi@0 158 return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
aoqi@0 159 } catch (ClientCodeException ex) {
aoqi@0 160 throw new RuntimeException(ex.getCause());
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 public static void processOptions(Context context,
aoqi@0 165 JavaFileManager fileManager,
aoqi@0 166 Iterable<String> options)
aoqi@0 167 {
aoqi@0 168 if (options == null)
aoqi@0 169 return;
aoqi@0 170
aoqi@0 171 final Options optionTable = Options.instance(context);
aoqi@0 172 Log log = Log.instance(context);
aoqi@0 173
aoqi@0 174 Option[] recognizedOptions =
aoqi@0 175 Option.getJavacToolOptions().toArray(new Option[0]);
aoqi@0 176 OptionHelper optionHelper = new GrumpyHelper(log) {
aoqi@0 177 @Override
aoqi@0 178 public String get(Option option) {
aoqi@0 179 return optionTable.get(option.getText());
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 @Override
aoqi@0 183 public void put(String name, String value) {
aoqi@0 184 optionTable.put(name, value);
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 @Override
aoqi@0 188 public void remove(String name) {
aoqi@0 189 optionTable.remove(name);
aoqi@0 190 }
aoqi@0 191 };
aoqi@0 192
aoqi@0 193 Iterator<String> flags = options.iterator();
aoqi@0 194 while (flags.hasNext()) {
aoqi@0 195 String flag = flags.next();
aoqi@0 196 int j;
aoqi@0 197 for (j=0; j<recognizedOptions.length; j++)
aoqi@0 198 if (recognizedOptions[j].matches(flag))
aoqi@0 199 break;
aoqi@0 200
aoqi@0 201 if (j == recognizedOptions.length) {
aoqi@0 202 if (fileManager.handleOption(flag, flags)) {
aoqi@0 203 continue;
aoqi@0 204 } else {
aoqi@0 205 String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag);
aoqi@0 206 throw new IllegalArgumentException(msg);
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 Option option = recognizedOptions[j];
aoqi@0 211 if (option.hasArg()) {
aoqi@0 212 if (!flags.hasNext()) {
aoqi@0 213 String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag);
aoqi@0 214 throw new IllegalArgumentException(msg);
aoqi@0 215 }
aoqi@0 216 String operand = flags.next();
aoqi@0 217 if (option.process(optionHelper, flag, operand))
aoqi@0 218 // should not happen as the GrumpyHelper will throw exceptions
aoqi@0 219 // in case of errors
aoqi@0 220 throw new IllegalArgumentException(flag + " " + operand);
aoqi@0 221 } else {
aoqi@0 222 if (option.process(optionHelper, flag))
aoqi@0 223 // should not happen as the GrumpyHelper will throw exceptions
aoqi@0 224 // in case of errors
aoqi@0 225 throw new IllegalArgumentException(flag);
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 optionTable.notifyListeners();
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
aoqi@0 233 if (err == null)
aoqi@0 234 err = System.err;
aoqi@0 235 for (String argument : arguments)
aoqi@0 236 argument.getClass(); // null check
aoqi@0 237 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 public Set<SourceVersion> getSourceVersions() {
aoqi@0 241 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
aoqi@0 242 SourceVersion.latest()));
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 public int isSupportedOption(String option) {
aoqi@0 246 Set<Option> recognizedOptions = Option.getJavacToolOptions();
aoqi@0 247 for (Option o : recognizedOptions) {
aoqi@0 248 if (o.matches(option))
aoqi@0 249 return o.hasArg() ? 1 : 0;
aoqi@0 250 }
aoqi@0 251 return -1;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 }

mercurial