langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Main.java
author ksrini
Wed, 15 Feb 2017 18:07:28 -0800
changeset 43874 d49715456f51
parent 43871 f164f4506389
child 44229 6ebcb2cb9507
permissions -rw-r--r--
8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43871
f164f4506389 8173596: JavaCompiler.CompilationTask should support addModules
jjg
parents: 42260
diff changeset
     2
 * Copyright (c) 1999, 2017, 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: 5319
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: 5319
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: 5319
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5319
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5319
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.main;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    28
import java.io.FileNotFoundException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
    30
import java.io.InputStream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    32
import java.net.URL;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27846
diff changeset
    33
import java.nio.file.NoSuchFileException;
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    34
import java.security.DigestInputStream;
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    35
import java.security.MessageDigest;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    36
import java.security.NoSuchAlgorithmException;
10193
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
    37
import java.util.Set;
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
    38
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    39
import javax.tools.JavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
14960
ea7606a9683e 8004833: Integrate doclint support into javac
jjg
parents: 14954
diff changeset
    41
import com.sun.tools.javac.api.BasicJavacTask;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
    42
import com.sun.tools.javac.file.CacheFSInfo;
29780
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 27852
diff changeset
    43
import com.sun.tools.javac.file.BaseFileManager;
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;
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
    45
import com.sun.tools.javac.jvm.Target;
43874
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
    46
import com.sun.tools.javac.main.CommandLine.UnmatchedQuote;
31506
4e07f827a794 8072480: javac should support compilation for a specific platform version
jlahoda
parents: 30012
diff changeset
    47
import com.sun.tools.javac.platform.PlatformDescription;
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
    48
import com.sun.tools.javac.processing.AnnotationProcessingError;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.util.*;
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    50
import com.sun.tools.javac.util.Log.PrefixKind;
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
    51
import com.sun.tools.javac.util.Log.WriterKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
    53
/** This class provides a command line interface to the javac compiler.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    55
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    56
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
public class Main {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /** The name of the compiler, for use in diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    String ownName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
    66
    /** The writer to use for normal output.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
    67
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
    68
    PrintWriter stdOut;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
    69
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /** The writer to use for diagnostic output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
    72
    PrintWriter stdErr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 10638
diff changeset
    74
    /** The log to use for diagnostic output.
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 10638
diff changeset
    75
     */
