langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java
author ohrstrom
Tue, 18 Dec 2012 10:23:40 +0100
changeset 14957 ea7808ffcf6d
parent 14259 fb94a1df0d53
child 20604 13bdd49ddaf0
permissions -rw-r--r--
8004657: Add hooks to javac to enable reporting dependency information. Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 11314
diff changeset
     2
 * Copyright (c) 2005, 2012, 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.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    30
import java.io.OutputStreamWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.Writer;
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    33
import java.nio.charset.Charset;
10
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.Locale;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.source.util.JavacTask;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    43
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.main.Main;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    45
import com.sun.tools.javac.main.Option;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    46
import com.sun.tools.javac.main.OptionHelper;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    47
import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
    48
import com.sun.tools.javac.util.ClientCodeException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.util.Log;
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    51
import com.sun.tools.javac.util.Log.PrefixKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * TODO: describe com.sun.tools.javac.api.Tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    57
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * @author Peter von der Ah\u00e9
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
public final class JavacTool implements JavaCompiler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    66
     * Constructor used by service provider mechanism.  The recommended way to
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    67
     * obtain an instance of this class is by using {@link #create} or the
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    68
     * service provider mechanism.
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 11314
diff changeset
    69
     * @see javax.tools.JavaCompiler
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @see javax.tools.ToolProvider
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @see #create
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public JavacTool() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * Static factory method for creating new instances of this tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * @return new instance of this tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public static JavacTool create() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return new JavacTool();
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public JavacFileManager getStandardFileManager(
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        Locale locale,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        Charset charset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        Context context = new Context();
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    89
        context.put(Locale.class, locale);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        if (diagnosticListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            context.put(DiagnosticListener.class, diagnosticListener);
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    92
        PrintWriter pw = (charset == null)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    93
                ? new PrintWriter(System.err, true)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    94
                : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    95
        context.put(Log.outKey, pw);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return new JavacFileManager(context, true, charset);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    99
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    public JavacTask getTask(Writer out,
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                             JavaFileManager fileManager,
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                             DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                             Iterable<String> options,
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                             Iterable<String> classes,
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   105
                             Iterable<? extends JavaFileObject> compilationUnits) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   106
        Context context = new Context();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   107
        return getTask(out, fileManager, diagnosticListener,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   108
                options, classes, compilationUnits,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   109
                context);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   110
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   111
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   112
    public JavacTask getTask(Writer out,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   113
                             JavaFileManager fileManager,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   114
                             DiagnosticListener<? super JavaFileObject> diagnosticListener,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   115
                             Iterable<String> options,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   116
                             Iterable<String> classes,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   117
                             Iterable<? extends JavaFileObject> compilationUnits,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   118
                             Context context)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   120
        try {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   121
            ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   123
            final String kindMsg = "All compilation units must be of SOURCE kind";
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   124
            if (options != null)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   125
                for (String option : options)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   126
                    option.getClass(); // null check
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   127
            if (classes != null) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   128
                for (String cls : classes)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   129
                    if (!SourceVersion.isName(cls)) // implicit null check
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   130
                        throw new IllegalArgumentException("Not a valid class name: " + cls);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   131
            }
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   132
            if (compilationUnits != null) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   133
                compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   134
                for (JavaFileObject cu : compilationUnits) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   135
                    if (cu.getKind() != JavaFileObject.Kind.SOURCE)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   136
                        throw new IllegalArgumentException(kindMsg);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   137
                }
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   138
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   140
            if (diagnosticListener != null)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   141
                context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   142
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   143
            if (out == null)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   144
                context.put(Log.outKey, new PrintWriter(System.err, true));
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   145
            else
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   146
                context.put(Log.outKey, new PrintWriter(out, true));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   148
            if (fileManager == null)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   149
                fileManager = getStandardFileManager(diagnosticListener, null, null);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   150
            fileManager = ccw.wrap(fileManager);
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   151
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   152
            context.put(JavaFileManager.class, fileManager);
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   153
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   154
            processOptions(context, fileManager, options);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   155
            Main compiler = new Main("javacTask", context.get(Log.outKey));
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   156
            return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   157
        } catch (ClientCodeException ex) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   158
            throw new RuntimeException(ex.getCause());
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   159
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
14957
ea7808ffcf6d 8004657: Add hooks to javac to enable reporting dependency information.
ohrstrom
parents: 14259
diff changeset
   162
    public static void processOptions(Context context,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                                       JavaFileManager fileManager,
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                                       Iterable<String> options)
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   169
        final Options optionTable = Options.instance(context);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   170
        Log log = Log.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   172
        Option[] recognizedOptions =
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   173
                Option.getJavacToolOptions().toArray(new Option[0]);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   174
        OptionHelper optionHelper = new GrumpyHelper(log) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   175
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   176
            public String get(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   177
                return optionTable.get(option.getText());
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   178
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   179
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   180
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   181
            public void put(String name, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   182
                optionTable.put(name, value);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   183
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   184
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   185
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   186
            public void remove(String name) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   187
                optionTable.remove(name);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   188
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   189
        };
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   190
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        Iterator<String> flags = options.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        while (flags.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            String flag = flags.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            int j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            for (j=0; j<recognizedOptions.length; j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                if (recognizedOptions[j].matches(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            if (j == recognizedOptions.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                if (fileManager.handleOption(flag, flags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                    continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                } else {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   203
                    String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    throw new IllegalArgumentException(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   208
            Option option = recognizedOptions[j];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                if (!flags.hasNext()) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   211
                    String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                    throw new IllegalArgumentException(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                String operand = flags.next();
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   215
                if (option.process(optionHelper, flag, operand))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                    // should not happen as the GrumpyHelper will throw exceptions
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                    // in case of errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                    throw new IllegalArgumentException(flag + " " + operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            } else {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   220
                if (option.process(optionHelper, flag))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                    // should not happen as the GrumpyHelper will throw exceptions
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    // in case of errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                    throw new IllegalArgumentException(flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        }
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   226
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   227
        optionTable.notifyListeners();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        if (err == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            err = System.err;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        for (String argument : arguments)
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            argument.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    public Set<SourceVersion> getSourceVersions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                                                         SourceVersion.latest()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    public int isSupportedOption(String option) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   244
        Set<Option> recognizedOptions = Option.getJavacToolOptions();
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   245
        for (Option o : recognizedOptions) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            if (o.matches(option))
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                return o.hasArg() ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
}