langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
author xdono
Wed, 02 Jul 2008 12:56:02 -0700
changeset 735 372aa565a221
parent 731 1dd22bdb9ca5
child 864 b1cf6afb8244
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 731
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.main;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.LinkedHashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.MissingResourceException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.ResourceBundle;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.logging.Handler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.logging.Logger;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 169
diff changeset
    43
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.source.util.TaskEvent;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.source.util.TaskListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.parser.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.comp.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.jvm.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.javac.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import javax.annotation.processing.Processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import static javax.tools.StandardLocation.CLASS_OUTPUT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import static com.sun.tools.javac.util.ListBuffer.lb;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
// TEMP, until we have a more efficient way to save doc comment info
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.parser.DocCommentScanner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
/** This class could be the main entry point for GJC when GJC is used as a
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *  component in a larger software system. It provides operations to
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *  construct a new compiler, and to run a new compiler on a set of source
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 *  files.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
public class JavaCompiler implements ClassReader.SourceCompleter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /** The context key for the compiler. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    protected static final Context.Key<JavaCompiler> compilerKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        new Context.Key<JavaCompiler>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    /** Get the JavaCompiler instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public static JavaCompiler instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        JavaCompiler instance = context.get(compilerKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            instance = new JavaCompiler(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** The current version number as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    public static String version() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        return version("release");  // mm.nn.oo[-milestone]
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /** The current full version number as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public static String fullVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        return version("full"); // mm.mm.oo[-milestone]-build
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    private static final String versionRBName = "com.sun.tools.javac.resources.version";
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private static ResourceBundle versionRB;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private static String version(String key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (versionRB == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                versionRB = ResourceBundle.getBundle(versionRBName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            } catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                return Log.getLocalizedString("version.resource.missing", System.getProperty("java.version"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            return versionRB.getString(key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        catch (MissingResourceException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            return Log.getLocalizedString("version.unknown", System.getProperty("java.version"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    private static enum CompilePolicy {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
         * Just attribute the parse trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        ATTR_ONLY,
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
         * Just attribute and do flow analysis on the parse trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
         * This should catch most user errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        CHECK_ONLY,
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
         * Attribute everything, then do flow analysis for everything,
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
         * then desugar everything, and only then generate output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
         * Means nothing is generated if there are any errors in any classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        SIMPLE,
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
         * After attributing everything and doing flow analysis,
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
         * group the work by compilation unit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
         * Then, process the work for each compilation unit together.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
         * Means nothing is generated for a compilation unit if the are any errors
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
         * in the compilation unit  (or in any preceding compilation unit.)
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        BY_FILE,
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
         * Completely process each entry on the todo list in turn.
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
         * -- this is the same for 1.5.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
         * Means output might be generated for some classes in a compilation unit
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         * and not others.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        BY_TODO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        static CompilePolicy decode(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            if (option == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                return DEFAULT_COMPILE_POLICY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            else if (option.equals("attr"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                return ATTR_ONLY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            else if (option.equals("check"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                return CHECK_ONLY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            else if (option.equals("simple"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                return SIMPLE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            else if (option.equals("byfile"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                return BY_FILE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            else if (option.equals("bytodo"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                return BY_TODO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                return DEFAULT_COMPILE_POLICY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    private static CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    private static enum ImplicitSourcePolicy {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        /** Don't generate or process implicitly read source files. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        NONE,
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        /** Generate classes for implicitly read source files. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        CLASS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        /** Like CLASS, but generate warnings if annotation processing occurs */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        UNSET;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        static ImplicitSourcePolicy decode(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            if (option == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                return UNSET;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            else if (option.equals("none"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                return NONE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            else if (option.equals("class"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                return CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                return UNSET;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /** The log to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    public Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   202
    /** Factory for creating diagnostic objects
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   203
     */
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   204
    JCDiagnostic.Factory diagFactory;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   205
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /** The tree factory module.
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    protected TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** The class reader.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    protected ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /** The class writer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    protected ClassWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /** The module for the symbol table entry phases.
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    protected Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    /** The symbol table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    protected Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    /** The language version.
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    protected Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    /** The module for code generation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    protected Gen gen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    /** The name table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    protected Name.Table names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    /** The attributor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    protected Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    /** The attributor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    protected Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /** The flow analyzer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    protected Flow flow;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    /** The type eraser.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    TransTypes transTypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    /** The syntactic sugar desweetener.
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    Lower lower;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    /** The annotation annotator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    protected Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    /** Force a completion failure on this name
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    protected final Name completionFailureName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /** Type utilities.
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    protected Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /** Access to file objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    protected JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    /** Factory for parsers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    protected Parser.Factory parserFactory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /** Optional listener for progress events
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    protected TaskListener taskListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     * Annotation processing may require and provide a new instance
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     * of the compiler to be used for the analyze and generate phases.
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    protected JavaCompiler delegateCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * Flag set if any annotation processing occurred.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    protected boolean annotationProcessingOccurred;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     * Flag set if any implicit source files read.
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    protected boolean implicitSourceFilesRead;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    protected Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    /** Construct a new compiler using a shared context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    public JavaCompiler(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        context.put(compilerKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        // if fileManager not already set, register the JavacFileManager to be used
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        if (context.get(JavaFileManager.class) == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            JavacFileManager.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        names = Name.Table.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        log = Log.instance(context);
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   312
        diagFactory = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        reader = ClassReader.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        writer = ClassWriter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        todo = Todo.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        parserFactory = Parser.Factory.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            // catch completion problems with predefineds
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            // inlined Check.completionError as it is not initialized yet
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   327
            log.error("cant.access", ex.sym, ex.getDetailValue());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            if (ex instanceof ClassReader.BadClassFile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                throw new Abort();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        gen = Gen.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        flow = Flow.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        transTypes = TransTypes.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        lower = Lower.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        taskListener = context.get(TaskListener.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        reader.sourceCompleter = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        verbose       = options.get("-verbose")       != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        sourceOutput  = options.get("-printsource")   != null; // used to be -s
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        stubOutput    = options.get("-stubs")         != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        relax         = options.get("-relax")         != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        printFlat     = options.get("-printflat")     != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        attrParseOnly = options.get("-attrparseonly") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        encoding      = options.get("-encoding");
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        lineDebugInfo = options.get("-g:")            == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                        options.get("-g:lines")       != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        genEndPos     = options.get("-Xjcov")         != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                        context.get(DiagnosticListener.class) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        devVerbose    = options.get("dev") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        processPcks   = options.get("process.packages") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        verboseCompilePolicy = options.get("verboseCompilePolicy") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        if (attrParseOnly)
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            compilePolicy = CompilePolicy.ATTR_ONLY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            compilePolicy = CompilePolicy.decode(options.get("compilePolicy"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        completionFailureName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            (options.get("failcomplete") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            ? names.fromString(options.get("failcomplete"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    /* Switches:
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
    /** Verbose output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    public boolean verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    /** Emit plain Java source files rather than class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    public boolean sourceOutput;
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    /** Emit stub source files rather than class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    public boolean stubOutput;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    /** Generate attributed parse tree only.
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    public boolean attrParseOnly;
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    /** Switch: relax some constraints for producing the jsr14 prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    boolean relax;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
    /** Debug switch: Emit Java sources after inner class flattening.
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    public boolean printFlat;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    /** The encoding to be used for source input.
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    public String encoding;
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    /** Generate code with the LineNumberTable attribute for debugging
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    public boolean lineDebugInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
    /** Switch: should we store the ending positions?
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    public boolean genEndPos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    /** Switch: should we debug ignored exceptions
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    protected boolean devVerbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    /** Switch: should we (annotation) process packages as well
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    protected boolean processPcks;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
    /** Switch: is annotation processing requested explitly via
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     * CompilationTask.setProcessors?
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    protected boolean explicitAnnotationProcessingRequested = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     * The policy for the order in which to perform the compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
    protected CompilePolicy compilePolicy;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
     * The policy for what to do with implicitly read source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    protected ImplicitSourcePolicy implicitSourcePolicy;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
     * Report activity related to compilePolicy
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    public boolean verboseCompilePolicy;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    /** A queue of all as yet unattributed classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    public Todo todo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
    private Set<Env<AttrContext>> deferredSugar = new HashSet<Env<AttrContext>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    /** The set of currently compiled inputfiles, needed to ensure
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
     *  we don't accidentally overwrite an input file when -s is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
     *  initialized by `compile'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    protected Set<JavaFileObject> inputFiles = new HashSet<JavaFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    /** The number of errors reported so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    public int errorCount() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        if (delegateCompiler != null && delegateCompiler != this)
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
            return delegateCompiler.errorCount();
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            return log.nerrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    protected final <T> List<T> stopIfError(ListBuffer<T> listBuffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        if (errorCount() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            return listBuffer.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    protected final <T> List<T> stopIfError(List<T> list) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        if (errorCount() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            return list;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    /** The number of warnings reported so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    public int warningCount() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        if (delegateCompiler != null && delegateCompiler != this)
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            return delegateCompiler.warningCount();
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            return log.nwarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    /** Whether or not any parse errors have occurred.
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    public boolean parseErrors() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        return parseErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    protected Scanner.Factory getScannerFactory() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        return Scanner.Factory.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    /** Try to open input stream with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
     *  Report an error if this fails.
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     *  @param filename   The file name of the input stream to be opened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    public CharSequence readSource(JavaFileObject filename) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            inputFiles.add(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            return filename.getCharContent(false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            log.error("error.reading.file", filename, e.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    /** Parse contents of input stream.
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     *  @param filename     The name of the file from which input stream comes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
     *  @param input        The input stream to be parsed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
    protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        long msec = now();
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        JCCompilationUnit tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                                      null, List.<JCTree>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        if (content != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
            if (verbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                printVerbose("parsing.started", filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                taskListener.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            int initialErrorCount = log.nerrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            Scanner scanner = getScannerFactory().newScanner(content);
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            Parser parser = parserFactory.newParser(scanner, keepComments(), genEndPos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            tree = parser.compilationUnit();
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            parseErrors |= (log.nerrors > initialErrorCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            if (lineDebugInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                tree.lineMap = scanner.getLineMap();
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            if (verbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                printVerbose("parsing.done", Long.toString(elapsed(msec)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        tree.sourcefile = filename;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        if (content != null && taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
            TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            taskListener.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        public boolean keepComments = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        protected boolean keepComments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
            return keepComments || sourceOutput || stubOutput;
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
    /** Parse contents of file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
     *  @param filename     The name of the file to be parsed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    public JCTree.JCCompilationUnit parse(String filename) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        JavacFileManager fm = (JavacFileManager)fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next());
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    /** Parse contents of file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     *  @param filename     The name of the file to be parsed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    public JCTree.JCCompilationUnit parse(JavaFileObject filename) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        JavaFileObject prev = log.useSource(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
            JCTree.JCCompilationUnit t = parse(filename, readSource(filename));
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
            if (t.endPositions != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                log.setEndPosTable(filename, t.endPositions);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    /** Resolve an identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
     * @param name      The identifier to resolve
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    public Symbol resolveIdent(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        if (name.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
            return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        JavaFileObject prev = log.useSource(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
            JCExpression tree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            for (String s : name.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                tree = (tree == null) ? make.Ident(names.fromString(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
                                      : make.Select(tree, names.fromString(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            JCCompilationUnit toplevel =
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            toplevel.packge = syms.unnamedPackage;
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            return attr.attribIdent(tree, toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            log.useSource(prev);
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
    /** Emit plain Java source for a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
     *  @param env    The attribution environment of the outermost class
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
     *                containing this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
     *  @param cdef   The class definition to be printed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        JavaFileObject outFile
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                                               cdef.sym.flatname.toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                                               JavaFileObject.Kind.SOURCE,
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
                                               null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        if (inputFiles.contains(outFile)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            BufferedWriter out = new BufferedWriter(outFile.openWriter());
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                new Pretty(out, true).printUnit(env.toplevel, cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                if (verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                    printVerbose("wrote.file", outFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
            return outFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
    /** Generate code and emit a class file for a given class
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
     *  @param env    The attribution environment of the outermost class
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     *                containing this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
     *  @param cdef   The class definition from which code is generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            if (gen.genClass(env, cdef))
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
                return writer.writeClass(cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        } catch (ClassWriter.PoolOverflow ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
            log.error(cdef.pos(), "limit.pool");
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        } catch (ClassWriter.StringOverflow ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            log.error(cdef.pos(), "limit.string.overflow",
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                      ex.value.substring(0, 20));
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            chk.completionError(cdef.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    /** Complete compiling a source file that has been accessed
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
     *  by the class file reader.
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
     *  @param c          The class the source file of which needs to be compiled.
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
     *  @param filename   The name of the source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
     *  @param f          An input stream that reads the source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
    public void complete(ClassSymbol c) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
//      System.err.println("completing " + c);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        if (completionFailureName == c.fullname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
            throw new CompletionFailure(c, "user-selected completion failure by class name");
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        JCCompilationUnit tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
        JavaFileObject filename = c.classfile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
        JavaFileObject prev = log.useSource(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            tree = parse(filename, filename.getCharContent(false));
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
            log.error("error.reading.file", filename, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
            tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
            taskListener.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        enter.complete(List.of(tree), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            taskListener.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
        if (enter.getEnv(c) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            boolean isPkgInfo =
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
                tree.sourcefile.isNameCompatible("package-info",
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                                                 JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
            if (isPkgInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                if (enter.getEnv(tree.packge) == null) {
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   692
                    JCDiagnostic diag =
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   693
                        diagFactory.fragment("file.does.not.contain.package",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
                                                 c.location());
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   695
                    throw reader.new BadClassFile(c, filename, diag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            } else {
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   698
                JCDiagnostic diag =
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   699
                        diagFactory.fragment("file.doesnt.contain.class",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   700
                                            c.getQualifiedName());
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
   701
                throw reader.new BadClassFile(c, filename, diag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
        implicitSourceFilesRead = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
    /** Track when the JavaCompiler has been used to compile something. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    private boolean hasBeenUsed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
    private long start_msec = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
    public long elapsed_msec = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
    /** Track whether any errors occurred while parsing source text. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    private boolean parseErrors = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
    public void compile(List<JavaFileObject> sourceFileObject)
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
        throws Throwable {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        compile(sourceFileObject, List.<String>nil(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
     * Main method: compile a list of files, return all compiled classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
     * @param sourceFileObjects file objects to be compiled
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
     * @param classnames class names to process for annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
     * @param processors user provided annotation processors to bypass
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
     * discovery, {@code null} means that no processors were provided
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
    public void compile(List<JavaFileObject> sourceFileObjects,
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
                        List<String> classnames,
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                        Iterable<? extends Processor> processors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        throws IOException // TODO: temp, from JavacProcessingEnvironment
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        if (processors != null && processors.iterator().hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
            explicitAnnotationProcessingRequested = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        // as a JavaCompiler can only be used once, throw an exception if
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        // it has been used before.
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        if (hasBeenUsed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
            throw new AssertionError("attempt to reuse JavaCompiler");
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        hasBeenUsed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        start_msec = now();
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            initProcessAnnotations(processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            // These method calls must be chained to avoid memory leaks
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            delegateCompiler = processAnnotations(enterTrees(stopIfError(parseFiles(sourceFileObjects))),
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                                                  classnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
            delegateCompiler.compile2();
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
            delegateCompiler.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            elapsed_msec = delegateCompiler.elapsed_msec;
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
        } catch (Abort ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            if (devVerbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                ex.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
     * The phases following annotation processing: attribution,
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
     * desugar, and finally code generation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
    private void compile2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            switch (compilePolicy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            case ATTR_ONLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                attribute(todo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
            case CHECK_ONLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                flow(attribute(todo));
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
            case SIMPLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
                generate(desugar(flow(attribute(todo))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
            case BY_FILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                for (List<Env<AttrContext>> list : groupByFile(flow(attribute(todo))).values())
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
                    generate(desugar(list));
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
            case BY_TODO:
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                while (todo.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                    generate(desugar(flow(attribute(todo.next()))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
                assert false: "unknown compile policy";
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        } catch (Abort ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
            if (devVerbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                ex.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        if (verbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
            elapsed_msec = elapsed(start_msec);;
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
            printVerbose("total", Long.toString(elapsed_msec));
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        reportDeferredDiagnostics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        if (!log.hasDiagnosticListener()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            printCount("error", errorCount());
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
            printCount("warn", warningCount());
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
    private List<JCClassDecl> rootClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
     * Parses a list of files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
   public List<JCCompilationUnit> parseFiles(List<JavaFileObject> fileObjects) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
       if (errorCount() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
           return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        //parse all files
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        ListBuffer<JCCompilationUnit> trees = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        for (JavaFileObject fileObject : fileObjects)
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
            trees.append(parse(fileObject));
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
        return trees.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     * Enter the symbols found in a list of parse trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
     * As a side-effect, this puts elements on the "todo" list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
     * Also stores a list of all top level classes in rootClasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
    public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        //enter symbols for all files
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            for (JCCompilationUnit unit: roots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
                taskListener.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
        enter.main(roots);
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
        if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
            for (JCCompilationUnit unit: roots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
                taskListener.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        //If generating source, remember the classes declared in
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
        //the original compilation units listed on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        if (sourceOutput || stubOutput) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
            ListBuffer<JCClassDecl> cdefs = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
            for (JCCompilationUnit unit : roots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
                for (List<JCTree> defs = unit.defs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
                     defs.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
                     defs = defs.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
                    if (defs.head instanceof JCClassDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
                        cdefs.append((JCClassDecl)defs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
            rootClasses = cdefs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        return roots;
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
     * Set to true to enable skeleton annotation processing code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
     * Currently, we assume this variable will be replaced more
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
     * advanced logic to figure out if annotation processing is
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
     * needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
    boolean processAnnotations = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
     * Object to handle annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
    JavacProcessingEnvironment procEnvImpl = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
     * Check if we should process annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
     * If so, and if no scanner is yet registered, then set up the DocCommentScanner
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
     * to catch doc comments, and set keepComments so the parser records them in
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
     * the compilation unit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
     * @param processors user provided annotation processors to bypass
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
     * discovery, {@code null} means that no processors were provided
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
    public void initProcessAnnotations(Iterable<? extends Processor> processors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        // Process annotations if processing is not disabled and there
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        // is at least one Processor available.
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
        if (options.get("-proc:none") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            processAnnotations = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
        } else if (procEnvImpl == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
            procEnvImpl = new JavacProcessingEnvironment(context, processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            processAnnotations = procEnvImpl.atLeastOneProcessor();
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
            if (processAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                if (context.get(Scanner.Factory.scannerFactoryKey) == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                    DocCommentScanner.Factory.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                options.put("save-parameter-names", "save-parameter-names");
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                reader.saveParameterNames = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                keepComments = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                if (taskListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                    taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            } else { // free resources
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
                procEnvImpl.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
    // TODO: called by JavacTaskImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
    public JavaCompiler processAnnotations(List<JCCompilationUnit> roots) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
        return processAnnotations(roots, List.<String>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
     * Process any anotations found in the specifed compilation units.
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
     * @param roots a list of compilation units
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
     * @return an instance of the compiler in which to complete the compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
    public JavaCompiler processAnnotations(List<JCCompilationUnit> roots,
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
                                           List<String> classnames)
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
        throws IOException  { // TODO: see TEMP note in JavacProcessingEnvironment
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
        if (errorCount() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            // Errors were encountered.  If todo is empty, then the
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
            // encountered errors were parse errors.  Otherwise, the
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
            // errors were found during the enter phase which should
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
            // be ignored when processing annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
            if (todo.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
        // ASSERT: processAnnotations and procEnvImpl should have been set up by
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
        // by initProcessAnnotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
        // NOTE: The !classnames.isEmpty() checks should be refactored to Main.
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
        if (!processAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
            // If there are no annotation processors present, and
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
            // annotation processing is to occur with compilation,
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
            // emit a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
            Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
            if (options.get("-proc:only") != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                log.warning("proc.proc-only.requested.no.procs");
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
                todo.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
            // If not processing annotations, classnames must be empty
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
            if (!classnames.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
                log.error("proc.no.explicit.annotation.processing.requested",
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
                          classnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
            return this; // continue regular compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
            List<ClassSymbol> classSymbols = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
            List<PackageSymbol> pckSymbols = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
            if (!classnames.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                 // Check for explicit request for annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                 // processing
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                if (!explicitAnnotationProcessingRequested()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                    log.error("proc.no.explicit.annotation.processing.requested",
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                              classnames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
                    return this; // TODO: Will this halt compilation?
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                    boolean errors = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                    for (String nameStr : classnames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                        Symbol sym = resolveIdent(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                        if (sym == null || (sym.kind == Kinds.PCK && !processPcks)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                            log.error("proc.cant.find.class", nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                            errors = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                            continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
                            if (sym.kind == Kinds.PCK)
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                                sym.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
                            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
                                Name name = names.fromString(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                                if (sym.kind == Kinds.PCK)
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                                    pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                                    classSymbols = classSymbols.prepend((ClassSymbol)sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                            assert sym.kind == Kinds.PCK;
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                            log.warning("proc.package.does.not.exist", nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                            pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                        } catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                            log.error("proc.cant.find.class", nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                            errors = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                            continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                    if (errors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
            JavaCompiler c = procEnvImpl.doProcessing(context, roots, classSymbols, pckSymbols);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
            if (c != this)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
                annotationProcessingOccurred = c.annotationProcessingOccurred = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
            return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
        } catch (CompletionFailure ex) {
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents: 10
diff changeset
  1006
            log.error("cant.access", ex.sym, ex.getDetailValue());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
    boolean explicitAnnotationProcessingRequested() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
            explicitAnnotationProcessingRequested ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            options.get("-processor") != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
            options.get("-processorpath") != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            options.get("-proc:only") != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            options.get("-Xprint") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
     * Attribute a list of parse trees, such as found on the "todo" list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
     * Note that attributing classes may cause additional files to be
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
     * parsed and entered via the SourceCompleter.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
     * Attribution of the entries in the list does not stop if any errors occur.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
     * @returns a list of environments for attributd classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
    public List<Env<AttrContext>> attribute(ListBuffer<Env<AttrContext>> envs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
        ListBuffer<Env<AttrContext>> results = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
        while (envs.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
            results.append(attribute(envs.next()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
        return results.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
     * Attribute a parse tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
     * @returns the attributed parse tree
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
    public Env<AttrContext> attribute(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
        if (verboseCompilePolicy)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
            log.printLines(log.noticeWriter, "[attribute " + env.enclClass.sym + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
        if (verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
            printVerbose("checking.attribution", env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
        if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
            taskListener.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
        JavaFileObject prev = log.useSource(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                                  env.enclClass.sym.sourcefile != null ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                                  env.enclClass.sym.sourcefile :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                                  env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
            attr.attribClass(env.tree.pos(), env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
     * Perform dataflow checks on attributed parse trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
     * These include checks for definite assignment and unreachable statements.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
     * If any errors occur, an empty list will be returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
     * @returns the list of attributed parse trees
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
    public List<Env<AttrContext>> flow(List<Env<AttrContext>> envs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
        ListBuffer<Env<AttrContext>> results = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
        for (List<Env<AttrContext>> l = envs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
            flow(l.head, results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
        return stopIfError(results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
     * Perform dataflow checks on an attributed parse tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
    public List<Env<AttrContext>> flow(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        ListBuffer<Env<AttrContext>> results = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
        flow(env, results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        return stopIfError(results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
     * Perform dataflow checks on an attributed parse tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
    protected void flow(Env<AttrContext> env, ListBuffer<Env<AttrContext>> results) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
            if (errorCount() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            if (relax || deferredSugar.contains(env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
                results.append(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
            if (verboseCompilePolicy)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                log.printLines(log.noticeWriter, "[flow " + env.enclClass.sym + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
            JavaFileObject prev = log.useSource(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
                                                env.enclClass.sym.sourcefile != null ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
                                                env.enclClass.sym.sourcefile :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                                                env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                make.at(Position.FIRSTPOS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
                TreeMaker localMake = make.forToplevel(env.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                flow.analyzeTree(env.tree, localMake);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                if (errorCount() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                results.append(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
            finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
            if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                taskListener.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
     * Prepare attributed parse trees, in conjunction with their attribution contexts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
     * for source or code generation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
     * If any errors occur, an empty list will be returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
     * @returns a list containing the classes to be generated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
    public List<Pair<Env<AttrContext>, JCClassDecl>> desugar(List<Env<AttrContext>> envs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
        ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
        for (List<Env<AttrContext>> l = envs; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
            desugar(l.head, results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
        return stopIfError(results);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
     * Prepare attributed parse trees, in conjunction with their attribution contexts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
     * for source or code generation. If the file was not listed on the command line,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
     * the current implicitSourcePolicy is taken into account.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
     * The preparation stops as soon as an error is found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
    protected void desugar(Env<AttrContext> env, ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
        if (errorCount() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
        if (implicitSourcePolicy == ImplicitSourcePolicy.NONE
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                && !inputFiles.contains(env.toplevel.sourcefile)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
        if (desugarLater(env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            if (verboseCompilePolicy)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
                log.printLines(log.noticeWriter, "[defer " + env.enclClass.sym + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            todo.append(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        deferredSugar.remove(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        if (verboseCompilePolicy)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            log.printLines(log.noticeWriter, "[desugar " + env.enclClass.sym + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
                                  env.enclClass.sym.sourcefile :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
                                  env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
            //save tree prior to rewriting
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            JCTree untranslated = env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
            make.at(Position.FIRSTPOS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
            TreeMaker localMake = make.forToplevel(env.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
            if (env.tree instanceof JCCompilationUnit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
                if (!(stubOutput || sourceOutput || printFlat)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
                    List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
                    if (pdef.head != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
                        assert pdef.tail.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                        results.append(new Pair<Env<AttrContext>, JCClassDecl>(env, (JCClassDecl)pdef.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            if (stubOutput) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                //emit stub Java source file, only for compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                //units enumerated explicitly on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
                JCClassDecl cdef = (JCClassDecl)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
                if (untranslated instanceof JCClassDecl &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
                    rootClasses.contains((JCClassDecl)untranslated) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                    ((cdef.mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
                     cdef.sym.packge().getQualifiedName() == names.java_lang)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
                    results.append(new Pair<Env<AttrContext>, JCClassDecl>(env, removeMethodBodies(cdef)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
            env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
            if (errorCount() != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
            if (sourceOutput) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
                //emit standard Java source file, only for compilation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                //units enumerated explicitly on the command line
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                JCClassDecl cdef = (JCClassDecl)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
                if (untranslated instanceof JCClassDecl &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
                    rootClasses.contains((JCClassDecl)untranslated)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
                    results.append(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
            //translate out inner classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
            List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
            if (errorCount() != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            //generate code for each class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
                JCClassDecl cdef = (JCClassDecl)l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
                results.append(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
     * Determine if a class needs to be desugared later.  As erasure
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
     * (TransTypes) destroys information needed in flow analysis, we
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
     * need to ensure that supertypes are translated before derived
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
     * types are translated.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
    public boolean desugarLater(final Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
        if (compilePolicy == CompilePolicy.BY_FILE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
        if (!devVerbose && deferredSugar.contains(env))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
            // guarantee that compiler terminates
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        class ScanNested extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
            Set<Symbol> externalSupers = new HashSet<Symbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
            public void visitClassDef(JCClassDecl node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
                Type st = types.supertype(node.sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
                if (st.tag == TypeTags.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
                    ClassSymbol c = st.tsym.outermostClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
                    Env<AttrContext> stEnv = enter.getEnv(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
                    if (stEnv != null && env != stEnv)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
                        externalSupers.add(st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
                super.visitClassDef(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
        ScanNested scanner = new ScanNested();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
        scanner.scan(env.tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
        if (scanner.externalSupers.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
        if (!deferredSugar.add(env) && devVerbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
            throw new AssertionError(env.enclClass.sym + " was deferred, " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
                                     "second time has these external super types " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
                                     scanner.externalSupers);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
    /** Generates the source or class file for a list of classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
     * The decision to generate a source file or a class file is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
     * based upon the compiler's options.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
     * Generation stops if an error occurs while writing files.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
    public void generate(List<Pair<Env<AttrContext>, JCClassDecl>> list) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
        generate(list, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
    public void generate(List<Pair<Env<AttrContext>, JCClassDecl>> list, ListBuffer<JavaFileObject> results) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
        boolean usePrintSource = (stubOutput || sourceOutput || printFlat);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
        for (List<Pair<Env<AttrContext>, JCClassDecl>> l = list; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
            Pair<Env<AttrContext>, JCClassDecl> x = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
            Env<AttrContext> env = x.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
            JCClassDecl cdef = x.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
            if (verboseCompilePolicy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
                log.printLines(log.noticeWriter, "[generate "
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
                               + (usePrintSource ? " source" : "code")
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
                               + " " + env.enclClass.sym + "]");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
            if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
                taskListener.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
            JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
                                      env.enclClass.sym.sourcefile :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
                                      env.toplevel.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
                JavaFileObject file;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
                if (usePrintSource)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
                    file = printSource(env, cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
                    file = genCode(env, cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
                if (results != null && file != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
                    results.append(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
            } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
                log.error(cdef.pos(), "class.cant.write",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
                          cdef.sym, ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
            if (taskListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                taskListener.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
        // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
        Map<JCCompilationUnit, List<Env<AttrContext>>> groupByFile(List<Env<AttrContext>> list) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
            // use a LinkedHashMap to preserve the order of the original list as much as possible
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
            Map<JCCompilationUnit, List<Env<AttrContext>>> map = new LinkedHashMap<JCCompilationUnit, List<Env<AttrContext>>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
            Set<JCCompilationUnit> fixupSet = new HashSet<JCTree.JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
            for (List<Env<AttrContext>> l = list; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
                Env<AttrContext> env = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
                List<Env<AttrContext>> sublist = map.get(env.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
                if (sublist == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
                    sublist = List.of(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
                    // this builds the list for the file in reverse order, so make a note
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
                    // to reverse the list before returning.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
                    sublist = sublist.prepend(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
                    fixupSet.add(env.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                map.put(env.toplevel, sublist);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
            // fixup any lists that need reversing back to the correct order
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
            for (JCTree.JCCompilationUnit tree: fixupSet)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                map.put(tree, map.get(tree).reverse());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
            return map;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
        JCClassDecl removeMethodBodies(JCClassDecl cdef) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
            final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
            class MethodBodyRemover extends TreeTranslator {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
                public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
                    tree.mods.flags &= ~Flags.SYNCHRONIZED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
                    for (JCVariableDecl vd : tree.params)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
                        vd.mods.flags &= ~Flags.FINAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
                    tree.body = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
                    super.visitMethodDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
                public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
                    if (tree.init != null && tree.init.type.constValue() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
                        tree.init = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
                    super.visitVarDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
                public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
                    ListBuffer<JCTree> newdefs = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
                    for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
                        JCTree t = it.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                        switch (t.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                        case JCTree.CLASSDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                            if (isInterface ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                                (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
                                (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                                newdefs.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
                        case JCTree.METHODDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
                            if (isInterface ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
                                (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
                                ((JCMethodDecl) t).sym.name == names.init ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
                                (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
                                newdefs.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
                            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                        case JCTree.VARDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                            if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                                (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                                newdefs.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                    tree.defs = newdefs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                    super.visitClassDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
            MethodBodyRemover r = new MethodBodyRemover();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
            return r.translate(cdef);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
    public void reportDeferredDiagnostics() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
        if (annotationProcessingOccurred
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
                && implicitSourceFilesRead
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
                && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
            if (explicitAnnotationProcessingRequested())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
                log.warning("proc.use.implicit");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                log.warning("proc.use.proc.or.implicit");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
        chk.reportDeferredDiagnostics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
    /** Close the compiler, flushing the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
        close(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
    private void close(boolean disposeNames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
        rootClasses = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
        reader = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
        make = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
        writer = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
        enter = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
        if (todo != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
            todo.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
        todo = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
        parserFactory = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
        syms = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
        source = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
        attr = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
        chk = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
        gen = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
        flow = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
        transTypes = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
        lower = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
        annotate = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
        types = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
        log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
            fileManager.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
            throw new Abort(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
            if (names != null && disposeNames)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
                names.dispose();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
            names = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
    /** Output for "-verbose" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
     *  @param key The key to look up the correct internationalized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
     *  @param arg An argument for substitution into the output string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
    protected void printVerbose(String key, Object arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
        Log.printLines(log.noticeWriter, log.getLocalizedString("verbose." + key, arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
    /** Print numbers of errors and warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
    protected void printCount(String kind, int count) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
        if (count != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
            String text;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
            if (count == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
                text = log.getLocalizedString("count." + kind, String.valueOf(count));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
                text = log.getLocalizedString("count." + kind + ".plural", String.valueOf(count));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
            Log.printLines(log.errWriter, text);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
            log.errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
    private static long now() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
        return System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
    private static long elapsed(long then) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
        return now() - then;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
    public void initRound(JavaCompiler prev) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
        keepComments = prev.keepComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
        start_msec = prev.start_msec;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
        hasBeenUsed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
    public static void enableLogging() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
        Logger logger = Logger.getLogger(com.sun.tools.javac.Main.class.getPackage().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
        logger.setLevel(Level.ALL);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
        for (Handler h : logger.getParent().getHandlers()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
            h.setLevel(Level.ALL);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
       }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
}