langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 7681 1f0819a3341f
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5004
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Locale;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.source.util.JavacTask;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    44
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.main.JavacOption.OptionKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.main.JavacOption;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.main.Main;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.main.RecognizedOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.util.Log;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 735
diff changeset
    52
import com.sun.tools.javac.util.JavacMessages;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import java.nio.charset.Charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * TODO: describe com.sun.tools.javac.api.Tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    60
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * @author Peter von der Ah\u00e9
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
public final class JavacTool implements JavaCompiler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private final List<Pair<String,String>> options
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        = new ArrayList<Pair<String,String>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    private final Context dummyContext = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private final PrintWriter silent = new PrintWriter(new OutputStream(){
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        public void write(int b) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    });
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private final Main sharedCompiler = new Main("javac", silent);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        sharedCompiler.setOptions(Options.instance(dummyContext));
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Constructor used by service provider mechanism.  The correct way to
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * obtain an instance of this class is using create or the service provider
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * mechanism.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * @see javax.tools.JavaCompilerTool
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * @see javax.tools.ToolProvider
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * @see #create
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    public JavacTool() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * Static factory method for creating new instances of this tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * @return new instance of this tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public static JavacTool create() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        return new JavacTool();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    private String argsToString(Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        String newArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        if (args.length > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            StringBuilder sb = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            String separator = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            for (Object arg : args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                sb.append(separator).append(arg.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                separator = File.pathSeparator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            newArgs = sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        return newArgs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private void setOption1(String name, OptionKind kind, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        String arg = argsToString(args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        JavacOption option = sharedCompiler.getOption(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        if (option == null || !match(kind, option.getKind()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            throw new IllegalArgumentException(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        if ((args.length != 0) != option.hasArg())
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            if (option.process(null, name, arg)) // FIXME
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                throw new IllegalArgumentException(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (option.process(null, name)) // FIXME
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                throw new IllegalArgumentException(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        options.add(new Pair<String,String>(name,arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    public void setOption(String name, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        setOption1(name, OptionKind.NORMAL, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    public void setExtendedOption(String name, Object... args)  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        setOption1(name, OptionKind.EXTENDED, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    private static boolean match(OptionKind clientKind, OptionKind optionKind) {
5004
6c2694dc2da0 6929544: langtools source code uses statics qualified by instance variables
jjg
parents: 1471
diff changeset
   140
        return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public JavacFileManager getStandardFileManager(
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        Locale locale,
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        Charset charset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        Context context = new Context();
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 735
diff changeset
   148
        JavacMessages.instance(context).setCurrentLocale(locale);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        if (diagnosticListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            context.put(DiagnosticListener.class, diagnosticListener);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        return new JavacFileManager(context, true, charset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    private boolean compilationInProgress = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * Register that a compilation is about to start.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    void beginContext(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        if (compilationInProgress)
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            throw new IllegalStateException("Compilation in progress");
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        compilationInProgress = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        final JavaFileManager givenFileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        context.put(JavaFileManager.class, (JavaFileManager)null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            public JavaFileManager make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                if (givenFileManager != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                    context.put(JavaFileManager.class, givenFileManager);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                    return givenFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                    return new JavacFileManager(context, true, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * Register that a compilation is completed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    void endContext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        compilationInProgress = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    public JavacTask getTask(Writer out,
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                             JavaFileManager fileManager,
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                             DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                             Iterable<String> options,
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                             Iterable<String> classes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                             Iterable<? extends JavaFileObject> compilationUnits)
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        final String kindMsg = "All compilation units must be of SOURCE kind";
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        if (options != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            for (String option : options)
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                option.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        if (classes != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            for (String cls : classes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                if (!SourceVersion.isName(cls)) // implicit null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                    throw new IllegalArgumentException("Not a valid class name: " + cls);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        if (compilationUnits != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            for (JavaFileObject cu : compilationUnits) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    throw new IllegalArgumentException(kindMsg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        if (diagnosticListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            context.put(DiagnosticListener.class, diagnosticListener);
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        if (out == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            context.put(Log.outKey, new PrintWriter(System.err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            context.put(Log.outKey, new PrintWriter(out, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        if (fileManager == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            fileManager = getStandardFileManager(diagnosticListener, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        context.put(JavaFileManager.class, fileManager);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        processOptions(context, fileManager, options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        Main compiler = new Main("javacTask", context.get(Log.outKey));
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    private static void processOptions(Context context,
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                                       JavaFileManager fileManager,
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                                       Iterable<String> options)
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        Options optionTable = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        JavacOption[] recognizedOptions =
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        Iterator<String> flags = options.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        while (flags.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            String flag = flags.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            int j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            for (j=0; j<recognizedOptions.length; j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                if (recognizedOptions[j].matches(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            if (j == recognizedOptions.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                if (fileManager.handleOption(flag, flags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                    continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                    String msg = Main.getLocalizedString("err.invalid.flag", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                    throw new IllegalArgumentException(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            JavacOption option = recognizedOptions[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                if (!flags.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                    String msg = Main.getLocalizedString("err.req.arg", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                    throw new IllegalArgumentException(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                String operand = flags.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                if (option.process(optionTable, flag, operand))
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    // should not happen as the GrumpyHelper will throw exceptions
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                    // in case of errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    throw new IllegalArgumentException(flag + " " + operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                if (option.process(optionTable, flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                    // should not happen as the GrumpyHelper will throw exceptions
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    // in case of errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    throw new IllegalArgumentException(flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        if (err == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            err = System.err;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        for (String argument : arguments)
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            argument.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    public Set<SourceVersion> getSourceVersions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                                                         SourceVersion.latest()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    public int isSupportedOption(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        JavacOption[] recognizedOptions =
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        for (JavacOption o : recognizedOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            if (o.matches(option))
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                return o.hasArg() ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
}