langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java
author jjg
Wed, 10 Aug 2016 15:47:46 -0700
changeset 40308 274367a99f98
parent 40232 4995ab1a4558
child 42498 ceaf5bafa1b5
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers 8160489: Multiple -Xpatch lines ignored by javac 8156998: javac should support new option -XinheritRuntimeEnvironment Reviewed-by: jlahoda, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
     2
 * Copyright (c) 2005, 2016, 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.Locale;
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27379
diff changeset
    37
import java.util.Objects;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    39
10
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;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    45
import com.sun.tools.javac.main.Arguments;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    46
import com.sun.tools.javac.main.Option;
29780
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    47
import com.sun.tools.javac.file.BaseFileManager;
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
    48
import com.sun.tools.javac.file.CacheFSInfo;
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
    49
import com.sun.tools.javac.jvm.Target;
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
    50
import com.sun.tools.javac.util.ClientCodeException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.util.Context;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    52
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    53
import com.sun.tools.javac.util.DefinedBy.Api;
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
    54
import com.sun.tools.javac.util.List;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.javac.util.Log;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    56
import com.sun.tools.javac.util.PropagatedException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * TODO: describe com.sun.tools.javac.api.Tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    61
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * @author Peter von der Ah\u00e9
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
public final class JavacTool implements JavaCompiler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /**
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    70
     * Constructor used by service provider mechanism.  The recommended way to
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    71
     * obtain an instance of this class is by using {@link #create} or the
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    72
     * service provider mechanism.
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 11314
diff changeset
    73
     * @see javax.tools.JavaCompiler
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @see javax.tools.ToolProvider
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * @see #create
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    public JavacTool() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Static factory method for creating new instances of this tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * @return new instance of this tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public static JavacTool create() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        return new JavacTool();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    88
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    public JavacFileManager getStandardFileManager(
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        Locale locale,
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        Charset charset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        Context context = new Context();
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    94
        context.put(Locale.class, locale);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        if (diagnosticListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            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
    97
        PrintWriter pw = (charset == null)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    98
                ? new PrintWriter(System.err, true)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents: 8844
diff changeset
    99
                : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37394
diff changeset
   100
        context.put(Log.errKey, pw);
37390
bf1552d6bc16 8150641: Repeated compilation with a long classpath significantly slower on JDK 9
jlahoda
parents: 36526
diff changeset
   101
        CacheFSInfo.preRegister(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return new JavacFileManager(context, true, charset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   105
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    public JavacTask getTask(Writer out,
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                             JavaFileManager fileManager,
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                             DiagnosticListener<? super JavaFileObject> diagnosticListener,
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                             Iterable<String> options,
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                             Iterable<String> classes,
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   111
                             Iterable<? extends JavaFileObject> compilationUnits) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   112
        Context context = new Context();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   113
        return getTask(out, fileManager, diagnosticListener,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   114
                options, classes, compilationUnits,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   115
                context);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   116
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   117
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   118
    /* Internal version of getTask, allowing context to be provided. */
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   119
    public JavacTask getTask(Writer out,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   120
                             JavaFileManager fileManager,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   121
                             DiagnosticListener<? super JavaFileObject> diagnosticListener,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   122
                             Iterable<String> options,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   123
                             Iterable<String> classes,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   124
                             Iterable<? extends JavaFileObject> compilationUnits,
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   125
                             Context context)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   127
        try {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   128
            ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   130
            if (options != null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   131
                for (String option : options)
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27379
diff changeset
   132
                    Objects.requireNonNull(option);
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   133
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   134
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   135
            if (classes != null) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   136
                for (String cls : classes) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   137
                    int sep = cls.indexOf('/'); // implicit null check
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   138
                    if (sep > 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   139
                        String mod = cls.substring(0, sep);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   140
                        if (!SourceVersion.isName(mod))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   141
                            throw new IllegalArgumentException("Not a valid module name: " + mod);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   142
                        cls = cls.substring(sep + 1);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   143
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   144
                    if (!SourceVersion.isName(cls))
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   145
                        throw new IllegalArgumentException("Not a valid class name: " + cls);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 29780
diff changeset
   146
                }
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   147
            }
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   148
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   149
            if (compilationUnits != null) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   150
                compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   151
                for (JavaFileObject cu : compilationUnits) {
20604
13bdd49ddaf0 8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents: 14957
diff changeset
   152
                    if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
13bdd49ddaf0 8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents: 14957
diff changeset
   153
                        String kindMsg = "Compilation unit is not of SOURCE kind: "
13bdd49ddaf0 8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents: 14957
diff changeset
   154
                                + "\"" + cu.getName() + "\"";
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   155
                        throw new IllegalArgumentException(kindMsg);
20604
13bdd49ddaf0 8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
ksrini
parents: 14957
diff changeset
   156
                    }
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   157
                }
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   158
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   160
            if (diagnosticListener != null)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   161
                context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   162
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   163
            if (out == null)
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37394
diff changeset
   164
                context.put(Log.errKey, new PrintWriter(System.err, true));
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   165
            else
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37394
diff changeset
   166
                context.put(Log.errKey, new PrintWriter(out, true));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   168
            if (fileManager == null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   169
                fileManager = getStandardFileManager(diagnosticListener, null, null);
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   170
                if (fileManager instanceof BaseFileManager) {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   171
                    ((BaseFileManager) fileManager).autoClose = true;
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   172
                }
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   173
            }
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   174
            fileManager = ccw.wrap(fileManager);
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   175
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   176
            context.put(JavaFileManager.class, fileManager);
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   177
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   178
            Arguments args = Arguments.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   179
            args.init("javac", options, classes, compilationUnits);
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   180
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   181
            // init multi-release jar handling
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   182
            if (fileManager.isSupportedOption(Option.MULTIRELEASE.primaryName) == 1) {
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   183
                Target target = Target.instance(context);
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   184
                List<String> list = List.of(target.multiReleaseValue());
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   185
                fileManager.handleOption(Option.MULTIRELEASE.primaryName, list.iterator());
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   186
            }
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 37390
diff changeset
   187
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   188
            return new JavacTaskImpl(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   189
        } catch (PropagatedException ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   190
            throw ex.getCause();
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   191
        } catch (ClientCodeException ex) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   192
            throw new RuntimeException(ex.getCause());
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 9069
diff changeset
   193
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   196
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        if (err == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            err = System.err;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        for (String argument : arguments)
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27379
diff changeset
   201
            Objects.requireNonNull(argument);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   205
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    public Set<SourceVersion> getSourceVersions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                                                         SourceVersion.latest()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   211
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    public int isSupportedOption(String option) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   213
        Set<Option> recognizedOptions = Option.getJavacToolOptions();
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   214
        for (Option o : recognizedOptions) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   215
            if (o.matches(option)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                return o.hasArg() ? 1 : 0;
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   217
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
}