duke@1: /* jjg@1358: * Copyright (c) 2005, 2012, 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.InputStream; duke@1: import java.io.OutputStream; jjg@944: import java.io.OutputStreamWriter; duke@1: import java.io.PrintWriter; duke@1: import java.io.Writer; jjg@944: import java.nio.charset.Charset; duke@1: import java.util.Collections; duke@1: import java.util.EnumSet; duke@1: import java.util.Iterator; 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.Main; jjg@1157: import com.sun.tools.javac.main.Option; jjg@1157: import com.sun.tools.javac.main.OptionHelper; jjg@1157: import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; jjg@946: import com.sun.tools.javac.util.ClientCodeException; duke@1: import com.sun.tools.javac.util.Context; duke@1: import com.sun.tools.javac.util.Log; jjg@1136: import com.sun.tools.javac.util.Log.PrefixKind; duke@1: import com.sun.tools.javac.util.Options; 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: /** jjg@1157: * Constructor used by service provider mechanism. The recommended way to jjg@1157: * obtain an instance of this class is by using {@link #create} or the jjg@1157: * service provider mechanism. jjg@1358: * @see javax.tools.JavaCompiler 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: public JavacFileManager getStandardFileManager( duke@1: DiagnosticListener diagnosticListener, duke@1: Locale locale, duke@1: Charset charset) { duke@1: Context context = new Context(); jjg@944: context.put(Locale.class, locale); duke@1: if (diagnosticListener != null) duke@1: context.put(DiagnosticListener.class, diagnosticListener); jjg@944: PrintWriter pw = (charset == null) jjg@944: ? new PrintWriter(System.err, true) jjg@944: : new PrintWriter(new OutputStreamWriter(System.err, charset), true); jjg@944: context.put(Log.outKey, pw); duke@1: return new JavacFileManager(context, true, charset); duke@1: } duke@1: jjg@1136: @Override duke@1: public JavacTask getTask(Writer out, duke@1: JavaFileManager fileManager, duke@1: DiagnosticListener diagnosticListener, duke@1: Iterable options, duke@1: Iterable classes, jjg@1136: Iterable compilationUnits) { jjg@1136: Context context = new Context(); jjg@1136: return getTask(out, fileManager, diagnosticListener, jjg@1136: options, classes, compilationUnits, jjg@1136: context); jjg@1136: } jjg@1136: jjg@1136: public JavacTask getTask(Writer out, jjg@1136: JavaFileManager fileManager, jjg@1136: DiagnosticListener diagnosticListener, jjg@1136: Iterable options, jjg@1136: Iterable classes, jjg@1136: Iterable compilationUnits, jjg@1136: Context context) duke@1: { jjg@946: try { jjg@946: ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); jjg@946: jjg@946: final String kindMsg = "All compilation units must be of SOURCE kind"; jjg@946: if (options != null) jjg@946: for (String option : options) jjg@946: option.getClass(); // null check jjg@946: if (classes != null) { jjg@946: for (String cls : classes) jjg@946: if (!SourceVersion.isName(cls)) // implicit null check jjg@946: throw new IllegalArgumentException("Not a valid class name: " + cls); jjg@946: } jjg@946: if (compilationUnits != null) { jjg@946: compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check jjg@946: for (JavaFileObject cu : compilationUnits) { jjg@946: if (cu.getKind() != JavaFileObject.Kind.SOURCE) jjg@946: throw new IllegalArgumentException(kindMsg); jjg@946: } jjg@946: } jjg@946: jjg@946: if (diagnosticListener != null) jjg@946: context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); jjg@946: jjg@946: if (out == null) jjg@946: context.put(Log.outKey, new PrintWriter(System.err, true)); jjg@946: else jjg@946: context.put(Log.outKey, new PrintWriter(out, true)); jjg@946: jjg@946: if (fileManager == null) jjg@946: fileManager = getStandardFileManager(diagnosticListener, null, null); jjg@946: fileManager = ccw.wrap(fileManager); jjg@1157: jjg@946: context.put(JavaFileManager.class, fileManager); jjg@1157: jjg@946: processOptions(context, fileManager, options); jjg@946: Main compiler = new Main("javacTask", context.get(Log.outKey)); jjg@946: return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); jjg@946: } catch (ClientCodeException ex) { jjg@946: throw new RuntimeException(ex.getCause()); duke@1: } duke@1: } duke@1: ohrstrom@1460: public 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: jjg@1157: final Options optionTable = Options.instance(context); jjg@1136: Log log = Log.instance(context); duke@1: jjg@1157: Option[] recognizedOptions = jjg@1157: Option.getJavacToolOptions().toArray(new Option[0]); jjg@1157: OptionHelper optionHelper = new GrumpyHelper(log) { jjg@1157: @Override jjg@1157: public String get(Option option) { jjg@1157: return optionTable.get(option.getText()); jjg@1157: } jjg@1157: jjg@1157: @Override jjg@1157: public void put(String name, String value) { jjg@1157: optionTable.put(name, value); jjg@1157: } jjg@1157: jjg@1157: @Override jjg@1157: public void remove(String name) { jjg@1157: optionTable.remove(name); jjg@1157: } jjg@1157: }; jjg@1157: 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) { jjg@1157: Set