0N/A/*
2362N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.api;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.io.Writer;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.EnumSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Set;
0N/Aimport javax.lang.model.SourceVersion;
0N/Aimport javax.tools.*;
0N/A
0N/Aimport com.sun.source.util.JavacTask;
0N/Aimport com.sun.tools.javac.file.JavacFileManager;
0N/Aimport com.sun.tools.javac.main.JavacOption.OptionKind;
0N/Aimport com.sun.tools.javac.main.JavacOption;
0N/Aimport com.sun.tools.javac.main.Main;
0N/Aimport com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
0N/Aimport com.sun.tools.javac.main.RecognizedOptions;
0N/Aimport com.sun.tools.javac.util.ClientCodeException;
0N/Aimport com.sun.tools.javac.util.Context;
0N/Aimport com.sun.tools.javac.util.Log;
0N/Aimport com.sun.tools.javac.util.Options;
0N/Aimport com.sun.tools.javac.util.Pair;
0N/A
0N/A/**
0N/A * TODO: describe com.sun.tools.javac.api.Tool
0N/A *
0N/A * <p><b>This is NOT part of any supported API.
0N/A * If you write code that depends on this, you do so at your own
0N/A * risk. This code and its internal interfaces are subject to change
0N/A * or deletion without notice.</b></p>
0N/A *
0N/A * @author Peter von der Ah\u00e9
0N/A */
0N/Apublic final class JavacTool implements JavaCompiler {
0N/A private final List<Pair<String,String>> options
0N/A = new ArrayList<Pair<String,String>>();
0N/A private final Context dummyContext = new Context();
0N/A
0N/A private final PrintWriter silent = new PrintWriter(new OutputStream(){
0N/A public void write(int b) {}
0N/A });
0N/A
0N/A private final Main sharedCompiler = new Main("javac", silent);
0N/A {
0N/A sharedCompiler.setOptions(Options.instance(dummyContext));
0N/A }
0N/A
0N/A /**
0N/A * Constructor used by service provider mechanism. The correct way to
0N/A * obtain an instance of this class is using create or the service provider
0N/A * mechanism.
0N/A * @see javax.tools.JavaCompilerTool
0N/A * @see javax.tools.ToolProvider
0N/A * @see #create
0N/A */
0N/A @Deprecated
0N/A public JavacTool() {}
0N/A
0N/A /**
0N/A * Static factory method for creating new instances of this tool.
0N/A * @return new instance of this tool
0N/A */
0N/A public static JavacTool create() {
0N/A return new JavacTool();
0N/A }
0N/A
0N/A private String argsToString(Object... args) {
0N/A String newArgs = null;
0N/A if (args.length > 0) {
0N/A StringBuilder sb = new StringBuilder();
0N/A String separator = "";
0N/A for (Object arg : args) {
0N/A sb.append(separator).append(arg.toString());
0N/A separator = File.pathSeparator;
0N/A }
0N/A newArgs = sb.toString();
0N/A }
0N/A return newArgs;
0N/A }
0N/A
0N/A private void setOption1(String name, OptionKind kind, Object... args) {
0N/A String arg = argsToString(args);
0N/A JavacOption option = sharedCompiler.getOption(name);
0N/A if (option == null || !match(kind, option.getKind()))
0N/A throw new IllegalArgumentException(name);
0N/A if ((args.length != 0) != option.hasArg())
0N/A throw new IllegalArgumentException(name);
0N/A if (option.hasArg()) {
0N/A if (option.process(null, name, arg)) // FIXME
0N/A throw new IllegalArgumentException(name);
0N/A } else {
0N/A if (option.process(null, name)) // FIXME
0N/A throw new IllegalArgumentException(name);
0N/A }
0N/A options.add(new Pair<String,String>(name,arg));
0N/A }
0N/A
0N/A public void setOption(String name, Object... args) {
0N/A setOption1(name, OptionKind.NORMAL, args);
0N/A }
0N/A
0N/A public void setExtendedOption(String name, Object... args) {
0N/A setOption1(name, OptionKind.EXTENDED, args);
0N/A }
0N/A
0N/A private static boolean match(OptionKind clientKind, OptionKind optionKind) {
0N/A return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind));
0N/A }
0N/A
0N/A public JavacFileManager getStandardFileManager(
0N/A DiagnosticListener<? super JavaFileObject> diagnosticListener,
0N/A Locale locale,
0N/A Charset charset) {
0N/A Context context = new Context();
0N/A context.put(Locale.class, locale);
0N/A if (diagnosticListener != null)
0N/A context.put(DiagnosticListener.class, diagnosticListener);
0N/A PrintWriter pw = (charset == null)
0N/A ? new PrintWriter(System.err, true)
0N/A : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
0N/A context.put(Log.outKey, pw);
0N/A return new JavacFileManager(context, true, charset);
0N/A }
0N/A
0N/A public JavacTask getTask(Writer out,
0N/A JavaFileManager fileManager,
0N/A DiagnosticListener<? super JavaFileObject> diagnosticListener,
0N/A Iterable<String> options,
0N/A Iterable<String> classes,
0N/A Iterable<? extends JavaFileObject> compilationUnits)
0N/A {
0N/A try {
0N/A Context context = new Context();
0N/A ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
0N/A
0N/A final String kindMsg = "All compilation units must be of SOURCE kind";
0N/A if (options != null)
0N/A for (String option : options)
0N/A option.getClass(); // null check
0N/A if (classes != null) {
0N/A for (String cls : classes)
0N/A if (!SourceVersion.isName(cls)) // implicit null check
0N/A throw new IllegalArgumentException("Not a valid class name: " + cls);
0N/A }
0N/A if (compilationUnits != null) {
0N/A compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
0N/A for (JavaFileObject cu : compilationUnits) {
0N/A if (cu.getKind() != JavaFileObject.Kind.SOURCE)
0N/A throw new IllegalArgumentException(kindMsg);
0N/A }
0N/A }
0N/A
0N/A if (diagnosticListener != null)
0N/A context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
0N/A
0N/A if (out == null)
0N/A context.put(Log.outKey, new PrintWriter(System.err, true));
0N/A else
0N/A context.put(Log.outKey, new PrintWriter(out, true));
0N/A
0N/A if (fileManager == null)
0N/A fileManager = getStandardFileManager(diagnosticListener, null, null);
0N/A fileManager = ccw.wrap(fileManager);
0N/A context.put(JavaFileManager.class, fileManager);
0N/A processOptions(context, fileManager, options);
0N/A Main compiler = new Main("javacTask", context.get(Log.outKey));
0N/A return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
0N/A } catch (ClientCodeException ex) {
0N/A throw new RuntimeException(ex.getCause());
0N/A }
0N/A }
0N/A
0N/A private static void processOptions(Context context,
0N/A JavaFileManager fileManager,
0N/A Iterable<String> options)
0N/A {
0N/A if (options == null)
0N/A return;
0N/A
0N/A Options optionTable = Options.instance(context);
0N/A
0N/A JavacOption[] recognizedOptions =
0N/A RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
0N/A Iterator<String> flags = options.iterator();
0N/A while (flags.hasNext()) {
0N/A String flag = flags.next();
0N/A int j;
0N/A for (j=0; j<recognizedOptions.length; j++)
0N/A if (recognizedOptions[j].matches(flag))
0N/A break;
0N/A
0N/A if (j == recognizedOptions.length) {
0N/A if (fileManager.handleOption(flag, flags)) {
0N/A continue;
0N/A } else {
0N/A String msg = Main.getLocalizedString("err.invalid.flag", flag);
0N/A throw new IllegalArgumentException(msg);
0N/A }
0N/A }
0N/A
0N/A JavacOption option = recognizedOptions[j];
0N/A if (option.hasArg()) {
0N/A if (!flags.hasNext()) {
0N/A String msg = Main.getLocalizedString("err.req.arg", flag);
0N/A throw new IllegalArgumentException(msg);
0N/A }
0N/A String operand = flags.next();
0N/A if (option.process(optionTable, flag, operand))
0N/A // should not happen as the GrumpyHelper will throw exceptions
0N/A // in case of errors
0N/A throw new IllegalArgumentException(flag + " " + operand);
0N/A } else {
0N/A if (option.process(optionTable, flag))
0N/A // should not happen as the GrumpyHelper will throw exceptions
0N/A // in case of errors
0N/A throw new IllegalArgumentException(flag);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
0N/A if (err == null)
0N/A err = System.err;
0N/A for (String argument : arguments)
0N/A argument.getClass(); // null check
0N/A return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
0N/A }
0N/A
0N/A public Set<SourceVersion> getSourceVersions() {
0N/A return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
0N/A SourceVersion.latest()));
0N/A }
0N/A
0N/A public int isSupportedOption(String option) {
0N/A JavacOption[] recognizedOptions =
0N/A RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
0N/A for (JavacOption o : recognizedOptions) {
0N/A if (o.matches(option))
0N/A return o.hasArg() ? 1 : 0;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A}
0N/A