langtools/src/share/classes/com/sun/tools/javac/main/Main.java
author jrose
Mon, 04 May 2009 21:04:04 -0700
changeset 2723 b659ca23d5f5
parent 2212 1d3dc0e0ba0c
child 5319 63dc7f367a37
permissions -rw-r--r--
6829189: Java programming with JSR 292 needs language support Summary: Language changes documented in http://wikis.sun.com/display/mlvm/ProjectCoinProposal Reviewed-by: jjg, darcy, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
2212
1d3dc0e0ba0c 6814575: Update copyright year
xdono
parents: 1996
diff changeset
     2
 * Copyright 1999-2009 Sun Microsystems, Inc.  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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.MissingResourceException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Source;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
    34
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
    35
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.jvm.Target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.main.JavacOption.Option;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.main.RecognizedOptions.OptionHelper;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.processing.AnnotationProcessingError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.annotation.processing.Processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
/** This class provides a commandline interface to the GJC compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
public class Main {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** The name of the compiler, for use in diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    String ownName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /** The writer to use for diagnostic output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    PrintWriter out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * If true, any command line arg errors will cause an exception.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    boolean fatalErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /** Result codes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    static final int
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        EXIT_OK = 0,        // Compilation completed with no errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        EXIT_ERROR = 1,     // Completed but reported errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        EXIT_CMDERR = 2,    // Bad command-line arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        EXIT_SYSERR = 3,    // System error or resource exhaustion.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        public void setOut(PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            Main.this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        public void error(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            Main.this.error(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        public void printVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            Log.printLines(out, getLocalizedString("version", ownName,  JavaCompiler.version()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        public void printFullVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            Log.printLines(out, getLocalizedString("fullVersion", ownName,  JavaCompiler.fullVersion()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        public void printHelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            help();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        public void printXhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            xhelp();
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        public void addFile(File f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            if (!filenames.contains(f))
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                filenames.append(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        public void addClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            classnames.append(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        }
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Construct a compiler instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    public Main(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        this(name, new PrintWriter(System.err, true));
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, PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        this.ownName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /** A table of all options that's passed to the JavaCompiler constructor.  */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private Options options = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /** The list of source files to process
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    public ListBuffer<File> filenames = null; // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** List of class files names passed on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public ListBuffer<String> classnames = null; // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /** Print a string that explains usage.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        Log.printLines(out, getLocalizedString("msg.usage.header", ownName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        for (int i=0; i<recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            recognizedOptions[i].help(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Print a string that explains usage for X options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    void xhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        for (int i=0; i<recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            recognizedOptions[i].xhelp(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /** Report a usage error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    void error(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        if (fatalErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            String msg = getLocalizedString(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            throw new PropagatedException(new IllegalStateException(msg));
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        warning(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        Log.printLines(out, getLocalizedString("msg.usage", ownName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** Report a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    void warning(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        Log.printLines(out, ownName + ": "
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                       + getLocalizedString(key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    public Option getOption(String flag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        for (Option option : recognizedOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            if (option.matches(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                return option;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public void setOptions(Options options) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            throw new NullPointerException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        this.options = options;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    public void setFatalErrors(boolean fatalErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        this.fatalErrors = fatalErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** Process command line arguments: store all command line options
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     *  in `options' table and return all source filenames.
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     *  @param flags    The array of command line arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public List<File> processArgs(String[] flags) { // XXX sb protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        int ac = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        while (ac < flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            String flag = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            Option option = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            if (flag.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                // quick hack to speed up file processing:
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                // if the option does not begin with '-', there is no need to check
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                // most of the compiler options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                    if (recognizedOptions[j].matches(flag)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                        option = recognizedOptions[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            if (option == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                error("err.invalid.flag", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                if (ac == flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                    error("err.req.arg", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                String operand = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                if (option.process(options, flag, operand))
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                if (option.process(options, flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        if (!checkDirectory("-d"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        if (!checkDirectory("-s"))
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
        String sourceString = options.get("-source");
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        Source source = (sourceString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            ? Source.lookup(sourceString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            : Source.DEFAULT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        String targetString = options.get("-target");
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        Target target = (targetString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            ? Target.lookup(targetString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            : Target.DEFAULT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        // We don't check source/target consistency for CLDC, as J2ME
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        // profiles are not aligned with J2SE targets; moreover, a
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        // single CLDC target may have many profiles.  In addition,
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        // this is needed for the continued functioning of the JSR14
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        // prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        if (Character.isDigit(target.name.charAt(0))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            if (target.compareTo(source.requiredTarget()) < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                if (targetString != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                    if (sourceString == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                        warning("warn.target.default.source.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                                targetString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                                source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                        warning("warn.source.target.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                                sourceString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                                source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                } else {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   271
                    target = source.requiredTarget();
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   272
                    options.put("-target", target.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                if (targetString == null && !source.allowGenerics()) {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   276
                    target = Target.JDK1_4;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   277
                    options.put("-target", target.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        }
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   281
        if (target.hasInvokedynamic()) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   282
            options.put("invokedynamic",  "invokedynamic");
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   283
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        return filenames.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        private boolean checkDirectory(String optName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            String value = options.get(optName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            if (value == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            File file = new File(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (!file.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                error("err.dir.not.found", value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            if (!file.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                error("err.file.not.directory", value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    public int compile(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        Context context = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        JavacFileManager.preRegister(context); // can't create it until Log has been set up
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        int result = compile(args, context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        if (fileManager instanceof JavacFileManager) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            // A fresh context was created above, so jfm must be a JavacFileManager
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            ((JavacFileManager)fileManager).close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    public int compile(String[] args, Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return compile(args, context, List.<JavaFileObject>nil(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    public int compile(String[] args,
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                       Context context,
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                       List<JavaFileObject> fileObjects,
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                       Iterable<? extends Processor> processors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        if (options == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            options = Options.instance(context); // creates a new one
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        filenames = new ListBuffer<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        classnames = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        JavaCompiler comp = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
         * TODO: Logic below about what is an acceptable command line
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
         * should be updated to take annotation processing semantics
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
         * into account.
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            if (args.length == 0 && fileObjects.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                return EXIT_CMDERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   346
            List<File> files;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            try {
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   348
                files = processArgs(CommandLine.parse(args));
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   349
                if (files == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                    // null signals an error in options, abort
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                    return EXIT_CMDERR;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   352
                } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    // it is allowed to compile nothing if just asking for help or version info
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    if (options.get("-help") != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                        || options.get("-X") != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                        || options.get("-version") != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                        || options.get("-fullversion") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                        return EXIT_OK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                    error("err.no.source.files");
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                    return EXIT_CMDERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            } catch (java.io.FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                Log.printLines(out, ownName + ": " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                               getLocalizedString("err.file.not.found",
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                                                  e.getMessage()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            boolean forceStdOut = options.get("stdout") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            if (forceStdOut) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                out.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                out = new PrintWriter(System.out, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            context.put(Log.outKey, out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   377
            // allow System property in following line as a Mustang legacy
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   378
            boolean batchMode = (options.get("nonBatchMode") == null
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   379
                        && System.getProperty("nonBatchMode") == null);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   380
            if (batchMode)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   381
                CacheFSInfo.preRegister(context);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   382
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            comp = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            if (comp == null) return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   388
            Log log = Log.instance(context);
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   389
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   390
            if (!files.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                // add filenames to fileObjects
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                comp = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                List<JavaFileObject> otherFiles = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                JavacFileManager dfm = (JavacFileManager)fileManager;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   395
                for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                    otherFiles = otherFiles.prepend(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                for (JavaFileObject fo : otherFiles)
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                    fileObjects = fileObjects.prepend(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            comp.compile(fileObjects,
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                         classnames.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                         processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   404
            if (log.expectDiagKeys != null) {
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   405
                if (log.expectDiagKeys.size() == 0) {
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   406
                    Log.printLines(log.noticeWriter, "all expected diagnostics found");
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   407
                    return EXIT_OK;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   408
                } else {
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   409
                    Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys);
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   410
                    return EXIT_ERROR;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   411
                }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   412
            }
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1471
diff changeset
   413
1996
c855318a4b03 6595666: fix -Werror
jjg
parents: 1866
diff changeset
   414
            if (comp.errorCount() != 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                return EXIT_ERROR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            ioMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        } catch (OutOfMemoryError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        } catch (StackOverflowError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        } catch (FatalError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            feMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        } catch(AnnotationProcessingError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            apMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        } catch (ClientCodeException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            // as specified by javax.tools.JavaCompiler#getTask
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            // and javax.tools.JavaCompiler.CompilationTask#call
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            throw new RuntimeException(ex.getCause());
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        } catch (PropagatedException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            throw ex.getCause();
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        } catch (Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            // Nasty.  If we've already reported an error, compensate
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            // for buggy compiler error recovery by swallowing thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            // exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            if (comp == null || comp.errorCount() == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                options == null || options.get("dev") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                bugMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            if (comp != null) comp.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            filenames = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            options = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        return EXIT_OK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    /** Print a message reporting an internal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    void bugMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        Log.printLines(out, getLocalizedString("msg.bug",
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                                               JavaCompiler.version()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    /** Print a message reporting an fatal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    void feMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        Log.printLines(out, ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    /** Print a message reporting an input/output error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    void ioMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        Log.printLines(out, getLocalizedString("msg.io"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
    /** Print a message reporting an out-of-resources error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    void resourceMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        Log.printLines(out, getLocalizedString("msg.resource"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
//      System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
    /** Print a message reporting an uncaught exception from an
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
     * annotation processor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    void apMessage(AnnotationProcessingError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        Log.printLines(out,
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                       getLocalizedString("msg.proc.annotation.uncaught.exception"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        ex.getCause().printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    private JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    /* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
     * Internationalization
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    /** Find a localized string in the resource bundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     *  @param key     The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    public static String getLocalizedString(String key, Object... args) { // FIXME sb private
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            if (messages == null)
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   503
                messages = new JavacMessages(javacBundleName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            return messages.getLocalizedString("javac." + key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            throw new Error("Fatal Error: Resource for javac is missing", e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    public static void useRawMessages(boolean enable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        if (enable) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   513
            messages = new JavacMessages(javacBundleName) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                    public String getLocalizedString(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                        return key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        } else {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   519
            messages = new JavacMessages(javacBundleName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
    private static final String javacBundleName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        "com.sun.tools.javac.resources.javac";
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1208
diff changeset
   526
    private static JavacMessages messages;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
}