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

Mon, 14 Mar 2011 11:48:41 -0700

author
jjg
date
Mon, 14 Mar 2011 11:48:41 -0700
changeset 930
cb119107aeea
parent 893
8f0dcb9499db
child 944
83260b3305ac
permissions
-rw-r--r--

7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2005, 2011, 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.File;
    29 import java.io.InputStream;
    30 import java.io.OutputStream;
    31 import java.io.PrintWriter;
    32 import java.io.Writer;
    33 import java.util.ArrayList;
    34 import java.util.Collections;
    35 import java.util.EnumSet;
    36 import java.util.Iterator;
    37 import java.util.List;
    38 import java.util.Locale;
    39 import java.util.Set;
    40 import javax.lang.model.SourceVersion;
    41 import javax.tools.*;
    43 import com.sun.source.util.JavacTask;
    44 import com.sun.tools.javac.file.JavacFileManager;
    45 import com.sun.tools.javac.main.JavacOption.OptionKind;
    46 import com.sun.tools.javac.main.JavacOption;
    47 import com.sun.tools.javac.main.Main;
    48 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
    49 import com.sun.tools.javac.main.RecognizedOptions;
    50 import com.sun.tools.javac.util.Context;
    51 import com.sun.tools.javac.util.Log;
    52 import com.sun.tools.javac.util.JavacMessages;
    53 import com.sun.tools.javac.util.Options;
    54 import com.sun.tools.javac.util.Pair;
    55 import java.nio.charset.Charset;
    57 /**
    58  * TODO: describe com.sun.tools.javac.api.Tool
    59  *
    60  * <p><b>This is NOT part of any supported API.
    61  * If you write code that depends on this, you do so at your own
    62  * risk.  This code and its internal interfaces are subject to change
    63  * or deletion without notice.</b></p>
    64  *
    65  * @author Peter von der Ah\u00e9
    66  */
    67 public final class JavacTool implements JavaCompiler {
    68     private final List<Pair<String,String>> options
    69         = new ArrayList<Pair<String,String>>();
    70     private final Context dummyContext = new Context();
    72     private final PrintWriter silent = new PrintWriter(new OutputStream(){
    73         public void write(int b) {}
    74     });
    76     private final Main sharedCompiler = new Main("javac", silent);
    77     {
    78         sharedCompiler.setOptions(Options.instance(dummyContext));
    79     }
    81     /**
    82      * Constructor used by service provider mechanism.  The correct way to
    83      * obtain an instance of this class is using create or the service provider
    84      * mechanism.
    85      * @see javax.tools.JavaCompilerTool
    86      * @see javax.tools.ToolProvider
    87      * @see #create
    88      */
    89     @Deprecated
    90     public JavacTool() {}
    92     /**
    93      * Static factory method for creating new instances of this tool.
    94      * @return new instance of this tool
    95      */
    96     public static JavacTool create() {
    97         return new JavacTool();
    98     }
   100     private String argsToString(Object... args) {
   101         String newArgs = null;
   102         if (args.length > 0) {
   103             StringBuilder sb = new StringBuilder();
   104             String separator = "";
   105             for (Object arg : args) {
   106                 sb.append(separator).append(arg.toString());
   107                 separator = File.pathSeparator;
   108             }
   109             newArgs = sb.toString();
   110         }
   111         return newArgs;
   112     }
   114     private void setOption1(String name, OptionKind kind, Object... args) {
   115         String arg = argsToString(args);
   116         JavacOption option = sharedCompiler.getOption(name);
   117         if (option == null || !match(kind, option.getKind()))
   118             throw new IllegalArgumentException(name);
   119         if ((args.length != 0) != option.hasArg())
   120             throw new IllegalArgumentException(name);
   121         if (option.hasArg()) {
   122             if (option.process(null, name, arg)) // FIXME
   123                 throw new IllegalArgumentException(name);
   124         } else {
   125             if (option.process(null, name)) // FIXME
   126                 throw new IllegalArgumentException(name);
   127         }
   128         options.add(new Pair<String,String>(name,arg));
   129     }
   131     public void setOption(String name, Object... args) {
   132         setOption1(name, OptionKind.NORMAL, args);
   133     }
   135     public void setExtendedOption(String name, Object... args)  {
   136         setOption1(name, OptionKind.EXTENDED, args);
   137     }
   139     private static boolean match(OptionKind clientKind, OptionKind optionKind) {
   140         return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind));
   141     }
   143     public JavacFileManager getStandardFileManager(
   144         DiagnosticListener<? super JavaFileObject> diagnosticListener,
   145         Locale locale,
   146         Charset charset) {
   147         Context context = new Context();
   148         JavacMessages.instance(context).setCurrentLocale(locale);
   149         if (diagnosticListener != null)
   150             context.put(DiagnosticListener.class, diagnosticListener);
   151         context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
   152         return new JavacFileManager(context, true, charset);
   153     }
   155     public JavacTask getTask(Writer out,
   156                              JavaFileManager fileManager,
   157                              DiagnosticListener<? super JavaFileObject> diagnosticListener,
   158                              Iterable<String> options,
   159                              Iterable<String> classes,
   160                              Iterable<? extends JavaFileObject> compilationUnits)
   161     {
   162         final String kindMsg = "All compilation units must be of SOURCE kind";
   163         if (options != null)
   164             for (String option : options)
   165                 option.getClass(); // null check
   166         if (classes != null) {
   167             for (String cls : classes)
   168                 if (!SourceVersion.isName(cls)) // implicit null check
   169                     throw new IllegalArgumentException("Not a valid class name: " + cls);
   170         }
   171         if (compilationUnits != null) {
   172             for (JavaFileObject cu : compilationUnits) {
   173                 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
   174                     throw new IllegalArgumentException(kindMsg);
   175             }
   176         }
   178         Context context = new Context();
   180         if (diagnosticListener != null)
   181             context.put(DiagnosticListener.class, diagnosticListener);
   183         if (out == null)
   184             context.put(Log.outKey, new PrintWriter(System.err, true));
   185         else
   186             context.put(Log.outKey, new PrintWriter(out, true));
   188         if (fileManager == null)
   189             fileManager = getStandardFileManager(diagnosticListener, null, null);
   190         context.put(JavaFileManager.class, fileManager);
   191         processOptions(context, fileManager, options);
   192         Main compiler = new Main("javacTask", context.get(Log.outKey));
   193         return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
   194     }
   196     private static void processOptions(Context context,
   197                                        JavaFileManager fileManager,
   198                                        Iterable<String> options)
   199     {
   200         if (options == null)
   201             return;
   203         Options optionTable = Options.instance(context);
   205         JavacOption[] recognizedOptions =
   206             RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
   207         Iterator<String> flags = options.iterator();
   208         while (flags.hasNext()) {
   209             String flag = flags.next();
   210             int j;
   211             for (j=0; j<recognizedOptions.length; j++)
   212                 if (recognizedOptions[j].matches(flag))
   213                     break;
   215             if (j == recognizedOptions.length) {
   216                 if (fileManager.handleOption(flag, flags)) {
   217                     continue;
   218                 } else {
   219                     String msg = Main.getLocalizedString("err.invalid.flag", flag);
   220                     throw new IllegalArgumentException(msg);
   221                 }
   222             }
   224             JavacOption option = recognizedOptions[j];
   225             if (option.hasArg()) {
   226                 if (!flags.hasNext()) {
   227                     String msg = Main.getLocalizedString("err.req.arg", flag);
   228                     throw new IllegalArgumentException(msg);
   229                 }
   230                 String operand = flags.next();
   231                 if (option.process(optionTable, flag, operand))
   232                     // should not happen as the GrumpyHelper will throw exceptions
   233                     // in case of errors
   234                     throw new IllegalArgumentException(flag + " " + operand);
   235             } else {
   236                 if (option.process(optionTable, flag))
   237                     // should not happen as the GrumpyHelper will throw exceptions
   238                     // in case of errors
   239                     throw new IllegalArgumentException(flag);
   240             }
   241         }
   242     }
   244     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
   245         if (err == null)
   246             err = System.err;
   247         for (String argument : arguments)
   248             argument.getClass(); // null check
   249         return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
   250     }
   252     public Set<SourceVersion> getSourceVersions() {
   253         return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
   254                                                          SourceVersion.latest()));
   255     }
   257     public int isSupportedOption(String option) {
   258         JavacOption[] recognizedOptions =
   259             RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
   260         for (JavacOption o : recognizedOptions) {
   261             if (o.matches(option))
   262                 return o.hasArg() ? 1 : 0;
   263         }
   264         return -1;
   265     }
   267 }

mercurial