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

Thu, 04 Aug 2016 23:36:47 -0700

author
asaha
date
Thu, 04 Aug 2016 23:36:47 -0700
changeset 3270
8a30511b2ea4
parent 2075
82044fe8c7f7
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8162511: 8u111 L10n resource file updates
Summary: 8u111 L10n resource file updates
Reviewed-by: coffeys
Contributed-by: li.jiang@oracle.com

duke@1 1 /*
ksrini@2075 2 * Copyright (c) 2005, 2013, 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.InputStream;
duke@1 29 import java.io.OutputStream;
jjg@944 30 import java.io.OutputStreamWriter;
duke@1 31 import java.io.PrintWriter;
duke@1 32 import java.io.Writer;
jjg@944 33 import java.nio.charset.Charset;
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.Locale;
duke@1 38 import java.util.Set;
duke@1 39 import javax.lang.model.SourceVersion;
duke@1 40 import javax.tools.*;
duke@1 41
duke@1 42 import com.sun.source.util.JavacTask;
jjg@50 43 import com.sun.tools.javac.file.JavacFileManager;
duke@1 44 import com.sun.tools.javac.main.Main;
jjg@1157 45 import com.sun.tools.javac.main.Option;
jjg@1157 46 import com.sun.tools.javac.main.OptionHelper;
jjg@1157 47 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
jjg@946 48 import com.sun.tools.javac.util.ClientCodeException;
duke@1 49 import com.sun.tools.javac.util.Context;
duke@1 50 import com.sun.tools.javac.util.Log;
jjg@1136 51 import com.sun.tools.javac.util.Log.PrefixKind;
duke@1 52 import com.sun.tools.javac.util.Options;
duke@1 53
duke@1 54 /**
duke@1 55 * TODO: describe com.sun.tools.javac.api.Tool
duke@1 56 *
jjg@581 57 * <p><b>This is NOT part of any supported API.
duke@1 58 * If you write code that depends on this, you do so at your own
duke@1 59 * risk. This code and its internal interfaces are subject to change
duke@1 60 * or deletion without notice.</b></p>
duke@1 61 *
duke@1 62 * @author Peter von der Ah\u00e9
duke@1 63 */
duke@1 64 public final class JavacTool implements JavaCompiler {
duke@1 65 /**
jjg@1157 66 * Constructor used by service provider mechanism. The recommended way to
jjg@1157 67 * obtain an instance of this class is by using {@link #create} or the
jjg@1157 68 * service provider mechanism.
jjg@1358 69 * @see javax.tools.JavaCompiler
duke@1 70 * @see javax.tools.ToolProvider
duke@1 71 * @see #create
duke@1 72 */
duke@1 73 @Deprecated
duke@1 74 public JavacTool() {}
duke@1 75
duke@1 76 /**
duke@1 77 * Static factory method for creating new instances of this tool.
duke@1 78 * @return new instance of this tool
duke@1 79 */
duke@1 80 public static JavacTool create() {
duke@1 81 return new JavacTool();
duke@1 82 }
duke@1 83
duke@1 84 public JavacFileManager getStandardFileManager(
duke@1 85 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 86 Locale locale,
duke@1 87 Charset charset) {
duke@1 88 Context context = new Context();
jjg@944 89 context.put(Locale.class, locale);
duke@1 90 if (diagnosticListener != null)
duke@1 91 context.put(DiagnosticListener.class, diagnosticListener);
jjg@944 92 PrintWriter pw = (charset == null)
jjg@944 93 ? new PrintWriter(System.err, true)
jjg@944 94 : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
jjg@944 95 context.put(Log.outKey, pw);
duke@1 96 return new JavacFileManager(context, true, charset);
duke@1 97 }
duke@1 98
jjg@1136 99 @Override
duke@1 100 public JavacTask getTask(Writer out,
duke@1 101 JavaFileManager fileManager,
duke@1 102 DiagnosticListener<? super JavaFileObject> diagnosticListener,
duke@1 103 Iterable<String> options,
duke@1 104 Iterable<String> classes,
jjg@1136 105 Iterable<? extends JavaFileObject> compilationUnits) {
jjg@1136 106 Context context = new Context();
jjg@1136 107 return getTask(out, fileManager, diagnosticListener,
jjg@1136 108 options, classes, compilationUnits,
jjg@1136 109 context);
jjg@1136 110 }
jjg@1136 111
jjg@1136 112 public JavacTask getTask(Writer out,
jjg@1136 113 JavaFileManager fileManager,
jjg@1136 114 DiagnosticListener<? super JavaFileObject> diagnosticListener,
jjg@1136 115 Iterable<String> options,
jjg@1136 116 Iterable<String> classes,
jjg@1136 117 Iterable<? extends JavaFileObject> compilationUnits,
jjg@1136 118 Context context)
duke@1 119 {
jjg@946 120 try {
jjg@946 121 ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
jjg@946 122
jjg@946 123 if (options != null)
jjg@946 124 for (String option : options)
jjg@946 125 option.getClass(); // null check
jjg@946 126 if (classes != null) {
jjg@946 127 for (String cls : classes)
jjg@946 128 if (!SourceVersion.isName(cls)) // implicit null check
jjg@946 129 throw new IllegalArgumentException("Not a valid class name: " + cls);
jjg@946 130 }
jjg@946 131 if (compilationUnits != null) {
jjg@946 132 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
jjg@946 133 for (JavaFileObject cu : compilationUnits) {
ksrini@2075 134 if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
ksrini@2075 135 String kindMsg = "Compilation unit is not of SOURCE kind: "
ksrini@2075 136 + "\"" + cu.getName() + "\"";
jjg@946 137 throw new IllegalArgumentException(kindMsg);
ksrini@2075 138 }
jjg@946 139 }
jjg@946 140 }
jjg@946 141
jjg@946 142 if (diagnosticListener != null)
jjg@946 143 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
jjg@946 144
jjg@946 145 if (out == null)
jjg@946 146 context.put(Log.outKey, new PrintWriter(System.err, true));
jjg@946 147 else
jjg@946 148 context.put(Log.outKey, new PrintWriter(out, true));
jjg@946 149
jjg@946 150 if (fileManager == null)
jjg@946 151 fileManager = getStandardFileManager(diagnosticListener, null, null);
jjg@946 152 fileManager = ccw.wrap(fileManager);
jjg@1157 153
jjg@946 154 context.put(JavaFileManager.class, fileManager);
jjg@1157 155
jjg@946 156 processOptions(context, fileManager, options);
jjg@946 157 Main compiler = new Main("javacTask", context.get(Log.outKey));
jjg@946 158 return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
jjg@946 159 } catch (ClientCodeException ex) {
jjg@946 160 throw new RuntimeException(ex.getCause());
duke@1 161 }
duke@1 162 }
duke@1 163
ohrstrom@1460 164 public static void processOptions(Context context,
duke@1 165 JavaFileManager fileManager,
duke@1 166 Iterable<String> options)
duke@1 167 {
duke@1 168 if (options == null)
duke@1 169 return;
duke@1 170
jjg@1157 171 final Options optionTable = Options.instance(context);
jjg@1136 172 Log log = Log.instance(context);
duke@1 173
jjg@1157 174 Option[] recognizedOptions =
jjg@1157 175 Option.getJavacToolOptions().toArray(new Option[0]);
jjg@1157 176 OptionHelper optionHelper = new GrumpyHelper(log) {
jjg@1157 177 @Override
jjg@1157 178 public String get(Option option) {
jjg@1157 179 return optionTable.get(option.getText());
jjg@1157 180 }
jjg@1157 181
jjg@1157 182 @Override
jjg@1157 183 public void put(String name, String value) {
jjg@1157 184 optionTable.put(name, value);
jjg@1157 185 }
jjg@1157 186
jjg@1157 187 @Override
jjg@1157 188 public void remove(String name) {
jjg@1157 189 optionTable.remove(name);
jjg@1157 190 }
jjg@1157 191 };
jjg@1157 192
duke@1 193 Iterator<String> flags = options.iterator();
duke@1 194 while (flags.hasNext()) {
duke@1 195 String flag = flags.next();
duke@1 196 int j;
duke@1 197 for (j=0; j<recognizedOptions.length; j++)
duke@1 198 if (recognizedOptions[j].matches(flag))
duke@1 199 break;
duke@1 200
duke@1 201 if (j == recognizedOptions.length) {
duke@1 202 if (fileManager.handleOption(flag, flags)) {
duke@1 203 continue;
duke@1 204 } else {
jjg@1136 205 String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag);
duke@1 206 throw new IllegalArgumentException(msg);
duke@1 207 }
duke@1 208 }
duke@1 209
jjg@1157 210 Option option = recognizedOptions[j];
duke@1 211 if (option.hasArg()) {
duke@1 212 if (!flags.hasNext()) {
jjg@1136 213 String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag);
duke@1 214 throw new IllegalArgumentException(msg);
duke@1 215 }
duke@1 216 String operand = flags.next();
jjg@1157 217 if (option.process(optionHelper, flag, operand))
duke@1 218 // should not happen as the GrumpyHelper will throw exceptions
duke@1 219 // in case of errors
duke@1 220 throw new IllegalArgumentException(flag + " " + operand);
duke@1 221 } else {
jjg@1157 222 if (option.process(optionHelper, flag))
duke@1 223 // should not happen as the GrumpyHelper will throw exceptions
duke@1 224 // in case of errors
duke@1 225 throw new IllegalArgumentException(flag);
duke@1 226 }
duke@1 227 }
jjg@1135 228
jjg@1135 229 optionTable.notifyListeners();
duke@1 230 }
duke@1 231
duke@1 232 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
duke@1 233 if (err == null)
duke@1 234 err = System.err;
duke@1 235 for (String argument : arguments)
duke@1 236 argument.getClass(); // null check
duke@1 237 return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
duke@1 238 }
duke@1 239
duke@1 240 public Set<SourceVersion> getSourceVersions() {
duke@1 241 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
duke@1 242 SourceVersion.latest()));
duke@1 243 }
duke@1 244
duke@1 245 public int isSupportedOption(String option) {
jjg@1157 246 Set<Option> recognizedOptions = Option.getJavacToolOptions();
jjg@1157 247 for (Option o : recognizedOptions) {
duke@1 248 if (o.matches(option))
duke@1 249 return o.hasArg() ? 1 : 0;
duke@1 250 }
duke@1 251 return -1;
duke@1 252 }
duke@1 253
duke@1 254 }

mercurial