15724
3063fb01c8a1 8004182: Add support for profiles in javac
jjg
parents: 14960
diff changeset
    76
    public Log log;
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 10638
diff changeset
    77
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /**
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    79
     * If true, certain errors will cause an exception, such as command line
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    80
     * arg errors, or exceptions in user provided code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     */
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    82
    boolean apiMode;
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    83
43874
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
    84
    private static final String ENV_OPT_NAME = "JDK_JAVAC_OPTIONS";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** Result codes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    88
    public enum Result {
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    89
        OK(0),        // Compilation completed with no errors.
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    90
        ERROR(1),     // Completed but reported errors.
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    91
        CMDERR(2),    // Bad command-line arguments
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    92
        SYSERR(3),    // System error or resource exhaustion.
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    93
        ABNORMAL(4);  // Compiler terminated abnormally
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    94
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    95
        Result(int exitCode) {
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    96
            this.exitCode = exitCode;
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    97
        }
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    98
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
    99
        public boolean isOK() {
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   100
            return (exitCode == 0);
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   101
        }
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   102
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   103
        public final int exitCode;
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   104
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Construct a compiler instance.
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   108
     * @param name the name of this tool
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public Main(String name) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   111
        this.ownName = name;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * Construct a compiler instance.
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   116
     * @param name the name of this tool
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   117
     * @param out a stream to which to write messages
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    public Main(String name, PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        this.ownName = name;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   121
        this.stdOut = this.stdErr = out;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
15724
3063fb01c8a1 8004182: Add support for profiles in javac
jjg
parents: 14960
diff changeset
   123
41448
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   124
    /**
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   125
     * Construct a compiler instance.
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   126
     * @param name the name of this tool
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   127
     * @param out a stream to which to write expected output
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   128
     * @param err a stream to which to write diagnostic output
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   129
     */
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   130
    public Main(String name, PrintWriter out, PrintWriter err) {
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   131
        this.ownName = name;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   132
        this.stdOut = out;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   133
        this.stdErr = err;
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   134
    }
6689bce0cd65 8159855: Create an SPI for tools
jjg
parents: 40308
diff changeset
   135
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /** Report a usage error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    void error(String key, Object... args) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   139
        if (apiMode) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   140
            String msg = log.localize(PrefixKind.JAVAC, key, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            throw new PropagatedException(new IllegalStateException(msg));
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        warning(key, args);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   144
        log.printLines(PrefixKind.JAVAC, "msg.usage", ownName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** Report a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    void warning(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   150
        log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   154
    /**
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   155
     * Programmatic interface for main function.
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   156
     * @param args  the command line parameters
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   157
     * @return the result of the compilation
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   159
    public Result compile(String[] args) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        JavacFileManager.preRegister(context); // can't create it until Log has been set up
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   162
        Result result = compile(args, context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        if (fileManager instanceof JavacFileManager) {
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   164
            try {
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   165
                // A fresh context was created above, so jfm must be a JavacFileManager
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   166
                ((JavacFileManager)fileManager).close();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   167
            } catch (IOException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   168
                bugMessage(ex);
34560
b6a567b677f7 8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents: 31506
diff changeset
   169
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   174
    /**
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   175
     * Internal version of compile, allowing context to be provided.
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   176
     * Note that the context needs to have a file manager set up.
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   177
     * @param argv  the command line parameters
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   178
     * @param context the context
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   179
     * @return the result of the compilation
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     */
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   181
    public Result compile(String[] argv, Context context) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   182
        if (stdOut != null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   183
            context.put(Log.outKey, stdOut);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   184
        }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   185
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   186
        if (stdErr != null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   187
            context.put(Log.errKey, stdErr);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   188
        }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 39599
diff changeset
   189
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 10638
diff changeset
   190
        log = Log.instance(context);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 10638
diff changeset
   191
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   192
        if (argv.length == 0) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   193
            OptionHelper h = new OptionHelper.GrumpyHelper(log) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   194
                @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   195
                public String getOwnName() { return ownName; }
27846
8a7a3a187e24 8058445: Javac throws exception when displaying info
jjg
parents: 27226
diff changeset
   196
                @Override
8a7a3a187e24 8058445: Javac throws exception when displaying info
jjg
parents: 27226
diff changeset
   197
                public void put(String name, String value) { }
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   198
            };
42260
2b4da2bf6660 8169676: boolean result of Option.process is often ignored
jjg
parents: 41448
diff changeset
   199
            try {
2b4da2bf6660 8169676: boolean result of Option.process is often ignored
jjg
parents: 41448
diff changeset
   200
                Option.HELP.process(h, "-help");
2b4da2bf6660 8169676: boolean result of Option.process is often ignored
jjg
parents: 41448
diff changeset
   201
            } catch (Option.InvalidValueException ignore) {
2b4da2bf6660 8169676: boolean result of Option.process is often ignored
jjg
parents: 41448
diff changeset
   202
            }
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   203
            return Result.CMDERR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   204
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
43874
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
   206
        // prefix argv with contents of environment variable and expand @-files
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        try {
43874
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
   208
            argv = CommandLine.parse(ENV_OPT_NAME, argv);
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
   209
        } catch (UnmatchedQuote ex) {
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
   210
            error("err.unmatched.quote", ex.variableName);
d49715456f51 8173308: JAVAC_OPTIONS should be updated to align with JAVA_OPTIONS
ksrini
parents: 43871
diff changeset
   211
            return Result.CMDERR;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27846
diff changeset
   212
        } catch (FileNotFoundException | NoSuchFileException e) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   213
            warning("err.file.not.found", e.getMessage());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   214
            return Result.SYSERR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   215
        } catch (IOException ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   216
            log.printLines(PrefixKind.JAVAC, "msg.io");
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   217
            ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   218
            return Result.SYSERR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   219
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   221
        Arguments args = Arguments.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   222
        args.init(ownName, argv);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   223
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   224
        if (log.nerrors > 0)
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   225
            return Result.CMDERR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   226
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   227
        Options options = Options.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   229
        // init Log
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   230
        boolean forceStdOut = options.isSet("stdout");
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   231
        if (forceStdOut) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   232
            log.flush();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   233
            log.setWriters(new PrintWriter(System.out, true));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   234
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   236
        // init CacheFSInfo
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   237
        // allow System property in following line as a Mustang legacy
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   238
        boolean batchMode = (options.isUnset("nonBatchMode")
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   239
                    && System.getProperty("nonBatchMode") == null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   240
        if (batchMode)
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   241
            CacheFSInfo.preRegister(context);
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 23792
diff changeset
   242
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   243
        boolean ok = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   244
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   245
        // init file manager
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   246
        fileManager = context.get(JavaFileManager.class);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   247
        if (fileManager instanceof BaseFileManager) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   248
            ((BaseFileManager) fileManager).setContext(context); // reinit with options
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   249
            ok &= ((BaseFileManager) fileManager).handleOptions(args.getDeferredFileManagerOptions());
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   250
        }
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   251
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   252
        // handle this here so it works even if no other options given
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   253
        String showClass = options.get("showClass");
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   254
        if (showClass != null) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   255
            if (showClass.equals("showClass")) // no value given for option
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   256
                showClass = "com.sun.tools.javac.Main";
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   257
            showClass(showClass);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   258
        }
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25445
diff changeset
   259
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   260
        ok &= args.validate();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   261
        if (!ok || log.nerrors > 0)
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   262
            return Result.CMDERR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   263
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   264
        if (args.isEmpty())
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   265
            return Result.OK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   267
        // init Dependencies
