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