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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1460
92fcf299cd09
child 2075
82044fe8c7f7
permissions
-rw-r--r--

Merge

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

mercurial