39599
3c7da4996d8c 8153407: javac, fold debug options
vromero
parents: 38611
diff changeset
   268
        if (options.isSet("debug.completionDeps")) {
30012
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   269
            Dependencies.GraphDependencies.preRegister(context);
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   270
        }
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   271
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   272
        // init plugins
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   273
        Set<List<String>> pluginOpts = args.getPluginOpts();
31506
4e07f827a794 8072480: javac should support compilation for a specific platform version
jlahoda
parents: 30012
diff changeset
   274
        if (!pluginOpts.isEmpty() || context.get(PlatformDescription.class) != null) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   275
            BasicJavacTask t = (BasicJavacTask) BasicJavacTask.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   276
            t.initPlugins(pluginOpts);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   277
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   278
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   279
        // init multi-release jar handling
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   280
        if (fileManager.isSupportedOption(Option.MULTIRELEASE.primaryName) == 1) {
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   281
            Target target = Target.instance(context);
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   282
            List<String> list = List.of(target.multiReleaseValue());
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   283
            fileManager.handleOption(Option.MULTIRELEASE.primaryName, list.iterator());
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   284
        }
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   285
30012
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   286
        // init JavaCompiler
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   287
        JavaCompiler comp = JavaCompiler.instance(context);
660da74452cf 8078261: Initialization of DocLint triggers out of order initialization of Dependencies
alundblad
parents: 29780
diff changeset
   288
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   289
        // init doclint
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   290
        List<String> docLintOpts = args.getDocLintOpts();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   291
        if (!docLintOpts.isEmpty()) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   292
            BasicJavacTask t = (BasicJavacTask) BasicJavacTask.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   293
            t.initDocLint(docLintOpts);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   294
        }
