langtools/src/share/classes/com/sun/tools/apt/main/Main.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 7637 467bc8fc514e
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
     2
 * Copyright (c) 2004, 2008, 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: 4937
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: 4937
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: 4937
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4937
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.apt.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.FileWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.text.MessageFormat;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.ResourceBundle;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.MissingResourceException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.StringTokenizer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.net.URLClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
    44
import javax.tools.JavaFileManager;
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
    45
import javax.tools.StandardLocation;
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
    46
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
    47
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.code.Source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.code.Symbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.code.Type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.jvm.Target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.apt.comp.AnnotationProcessingError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.apt.comp.UsageMessageNeededException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.apt.util.Bark;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.mirror.apt.AnnotationProcessorFactory;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 3378
diff changeset
    59
import static com.sun.tools.javac.file.Paths.pathToURLs;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 3378
diff changeset
    60
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
/** This class provides a commandline interface to the apt build-time
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  tool.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    64
 *  <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *  or deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 */
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
    69
@SuppressWarnings("deprecation")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
public class Main {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /** For testing: enter any options you want to be set implicitly
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     *  here.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    static String[] forcedOpts = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        // Preserve parameter names from class files if the class was
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        // compiled with debug enabled
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        "-XDsave-parameter-names"
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /** The name of the compiler, for use in diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    String ownName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** The writer to use for diagnostic output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    PrintWriter out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /** Instantiated factory to use in lieu of discovery process.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    AnnotationProcessorFactory providedFactory = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /** Map representing original command-line arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    Map<String,String> origOptions = new HashMap<String, String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /** Classloader to use for finding factories.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    ClassLoader aptCL = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    /** Result codes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    static final int
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        EXIT_OK = 0,        // Compilation completed with no errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        EXIT_ERROR = 1,     // Completed but reported errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        EXIT_CMDERR = 2,    // Bad command-line arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        EXIT_SYSERR = 3,    // System error or resource exhaustion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /** This class represents an option recognized by the main program
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    private class Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        /** Whether or not the option is used only aptOnly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        boolean aptOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        /** Option string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        /** Documentation key for arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        String argsNameKey;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        /** Documentation key for description.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        String descrKey;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        /** Suffix option (-foo=bar or -foo:bar)
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        boolean hasSuffix;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        Option(String name, String argsNameKey, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            this.argsNameKey = argsNameKey;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            this.descrKey = descrKey;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            char lastChar = name.charAt(name.length()-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            hasSuffix = lastChar == ':' || lastChar == '=';
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        Option(String name, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            this(name, null, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        /** Does this option take a (separate) operand?
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        boolean hasArg() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            return argsNameKey != null && !hasSuffix;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        /** Does argument string match option pattern?
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
         *  @param arg        The command line argument string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        boolean matches(String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            return hasSuffix ? arg.startsWith(name) : arg.equals(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        /** For javac-only options, print nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        String helpSynopsis() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            return name +
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                (argsNameKey == null ? "" :
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                 ((hasSuffix ? "" : " ") +
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                  getLocalizedString(argsNameKey)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        /** Print a line of documentation describing this option, if non-standard.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        void xhelp() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        /** Process the option (with arg). Return true if error detected.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            options.put(option, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        /** Process the option (without arg). Return true if error detected.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            if (hasSuffix)
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                return process(name, option.substring(name.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                return process(option, option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    private class SharedOption extends Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        SharedOption(String name, String argsNameKey, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            super(name, argsNameKey, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        SharedOption(String name, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            super(name, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            String s = "  " + helpSynopsis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            out.print(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            for (int j = s.length(); j < 29; j++) out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            Bark.printLines(out, getLocalizedString(descrKey));
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    private class AptOption extends Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        AptOption(String name, String argsNameKey, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            super(name, argsNameKey, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            aptOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        AptOption(String name, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            super(name, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            aptOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        /** Print a line of documentation describing this option, if standard.
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            String s = "  " + helpSynopsis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            out.print(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            for (int j = s.length(); j < 29; j++) out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            Bark.printLines(out, getLocalizedString(descrKey));
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    /** A nonstandard or extended (-X) option
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    private class XOption extends Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        XOption(String name, String argsNameKey, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            super(name, argsNameKey, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        XOption(String name, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            this(name, null, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        void help() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        void xhelp() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    /** A nonstandard or extended (-X) option
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    private class AptXOption extends Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        AptXOption(String name, String argsNameKey, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            super(name, argsNameKey, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            aptOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        AptXOption(String name, String descrKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            this(name, null, descrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        void xhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            String s = "  " + helpSynopsis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            out.print(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            for (int j = s.length(); j < 29; j++) out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            Log.printLines(out, getLocalizedString(descrKey));
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /** A hidden (implementor) option
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    private class HiddenOption extends Option {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        HiddenOption(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            super(name, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        HiddenOption(String name, String argsNameKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            super(name, argsNameKey, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        void help() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        void xhelp() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    private class AptHiddenOption extends HiddenOption {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        AptHiddenOption(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            super(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            aptOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        AptHiddenOption(String name, String argsNameKey) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            super(name, argsNameKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            aptOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    private Option[] recognizedOptions = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        new Option("-g",                                        "opt.g"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        new Option("-g:none",                                   "opt.g.none") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                options.put("-g:", "none");
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        new Option("-g:{lines,vars,source}",                    "opt.g.lines.vars.source") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            boolean matches(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                return s.startsWith("-g:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                String suboptions = option.substring(3);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                options.put("-g:", suboptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                // enter all the -g suboptions as "-g:suboption"
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                    String tok = t.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                    String opt = "-g:" + tok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                    options.put(opt, opt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        new XOption("-Xlint",                                   "opt.Xlint"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        new XOption("-Xlint:{"
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    + "all,"
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    + "cast,deprecation,divzero,empty,unchecked,fallthrough,path,serial,finally,overrides,"
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    + "-cast,-deprecation,-divzero,-empty,-unchecked,-fallthrough,-path,-serial,-finally,-overrides,"
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    + "none}",
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                                                                "opt.Xlint.suboptlist") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            boolean matches(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                return s.startsWith("-Xlint:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                String suboptions = option.substring(7);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                options.put("-Xlint:", suboptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                // enter all the -Xlint suboptions as "-Xlint:suboption"
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                    String tok = t.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                    String opt = "-Xlint:" + tok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    options.put(opt, opt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        new Option("-nowarn",                                   "opt.nowarn"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        new Option("-verbose",                                  "opt.verbose"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        // -deprecation is retained for command-line backward compatibility
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        new Option("-deprecation",                              "opt.deprecation") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                    options.put("-Xlint:deprecation", option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            },
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        new SharedOption("-classpath",     "opt.arg.path",      "opt.classpath"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        new SharedOption("-cp",            "opt.arg.path",      "opt.classpath") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                return super.process("-classpath", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        new Option("-sourcepath",          "opt.arg.path",      "opt.sourcepath"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        new Option("-bootclasspath",       "opt.arg.path",      "opt.bootclasspath") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                options.remove("-Xbootclasspath/p:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                options.remove("-Xbootclasspath/a:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                return super.process(option, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        new XOption("-Xbootclasspath/p:",  "opt.arg.path", "opt.Xbootclasspath.p"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        new XOption("-Xbootclasspath/a:",  "opt.arg.path", "opt.Xbootclasspath.a"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        new XOption("-Xbootclasspath:",    "opt.arg.path", "opt.bootclasspath") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                options.remove("-Xbootclasspath/p:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                options.remove("-Xbootclasspath/a:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                return super.process("-bootclasspath", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        new Option("-extdirs",             "opt.arg.dirs",      "opt.extdirs"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        new XOption("-Djava.ext.dirs=",    "opt.arg.dirs",      "opt.extdirs") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                return super.process("-extdirs", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        new Option("-endorseddirs",        "opt.arg.dirs",      "opt.endorseddirs"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        new XOption("-Djava.endorsed.dirs=","opt.arg.dirs",     "opt.endorseddirs") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                return super.process("-endorseddirs", arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        new Option("-proc:{none, only}",                        "opt.proc.none.only") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            public boolean matches(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                return s.equals("-proc:none") || s.equals("-proc:only");
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        new Option("-processor",        "opt.arg.class",        "opt.processor"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        new Option("-processorpath",    "opt.arg.path",         "opt.processorpath"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        new SharedOption("-d",          "opt.arg.path", "opt.d"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        new SharedOption("-s",          "opt.arg.path", "opt.s"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        new Option("-encoding",         "opt.arg.encoding",     "opt.encoding"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        new SharedOption("-source",             "opt.arg.release",      "opt.source") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            boolean process(String option, String operand) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                Source source = Source.lookup(operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                if (source == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                    error("err.invalid.source", operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                } else if (source.compareTo(Source.JDK1_5) > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                    error("err.unsupported.source.version", operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                return super.process(option, operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        new Option("-target",           "opt.arg.release",      "opt.target") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            boolean process(String option, String operand) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                Target target = Target.lookup(operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                if (target == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                    error("err.invalid.target", operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                } else if (target.compareTo(Target.JDK1_5) > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    error("err.unsupported.target.version", operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                return super.process(option, operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        new AptOption("-version",               "opt.version") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                Bark.printLines(out, ownName + " " + JavaCompiler.version());
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                return super.process(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        new HiddenOption("-fullversion"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
        new AptOption("-help",                                  "opt.help") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                Main.this.help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                return super.process(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        new SharedOption("-X",                                  "opt.X") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                Main.this.xhelp();
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                return super.process(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        // This option exists only for the purpose of documenting itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        // It's actually implemented by the launcher.
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        new AptOption("-J",             "opt.arg.flag",         "opt.J") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            String helpSynopsis() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                hasSuffix = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                return super.helpSynopsis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                throw new AssertionError
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                    ("the -J flag should be caught by the launcher.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        new SharedOption("-A",          "opt.proc.flag",        "opt.A") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                String helpSynopsis() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                    hasSuffix = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    return super.helpSynopsis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                boolean matches(String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                    return arg.startsWith("-A");
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                boolean hasArg() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                    return process(option, option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            },
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        new AptOption("-nocompile",     "opt.nocompile"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        new AptOption("-print",         "opt.print"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        new AptOption("-factorypath", "opt.arg.path", "opt.factorypath"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        new AptOption("-factory",     "opt.arg.class", "opt.factory"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        new AptXOption("-XListAnnotationTypes", "opt.XListAnnotationTypes"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        new AptXOption("-XListDeclarations",    "opt.XListDeclarations"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        new AptXOption("-XPrintAptRounds",      "opt.XPrintAptRounds"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        new AptXOption("-XPrintFactoryInfo",    "opt.XPrintFactoryInfo"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
         * Option to treat both classes and source files as
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
         * declarations that can be given on the command line and
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
         * processed as the result of an apt round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        new AptXOption("-XclassesAsDecls", "opt.XClassesAsDecls"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        // new Option("-moreinfo",                                      "opt.moreinfo") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        new HiddenOption("-moreinfo") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                Type.moreInfo = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                return super.process(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        // treat warnings as errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        new HiddenOption("-Werror"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        // use complex inference from context in the position of a method call argument
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        new HiddenOption("-complexinference"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        // prompt after each error
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        // new Option("-prompt",                                        "opt.prompt"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        new HiddenOption("-prompt"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        // dump stack on error
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        new HiddenOption("-doe"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        // display warnings for generic unchecked and unsafe operations
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        new HiddenOption("-warnunchecked") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                options.put("-Xlint:unchecked", option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        new HiddenOption("-Xswitchcheck") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                options.put("-Xlint:switchcheck", option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        // generate trace output for subtyping operations
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        new HiddenOption("-debugsubtyping"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        new XOption("-Xmaxerrs",        "opt.arg.number",       "opt.maxerrs"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        new XOption("-Xmaxwarns",       "opt.arg.number",       "opt.maxwarns"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        new XOption("-Xstdout",         "opt.arg.file",         "opt.Xstdout") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            boolean process(String option, String arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                    out = new PrintWriter(new FileWriter(arg), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
                } catch (java.io.IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
                    error("err.error.writing.file", arg, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                return super.process(option, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        new XOption("-Xprint",                                  "opt.print"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        new XOption("-XprintRounds",                            "opt.printRounds"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        new XOption("-XprintProcessorInfo",                     "opt.printProcessorInfo"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        /* -O is a no-op, accepted for backward compatibility. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        new HiddenOption("-O"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        /* -Xjcov produces tables to support the code coverage tool jcov. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        new HiddenOption("-Xjcov"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        /* This is a back door to the compiler's option table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
         * -Dx=y sets the option x to the value y.
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
         * -Dx sets the option x to the value x.
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        new HiddenOption("-XD") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
            String s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
            boolean matches(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                this.s = s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                return s.startsWith(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                s = s.substring(name.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                int eq = s.indexOf('=');
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                String key = (eq < 0) ? s : s.substring(0, eq);
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                String value = (eq < 0) ? s : s.substring(eq+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                options.put(key, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        new HiddenOption("sourcefile") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
                String s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                boolean matches(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                    this.s = s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
                    return s.endsWith(".java") ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                        (options.get("-XclassesAsDecls") != null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                boolean process(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
                    if (s.endsWith(".java")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                        if (!sourceFileNames.contains(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
                            sourceFileNames.add(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                    } else if (options.get("-XclassesAsDecls") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
                        classFileNames.add(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            },
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
     * Construct a compiler instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
    public Main(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        this(name, new PrintWriter(System.err, true));
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
     * Construct a compiler instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
    public Main(String name, PrintWriter out) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        this.ownName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        this.out = out;
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
    /** A table of all options that's passed to the JavaCompiler constructor.  */
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
    private Options options = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
    /** The list of source files to process
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
    java.util.List<String> sourceFileNames = new java.util.LinkedList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    /** The list of class files to process
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
    java.util.List<String> classFileNames = new java.util.LinkedList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    /** List of top level names of generated source files from most recent apt round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    java.util.Set<String> genSourceFileNames = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    /** List of names of generated class files from most recent apt round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    java.util.Set<String> genClassFileNames  = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
     * List of all the generated source file names across all apt rounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    java.util.Set<String> aggregateGenSourceFileNames = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
     * List of all the generated class file names across all apt rounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
    java.util.Set<String> aggregateGenClassFileNames  = new java.util.LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
     * List of all the generated file names across all apt rounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    java.util.Set<java.io.File> aggregateGenFiles = new java.util.LinkedHashSet<java.io.File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
     * Set of all factories that have provided a processor on some apt round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
    java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories  =
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
        new java.util.LinkedHashSet<Class<? extends AnnotationProcessorFactory> >();
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    /** Print a string that explains usage.
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    void help() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
        Bark.printLines(out, getLocalizedString("msg.usage.header", ownName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        for (int i=0; i < recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            recognizedOptions[i].help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        Bark.printLines(out, getLocalizedString("msg.usage.footer"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    /** Print a string that explains usage for X options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    void xhelp() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        for (int i=0; i<recognizedOptions.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            recognizedOptions[i].xhelp();
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        Bark.printLines(out, getLocalizedString("msg.usage.nonstandard.footer"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
    /** Report a usage error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
    void error(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        warning(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
    /** Report a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
    void warning(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        Bark.printLines(out, ownName + ": "
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                       + getLocalizedString(key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
    /** Process command line arguments: store all command line options
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
     *  in `options' table and return all source filenames.
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
     *  @param args    The array of command line arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
    protected java.util.List<String> processArgs(String[] flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
        int ac = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
        while (ac < flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            String flag = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            int j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            for (j=0; j < recognizedOptions.length; j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                if (recognizedOptions[j].matches(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            if (j == recognizedOptions.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                error("err.invalid.flag", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            Option option = recognizedOptions[j];
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            if (option.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                if (ac == flags.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                    error("err.req.arg", flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                String operand = flags[ac];
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                ac++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                if (option.process(flag, operand))
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                if (option.process(flag))
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        String sourceString = options.get("-source");
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        Source source = (sourceString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
            ? Source.lookup(sourceString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
            : Source.JDK1_5; // JDK 5 is the latest supported source version
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
        String targetString = options.get("-target");
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        Target target = (targetString != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
            ? Target.lookup(targetString)
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            : Target.JDK1_5; // JDK 5 is the latest supported source version
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        // We don't check source/target consistency for CLDC, as J2ME
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        // profiles are not aligned with J2SE targets; moreover, a
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
        // single CLDC target may have many profiles.  In addition,
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        // this is needed for the continued functioning of the JSR14
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        // prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        if (Character.isDigit(target.name.charAt(0)) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            target.compareTo(source.requiredTarget()) < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            if (targetString != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                if (sourceString == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                    warning("warn.target.default.source.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
                            targetString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
                            source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
                    warning("warn.source.target.conflict",
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
                            sourceString,
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
                            source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
                options.put("-target", source.requiredTarget().name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
        return sourceFileNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
    public int compile(String[] args, AnnotationProcessorFactory factory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        int returnCode = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        providedFactory = factory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
        Context context = new Context();
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   772
        JavacFileManager.preRegister(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        Bark bark;
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
         * Process the command line options to create the intial
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
         * options data.  This processing is at least partially reused
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
         * by any recursive apt calls.
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
        // For testing: assume all arguments in forcedOpts are
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
        // prefixed to command line arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
        processArgs(forcedOpts);
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
         * A run of apt only gets passed the most recently generated
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
         * files; the initial run of apt gets passed the files from
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
         * the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        java.util.List<String> origFilenames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
            // assign args the result of parse to capture results of
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
            // '@file' expansion
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            origFilenames = processArgs((args=CommandLine.parse(args)));
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
   797
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
   798
            if (options.get("suppress-tool-api-removal-message") == null) {
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
   799
                Bark.printLines(out, getLocalizedString("misc.Deprecation"));
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
   800
            }
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 2981
diff changeset
   801
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            if (origFilenames == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
                return EXIT_CMDERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            } else if (origFilenames.size() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                // it is allowed to compile nothing if just asking for help
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                if (options.get("-help") != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                    options.get("-X") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                    return EXIT_OK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
        } catch (java.io.FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            Bark.printLines(out, ownName + ": " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                           getLocalizedString("err.file.not.found",
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
                                              e.getMessage()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
            ioMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        } catch (OutOfMemoryError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
        } catch (StackOverflowError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
        } catch (FatalError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
            feMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
        } catch (sun.misc.ServiceConfigurationError sce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
            sceMessage(sce);
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        } catch (Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
            bugMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        boolean firstRound = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        boolean needSourcePath = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
        boolean needClassPath  = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
        boolean classesAsDecls = options.get("-XclassesAsDecls") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
         * Create augumented classpath and sourcepath values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
         * If any of the prior apt rounds generated any new source
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
         * files, the n'th apt round (and any javac invocation) has the
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
         * source destination path ("-s path") as the last element of
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
         * the "-sourcepath" to the n'th call.
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
         * If any of the prior apt rounds generated any new class files,
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
         * the n'th apt round (and any javac invocation) has the class
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
         * destination path ("-d path") as the last element of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
         * "-classpath" to the n'th call.
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        String augmentedSourcePath = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        String augmentedClassPath = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        String baseClassPath = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
             * Record original options for future annotation processor
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
             * invocations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            origOptions = new HashMap<String, String>(options.size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            for(String s: options.keySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                String value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                if (s.equals(value = options.get(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                    origOptions.put(s, (String)null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                    origOptions.put(s, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            origOptions = Collections.unmodifiableMap(origOptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   873
            JavacFileManager fm = (JavacFileManager) context.get(JavaFileManager.class);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                // Note: it might be necessary to check for an empty
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                // component ("") of the source path or class path
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                String sourceDest = options.get("-s");
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   879
                if (fm.hasLocation(StandardLocation.SOURCE_PATH)) {
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   880
                    for(File f: fm.getLocation(StandardLocation.SOURCE_PATH))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
                        augmentedSourcePath += (f + File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                    augmentedSourcePath += (sourceDest == null)?".":sourceDest;
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                    augmentedSourcePath = ".";
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
                    if (sourceDest != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
                        augmentedSourcePath += (File.pathSeparator + sourceDest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
                String classDest = options.get("-d");
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   891
                if (fm.hasLocation(StandardLocation.CLASS_PATH)) {
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   892
                    for(File f: fm.getLocation(StandardLocation.CLASS_PATH))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                        baseClassPath += (f + File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
                    // put baseClassPath into map to handle any
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                    // value needed for the classloader
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                    options.put("-classpath", baseClassPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                    augmentedClassPath = baseClassPath + ((classDest == null)?".":classDest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                    baseClassPath = ".";
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                    if (classDest != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                        augmentedClassPath = baseClassPath + (File.pathSeparator + classDest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                assert options.get("-classpath") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
             * Create base and augmented class loaders
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            ClassLoader augmentedAptCL = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
             * Use a url class loader to look for classes on the
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
             * user-specified class path. Prepend computed bootclass
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
             * path, which includes extdirs, to the URLClassLoader apt
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
             * uses.
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
                String aptclasspath = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
                String bcp = "";
2981
76ea6ddc4f72 6832154: refactor Paths to be just a utility class for JavacFileManager
jjg
parents: 735
diff changeset
   920
                Iterable<? extends File> bootclasspath = fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
                if (bootclasspath != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
                    for(File f: bootclasspath)
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
                        bcp += (f + File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
                // If the factory path is set, use that path
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                if (providedFactory == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                    aptclasspath = options.get("-factorypath");
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                if (aptclasspath == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
                    aptclasspath = options.get("-classpath");
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                assert aptclasspath != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                aptclasspath = (bcp + aptclasspath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
                aptCL = new URLClassLoader(pathToURLs(aptclasspath));
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                if (providedFactory == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                    options.get("-factorypath") != null) // same CL even if new class files written
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
                    augmentedAptCL = aptCL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
                    // Create class loader in case new class files are
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                    // written
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                    augmentedAptCL = new URLClassLoader(pathToURLs(augmentedClassPath.
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                                                                   substring(baseClassPath.length())),
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                                                        aptCL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
            int round = 0; // For -XPrintAptRounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
                round++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
                Context newContext = new Context();
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                Options newOptions = Options.instance(newContext); // creates a new context
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                newOptions.putAll(options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
                // populate with old options... don't bother reparsing command line, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
                // if genSource files, must add destination to source path
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                if (genSourceFileNames.size() > 0 && !firstRound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                    newOptions.put("-sourcepath", augmentedSourcePath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                    needSourcePath = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                aggregateGenSourceFileNames.addAll(genSourceFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                sourceFileNames.addAll(genSourceFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                genSourceFileNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                // Don't really need to track this; just have to add -d
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                // "foo" to class path if any class files are generated
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                if (genClassFileNames.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                    newOptions.put("-classpath", augmentedClassPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                    aptCL = augmentedAptCL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                    needClassPath = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                aggregateGenClassFileNames.addAll(genClassFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                classFileNames.addAll(genClassFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                genClassFileNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                options = newOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
                if (options.get("-XPrintAptRounds") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                    out.println("apt Round : " + round);
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                    out.println("filenames: " + sourceFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                    if (classesAsDecls)
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                        out.println("classnames: " + classFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                    out.println("options: " + options);
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                returnCode = compile(args, newContext);
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                firstRound = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                // Check for reported errors before continuing
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                bark = Bark.instance(newContext);
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
            } while(((genSourceFileNames.size() != 0 ) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                     (classesAsDecls && genClassFileNames.size() != 0)) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                    bark.nerrors == 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
        } catch (UsageMessageNeededException umne) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
            help();
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
            return EXIT_CMDERR; // will cause usage message to be printed
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
         * Do not compile if a processor has reported an error or if
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
         * there are no source files to process.  A more sophisticated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
         * test would also fail for syntax errors caught by javac.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
        if (options.get("-nocompile") == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
            options.get("-print")     == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
            bark.nerrors == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
            (origFilenames.size() > 0 || aggregateGenSourceFileNames.size() > 0 )) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
             * Need to create new argument string for calling javac:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
             * 1. apt specific arguments (e.g. -factory) must be stripped out
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
             * 2. proper settings for sourcepath and classpath must be used
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
             * 3. generated class names must be added
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
             * 4. class file names as declarations must be removed
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            int newArgsLength = args.length +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
                (needSourcePath?1:0) +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                (needClassPath?1:0) +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
                aggregateGenSourceFileNames.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
            // Null out apt-specific options and don't copy over into
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
            // newArgs. This loop should be a lot faster; the options
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
            // array should be replaced with a better data structure
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
            // which includes a map from strings to options.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
            //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            // If treating classes as declarations, must strip out
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
            // class names from the javac argument list
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
            argLoop:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
            for(int i = 0; i < args.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                int matchPosition = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                // "-A" by itself is recognized by apt but not javac
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                if (args[i] != null && args[i].equals("-A")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                    newArgsLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                    args[i] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                    continue argLoop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                    optionLoop:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
                    for(int j = 0; j < recognizedOptions.length; j++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                        if (args[i] != null && recognizedOptions[j].matches(args[i])) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
                            matchPosition = j;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
                            break optionLoop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                    if (matchPosition != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
                        Option op = recognizedOptions[matchPosition];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                        if (op.aptOnly) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                            newArgsLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                            args[i] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                            if (op.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                                newArgsLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                                args[i+1] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
                            if (op.hasArg()) { // skip over next string
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
                                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
                                continue argLoop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                            if ((options.get("-XclassesAsDecls") != null) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                                (matchPosition == (recognizedOptions.length-1)) ){
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                                // Remove class file names from
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                                // consideration by javac.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
                                if (! args[i].endsWith(".java")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                                    newArgsLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
                                    args[i] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
            String newArgs[] = new String[newArgsLength];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
            int j = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            for(int i=0; i < args.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
                if (args[i] != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
                    newArgs[j++] = args[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
            if (needClassPath)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                newArgs[j++] = "-XD-classpath=" + augmentedClassPath;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
            if (needSourcePath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
                newArgs[j++] = "-XD-sourcepath=" + augmentedSourcePath;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                for(String s: aggregateGenSourceFileNames)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
                    newArgs[j++] = s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            returnCode = com.sun.tools.javac.Main.compile(newArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
        return returnCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
    /** Programmatic interface for main function.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
     * @param args    The command line parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
    int compile(String[] args, Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
        boolean assertionsEnabled = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
        assert assertionsEnabled = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
        if (!assertionsEnabled) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
            // Bark.printLines(out, "fatal error: assertions must be enabled when running javac");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
            // return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
        int exitCode = EXIT_OK;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
        JavaCompiler comp = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            context.put(Bark.outKey, out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
            comp = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            if (comp == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
            java.util.List<String> nameList = new java.util.LinkedList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
            nameList.addAll(sourceFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
            if (options.get("-XclassesAsDecls") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                nameList.addAll(classFileNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            List<Symbol.ClassSymbol> cs
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                = comp.compile(List.from(nameList.toArray(new String[0])),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                               origOptions,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                               aptCL,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
                               providedFactory,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
                               productiveFactories,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
                               aggregateGenFiles);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
            /*
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
             * If there aren't new source files, we shouldn't bother
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
             *  running javac if there were errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
             *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
             * If there are new files, we should try running javac in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
             * case there were typing errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
             *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
            if (comp.errorCount() != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                options.get("-Werror") != null && comp.warningCount() != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                return EXIT_ERROR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
            ioMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
        } catch (OutOfMemoryError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
        } catch (StackOverflowError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            resourceMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        } catch (FatalError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            feMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            return EXIT_SYSERR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
        } catch (UsageMessageNeededException umne) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            help();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            return EXIT_CMDERR; // will cause usage message to be printed
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
        } catch (AnnotationProcessingError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            apMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        } catch (sun.misc.ServiceConfigurationError sce) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            sceMessage(sce);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
        } catch (Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
            bugMessage(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
            return EXIT_ABNORMAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
            if (comp != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
                comp.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
                genSourceFileNames.addAll(comp.getSourceFileNames());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
                genClassFileNames.addAll(comp.getClassFileNames());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
            sourceFileNames = new java.util.LinkedList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
            classFileNames  = new java.util.LinkedList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
        return exitCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
    /** Print a message reporting an internal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
    void bugMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
        Bark.printLines(out, getLocalizedString("msg.bug",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
                                               JavaCompiler.version()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
    /** Print a message reporting an fatal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
    void apMessage(AnnotationProcessingError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
        Bark.printLines(out, getLocalizedString("misc.Problem"));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
        ex.getCause().printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
    /** Print a message about sun.misc.Service problem.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
    void sceMessage(sun.misc.ServiceConfigurationError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
        Bark.printLines(out, getLocalizedString("misc.SunMiscService"));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
    /** Print a message reporting an fatal error.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
    void feMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
        Bark.printLines(out, ex.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
    /** Print a message reporting an input/output error.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
    void ioMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
        Bark.printLines(out, getLocalizedString("msg.io"));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
    /** Print a message reporting an out-of-resources error.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
    void resourceMessage(Throwable ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
        Bark.printLines(out, getLocalizedString("msg.resource"));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
        ex.printStackTrace(out);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
    /* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
     * Internationalization
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
     *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    /** Find a localized string in the resource bundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
     *  @param key     The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
    private static String getLocalizedString(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
        return getText(key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
    private static final String javacRB =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
        "com.sun.tools.javac.resources.javac";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
    private static final String aptRB =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
        "com.sun.tools.apt.resources.apt";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
    private static ResourceBundle messageRBjavac;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
    private static ResourceBundle messageRBapt;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
    /** Initialize ResourceBundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
    private static void initResource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
            messageRBapt   = ResourceBundle.getBundle(aptRB);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
            messageRBjavac = ResourceBundle.getBundle(javacRB);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
        } catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
            Error x = new FatalError("Fatal Error: Resource for apt or javac is missing");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
            x.initCause(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
            throw x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
    /** Get and format message string from resource.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
    private static String getText(String key, Object... _args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
        String[] args = new String[_args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
        for (int i=0; i<_args.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
            args[i] = "" + _args[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
        if (messageRBapt == null || messageRBjavac == null )
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
            initResource();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
            return MessageFormat.format(messageRBapt.getString("apt." + key),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
                                        (Object[]) args);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
        } catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
                return MessageFormat.format(messageRBjavac.getString("javac." + key),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
                                            (Object[]) args);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
            } catch (MissingResourceException f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
                String msg = "apt or javac message file broken: key={0} "
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
                    + "arguments={1}, {2}";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
                return MessageFormat.format(msg, (Object[]) args);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
}