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