14960
ea7606a9683e 8004833: Integrate doclint support into javac
jjg
parents: 14954
diff changeset
   295
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   296
        if (options.get(Option.XSTDOUT) != null) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   297
            // Stdout reassigned - ask compiler to close it when it is done
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   298
            comp.closeables = comp.closeables.prepend(log.getWriter(WriterKind.NOTICE));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   299
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   300
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   301
        try {
43871
f164f4506389 8173596: JavaCompiler.CompilationTask should support addModules
jjg
parents: 42260
diff changeset
   302
            comp.compile(args.getFileObjects(), args.getClassNames(), null, List.nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   304
            if (log.expectDiagKeys != null) {
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6721
diff changeset
   305
                if (log.expectDiagKeys.isEmpty()) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   306
                    log.printRawLines("all expected diagnostics found");
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   307
                    return Result.OK;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   308
                } else {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   309
                    log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   310
                    return Result.ERROR;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   311
                }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   312
            }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   313
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   314
            return (comp.errorCount() == 0) ? Result.OK : Result.ERROR;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   315
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   316
        } catch (OutOfMemoryError | StackOverflowError ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            resourceMessage(ex);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   318
            return Result.SYSERR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        } catch (FatalError ex) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   320
            feMessage(ex, options);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   321
            return Result.SYSERR;
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   322
        } catch (AnnotationProcessingError ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            apMessage(ex);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   324
            return Result.SYSERR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        } catch (PropagatedException ex) {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   326
            // TODO: what about errors from plugins?   should not simply rethrow the error here
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            throw ex.getCause();
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        } catch (Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            // Nasty.  If we've already reported an error, compensate
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            // for buggy compiler error recovery by swallowing thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            // exceptions.
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   332
            if (comp == null || comp.errorCount() == 0 || options.isSet("dev"))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                bugMessage(ex);
10638
c8e9604cf151 7075721: javac should have public enum for exit codes
jjg
parents: 10193
diff changeset
   334
            return Result.ABNORMAL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        } finally {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   336
            if (comp != null) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   337
                try {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   338
                    comp.close();
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   339
                } catch (ClientCodeException ex) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   340
                    throw new RuntimeException(ex.getCause());
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   341
                }
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   342
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    /** Print a message reporting an internal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    void bugMessage(Throwable ex) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   349
        log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   350
        ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 5857
diff changeset
   353
    /** Print a message reporting a fatal error.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
     */
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   355
    void feMessage(Throwable ex, Options options) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   356
        log.printRawLines(ex.getMessage());
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   357
        if (ex.getCause() != null && options.isSet("dev")) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   358
            ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 5857
diff changeset
   359
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    /** Print a message reporting an input/output error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    void ioMessage(Throwable ex) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   365
        log.printLines(PrefixKind.JAVAC, "msg.io");
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   366
        ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    /** Print a message reporting an out-of-resources error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    void resourceMessage(Throwable ex) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   372
        log.printLines(PrefixKind.JAVAC, "msg.resource");
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   373
        ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    /** Print a message reporting an uncaught exception from an
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     * annotation processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
    void apMessage(AnnotationProcessingError ex) {
14448
91aa6570b59a 7198690: missing compiler message
jjg
parents: 13631
diff changeset
   380
        log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception");
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   381
        ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
14548
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   384
    /** Print a message reporting an uncaught exception from an
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   385
     * annotation processor.
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   386
     */
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   387
    void pluginMessage(Throwable ex) {
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   388
        log.printLines(PrefixKind.JAVAC, "msg.plugin.uncaught.exception");
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   389
        ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   390
    }
aa687b312c97 8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents: 14448
diff changeset
   391
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   392
    /** Display the location and checksum of a class. */
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   393
    void showClass(String className) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   394
        PrintWriter pw = log.getWriter(WriterKind.NOTICE);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   395
        pw.println("javac: show class: " + className);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   396
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   397
        URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   398
        if (url != null) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   399
            pw.println("  " + url);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   400
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   401
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   402
        try (InputStream in = getClass().getResourceAsStream('/' + className.replace('.', '/') + ".class")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   403
            final String algorithm = "MD5";
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   404
            byte[] digest;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   405
            MessageDigest md = MessageDigest.getInstance(algorithm);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   406
            try (DigestInputStream din = new DigestInputStream(in, md)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   407
                byte[] buf = new byte[8192];
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   408
                int n;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   409
                do { n = din.read(buf); } while (n > 0);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   410
                digest = md.digest();
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   411
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   412
            StringBuilder sb = new StringBuilder();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   413
            for (byte b: digest)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   414
                sb.append(String.format("%02x", b));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   415
            pw.println("  " + algorithm + " checksum: " + sb);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   416
        } catch (NoSuchAlgorithmException | IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34560
diff changeset
   417
            pw.println("  cannot compute digest: " + e);
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   418
        }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   419
    }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   420
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   421
    // TODO: update this to JavacFileManager
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
    private JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
    /* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     * Internationalization
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   428
    public static final String javacBundleName =
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
        "com.sun.tools.javac.resources.javac";
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
}