langtools/src/share/classes/com/sun/tools/javac/main/Main.java
author jjg
Fri, 25 Mar 2011 07:58:53 -0700
changeset 9071 88cd61b4e5aa
parent 8623 cc57bd7697a2
child 10193 3e1ef5e9f4fd
permissions -rw-r--r--
6437138: JSR 199: Compiler doesn't diagnose crash in user code 6482554: uncaught exception from annotation processor not reported through JavaCompiler.CompilationTask.call Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8623
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.PrintWriter;
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    31
import java.net.URL;
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    32
import java.security.DigestInputStream;
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
    33
import java.security.MessageDigest;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.MissingResourceException;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    35
import javax.tools.JavaFileManager;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    36
import javax.tools.JavaFileObject;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    37
import javax.annotation.processing.Processor;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.code.Source;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
    40
import com.sun.tools.javac.file.CacheFSInfo;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    41
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.jvm.Target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.main.JavacOption.Option;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.main.RecognizedOptions.OptionHelper;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.processing.AnnotationProcessingError;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    47
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
    48
import static com.sun.tools.javac.main.OptionName.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
/** This class provides a commandline interface to the GJC compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    52
 *  <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
    53
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
public class Main {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /** The name of the compiler, for use in diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    String ownName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /** The writer to use for diagnostic output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    PrintWriter out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /**
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    68
     * 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
    69
     * arg errors, or exceptions in user provided code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    71
    boolean apiMode;
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
    72
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** Result codes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    static final int
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        EXIT_OK = 0,        // Compilation completed with no errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        EXIT_ERROR = 1,     // Completed but reported errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        EXIT_CMDERR = 2,    // Bad command-line arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        EXIT_SYSERR = 3,    // System error or resource exhaustion.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        public void setOut(PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            Main.this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        public void error(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            Main.this.error(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        public void printVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            Log.printLines(out, getLocalizedString("version", ownName,  JavaCompiler.version()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        public void printFullVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            Log.printLines(out, getLocalizedString("fullVersion", ownName,  JavaCompiler.fullVersion()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        public void printHelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        public void printXhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            xhelp();
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        public void addFile(File f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            if (!filenames.contains(f))
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                filenames.append(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        public void addClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            classnames.append(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    });
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     * Construct a compiler instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    public Main(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        this(name, new PrintWriter(System.err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * Construct a compiler instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public Main(String name, PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        this.ownName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** A table of all options that's passed to the JavaCompiler constructor.  */
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    private Options options = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /** The list of source files to process
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    public ListBuffer<File> filenames = null; // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    /** List of class files names passed on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public ListBuffer<String> classnames = null; // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /** Print a string that explains usage.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        Log.printLines(out, getLocalizedString("msg.usage.header", ownName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        for (int i=0; i<recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            recognizedOptions[i].help(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /** Print a string that explains usage for X options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    void xhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        for (int i=0; i<recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            recognizedOptions[i].xhelp(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    /** Report a usage error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    void error(String key, Object... args) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   168
        if (apiMode) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            String msg = getLocalizedString(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            throw new PropagatedException(new IllegalStateException(msg));
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        warning(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        Log.printLines(out, getLocalizedString("msg.usage", ownName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    /** Report a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    void warning(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        Log.printLines(out, ownName + ": "
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                       + getLocalizedString(key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    public Option getOption(String flag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        for (Option option : recognizedOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            if (option.matches(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                return option;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    public void setOptions(Options options) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            throw new NullPointerException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        this.options = options;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   197
    public void setAPIMode(boolean apiMode) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   198
        this.apiMode = apiMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /** Process command line arguments: store all command line options
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     *  in `options' table and return all source filenames.
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     *  @param flags    The array of command line arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    public List<File> processArgs(String[] flags) { // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        int ac = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        while (ac < flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            String flag = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            Option option = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            if (flag.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                // quick hack to speed up file processing:
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                // if the option does not begin with '-', there is no need to check
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                // most of the compiler options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                    if (recognizedOptions[j].matches(flag)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                        option = recognizedOptions[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            if (option == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                error("err.invalid.flag", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                if (ac == flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                    error("err.req.arg", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                String operand = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                if (option.process(options, flag, operand))
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                if (option.process(options, flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   246
        if (!checkDirectory(D))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            return null;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   248
        if (!checkDirectory(S))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   251
        String sourceString = options.get(SOURCE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        Source source = (sourceString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            ? Source.lookup(sourceString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            : Source.DEFAULT;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   255
        String targetString = options.get(TARGET);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        Target target = (targetString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            ? Target.lookup(targetString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            : Target.DEFAULT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        // We don't check source/target consistency for CLDC, as J2ME
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        // profiles are not aligned with J2SE targets; moreover, a
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        // single CLDC target may have many profiles.  In addition,
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        // this is needed for the continued functioning of the JSR14
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        // prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        if (Character.isDigit(target.name.charAt(0))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            if (target.compareTo(source.requiredTarget()) < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                if (targetString != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                    if (sourceString == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                        warning("warn.target.default.source.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                                targetString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                                source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        warning("warn.source.target.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                                sourceString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                                source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                } else {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   278
                    target = source.requiredTarget();
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   279
                    options.put("-target", target.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                if (targetString == null && !source.allowGenerics()) {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   283
                    target = Target.JDK1_4;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   284
                    options.put("-target", target.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   288
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   289
        // handle this here so it works even if no other options given
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   290
        String showClass = options.get("showClass");
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   291
        if (showClass != null) {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   292
            if (showClass.equals("showClass")) // no value given for option
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   293
                showClass = "com.sun.tools.javac.Main";
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   294
            showClass(showClass);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   295
        }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   296
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        return filenames.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    // where
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   300
        private boolean checkDirectory(OptionName optName) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            String value = options.get(optName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            if (value == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            File file = new File(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            if (!file.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                error("err.dir.not.found", value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            if (!file.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                error("err.file.not.directory", value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    public int compile(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        JavacFileManager.preRegister(context); // can't create it until Log has been set up
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        int result = compile(args, context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        if (fileManager instanceof JavacFileManager) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            // A fresh context was created above, so jfm must be a JavacFileManager
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            ((JavacFileManager)fileManager).close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    public int compile(String[] args, Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        return compile(args, context, List.<JavaFileObject>nil(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    public int compile(String[] args,
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                       Context context,
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                       List<JavaFileObject> fileObjects,
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                       Iterable<? extends Processor> processors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            options = Options.instance(context); // creates a new one
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        filenames = new ListBuffer<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        classnames = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        JavaCompiler comp = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
         * TODO: Logic below about what is an acceptable command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
         * should be updated to take annotation processing semantics
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
         * into account.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            if (args.length == 0 && fileObjects.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                return EXIT_CMDERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   359
            List<File> files;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            try {
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   361
                files = processArgs(CommandLine.parse(args));
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   362
                if (files == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                    // null signals an error in options, abort
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                    return EXIT_CMDERR;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   365
                } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                    // it is allowed to compile nothing if just asking for help or version info
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   367
                    if (options.isSet(HELP)
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   368
                        || options.isSet(X)
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   369
                        || options.isSet(VERSION)
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   370
                        || options.isSet(FULLVERSION))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                        return EXIT_OK;
8623
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
   372
                    if (JavaCompiler.explicitAnnotationProcessingRequested(options)) {
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
   373
                        error("err.no.source.files.classes");
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
   374
                    } else {
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
   375
                        error("err.no.source.files");
cc57bd7697a2 6986895: compiler gives misleading message for no input files
jjg
parents: 8036
diff changeset
   376
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                    return EXIT_CMDERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            } catch (java.io.FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                Log.printLines(out, ownName + ": " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                               getLocalizedString("err.file.not.found",
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                                                  e.getMessage()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   386
            boolean forceStdOut = options.isSet("stdout");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            if (forceStdOut) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                out.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                out = new PrintWriter(System.out, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            context.put(Log.outKey, out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   394
            // allow System property in following line as a Mustang legacy
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   395
            boolean batchMode = (options.isUnset("nonBatchMode")
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   396
                        && System.getProperty("nonBatchMode") == null);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   397
            if (batchMode)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   398
                CacheFSInfo.preRegister(context);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   399
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            comp = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            if (comp == null) return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   405
            Log log = Log.instance(context);
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   406
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   407
            if (!files.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                // add filenames to fileObjects
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                comp = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                List<JavaFileObject> otherFiles = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                JavacFileManager dfm = (JavacFileManager)fileManager;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   412
                for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                    otherFiles = otherFiles.prepend(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                for (JavaFileObject fo : otherFiles)
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                    fileObjects = fileObjects.prepend(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            comp.compile(fileObjects,
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                         classnames.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                         processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   421
            if (log.expectDiagKeys != null) {
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6721
diff changeset
   422
                if (log.expectDiagKeys.isEmpty()) {
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   423
                    Log.printLines(log.noticeWriter, "all expected diagnostics found");
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   424
                    return EXIT_OK;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   425
                } else {
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   426
                    Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys);
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   427
                    return EXIT_ERROR;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   428
                }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   429
            }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   430
1996
c855318a4b03 6595666: fix -Werror
jjg
parents: 1866
diff changeset
   431
            if (comp.errorCount() != 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                return EXIT_ERROR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            ioMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        } catch (OutOfMemoryError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        } catch (StackOverflowError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        } catch (FatalError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
            feMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            return EXIT_SYSERR;
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   445
        } catch (AnnotationProcessingError ex) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   446
            if (apiMode)
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   447
                throw new RuntimeException(ex.getCause());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            apMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        } catch (ClientCodeException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            // as specified by javax.tools.JavaCompiler#getTask
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            // and javax.tools.JavaCompiler.CompilationTask#call
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            throw new RuntimeException(ex.getCause());
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        } catch (PropagatedException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            throw ex.getCause();
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        } catch (Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            // Nasty.  If we've already reported an error, compensate
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
            // for buggy compiler error recovery by swallowing thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            // exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            if (comp == null || comp.errorCount() == 0 ||
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   461
                options == null || options.isSet("dev"))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                bugMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        } finally {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   465
            if (comp != null) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   466
                try {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   467
                    comp.close();
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   468
                } catch (ClientCodeException ex) {
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   469
                    throw new RuntimeException(ex.getCause());
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   470
                }
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8623
diff changeset
   471
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            filenames = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            options = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        return EXIT_OK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    /** Print a message reporting an internal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    void bugMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        Log.printLines(out, getLocalizedString("msg.bug",
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                                               JavaCompiler.version()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 5857
diff changeset
   486
    /** Print a message reporting a fatal error.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    void feMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        Log.printLines(out, ex.getMessage());
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6592
diff changeset
   490
        if (ex.getCause() != null && options.isSet("dev")) {
6581
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 5857
diff changeset
   491
            ex.getCause().printStackTrace(out);
f58f0ce45802 6980707: Reduce use of IOException in JavaCompiler
jjg
parents: 5857
diff changeset
   492
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
    /** Print a message reporting an input/output error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    void ioMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        Log.printLines(out, getLocalizedString("msg.io"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    /** Print a message reporting an out-of-resources error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
    void resourceMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        Log.printLines(out, getLocalizedString("msg.resource"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
//      System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    /** Print a message reporting an uncaught exception from an
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     * annotation processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    void apMessage(AnnotationProcessingError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        Log.printLines(out,
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                       getLocalizedString("msg.proc.annotation.uncaught.exception"));
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6721
diff changeset
   516
        ex.getCause().printStackTrace(out);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
5319
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   519
    /** 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
   520
    void showClass(String className) {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   521
        out.println("javac: show class: " + className);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   522
        URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   523
        if (url == null)
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   524
            out.println("  class not found");
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   525
        else {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   526
            out.println("  " + url);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   527
            try {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   528
                final String algorithm = "MD5";
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   529
                byte[] digest;
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   530
                MessageDigest md = MessageDigest.getInstance(algorithm);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   531
                DigestInputStream in = new DigestInputStream(url.openStream(), md);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   532
                try {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   533
                    byte[] buf = new byte[8192];
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   534
                    int n;
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   535
                    do { n = in.read(buf); } while (n > 0);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   536
                    digest = md.digest();
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   537
                } finally {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   538
                    in.close();
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   539
                }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   540
                StringBuilder sb = new StringBuilder();
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   541
                for (byte b: digest)
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   542
                    sb.append(String.format("%02x", b));
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   543
                out.println("  " + algorithm + " checksum: " + sb);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   544
            } catch (Exception e) {
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   545
                out.println("  cannot compute digest: " + e);
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   546
            }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   547
        }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   548
    }
63dc7f367a37 6942649: add hidden option to identify location and version of javac classes
jjg
parents: 2723
diff changeset
   549
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    private JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    /* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
     * Internationalization
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    /** Find a localized string in the resource bundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     *  @param key     The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    public static String getLocalizedString(String key, Object... args) { // FIXME sb private
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
            if (messages == null)
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   562
                messages = new JavacMessages(javacBundleName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            return messages.getLocalizedString("javac." + key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
        catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
            throw new Error("Fatal Error: Resource for javac is missing", e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
    public static void useRawMessages(boolean enable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        if (enable) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   572
            messages = new JavacMessages(javacBundleName) {
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6721
diff changeset
   573
                    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                    public String getLocalizedString(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                        return key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        } else {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   579
            messages = new JavacMessages(javacBundleName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
    private static final String javacBundleName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        "com.sun.tools.javac.resources.javac";
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   586
    private static JavacMessages messages;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
}