langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 169 ff76730e430e
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
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.jvm;
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.net.URI;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.StandardJavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.comp.Annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.code.Symtab;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.jvm.ClassFile.NameAndType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import javax.tools.JavaFileManager.Location;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import static javax.tools.StandardLocation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
/** This class provides operations to read a classfile into an internal
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  representation. The internal representation is anchored in a
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  ClassSymbol which contains in its scope symbol representations
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  for all other definitions in the classfile. Top-level Classes themselves
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *  appear as members of the scopes of PackageSymbols.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
public class ClassReader extends ClassFile implements Completer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /** The context key for the class reader. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    protected static final Context.Key<ClassReader> classReaderKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        new Context.Key<ClassReader>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /** Switch: verbose output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    boolean verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** Switch: check class file for correct minor version, unrecognized
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     *  attributes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    boolean checkClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /** Switch: read constant pool and code sections. This switch is initially
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     *  set to false but can be turned on from outside.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public boolean readAllOfClassFile = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /** Switch: read GJ signature information.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /** Switch: read varargs attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    boolean allowVarargs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    /** Switch: allow annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    boolean allowAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    /** Switch: preserve parameter names from the variable table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    public boolean saveParameterNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * Switch: cache completion failures unless -XDdev is used
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private boolean cacheCompletionFailure;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * Switch: prefer source files instead of newer when both source
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * and class are available
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public boolean preferSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /** The log to use for verbose output
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    /** The symbol table. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /** The name table. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    final Name.Table names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    /** Force a completion failure on this name
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    final Name completionFailureName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /** Access to files
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    private final JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** Can be reassigned from outside:
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     *  the completer to be used for ".java" files. If this remains unassigned
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     *  ".java" files will not be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    public SourceCompleter sourceCompleter = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /** A hashtable containing the encountered top-level and member classes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     *  indexed by flat names. The table does not contain local classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private Map<Name,ClassSymbol> classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /** A hashtable containing the encountered packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    private Map<Name, PackageSymbol> packages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    /** The current scope where type variables are entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    protected Scope typevars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /** The path name of the class file currently being read.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    protected JavaFileObject currentClassFile = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /** The class or method currently being read.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    protected Symbol currentOwner = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    /** The buffer containing the currently read class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    byte[] buf = new byte[0x0fff0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    /** The current input pointer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    int bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** The objects of the constant pool.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    Object[] poolObj;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /** For every constant pool entry, an index into buf where the
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     *  defining section of the entry is found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    int[] poolIdx;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** Get the ClassReader instance for this invocation. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    public static ClassReader instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        ClassReader instance = context.get(classReaderKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            instance = new ClassReader(context, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    /** Initialize classes and packages, treating this as the definitive classreader. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    public void init(Symtab syms) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        init(syms, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /** Initialize classes and packages, optionally treating this as
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     *  the definitive classreader.
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    private void init(Symtab syms, boolean definitive) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        if (classes != null) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        if (definitive) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            assert packages == null || packages == syms.packages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            packages = syms.packages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            assert classes == null || classes == syms.classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            classes = syms.classes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            packages = new HashMap<Name, PackageSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            classes = new HashMap<Name, ClassSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        packages.put(names.empty, syms.rootPackage);
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        syms.rootPackage.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        syms.unnamedPackage.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /** Construct a new class reader, optionally treated as the
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     *  definitive classreader for this invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    protected ClassReader(Context context, boolean definitive) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        if (definitive) context.put(classReaderKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        names = Name.Table.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        if (fileManager == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            throw new AssertionError("FileManager initialization error");
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        init(syms, definitive);
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        verbose        = options.get("-verbose")        != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        checkClassFile = options.get("-checkclassfile") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        allowGenerics    = source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        allowVarargs     = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        allowAnnotations = source.allowAnnotations();
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        saveParameterNames = options.get("save-parameter-names") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        cacheCompletionFailure = options.get("dev") == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        preferSource = "source".equals(options.get("-Xprefer"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        completionFailureName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            (options.get("failcomplete") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            ? names.fromString(options.get("failcomplete"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        typevars = new Scope(syms.noSymbol);
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    /** Add member to class unless it is synthetic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    private void enterMember(ClassSymbol c, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC)
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            c.members_field.enter(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
 * Error Diagnoses
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    public static class BadClassFile extends CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
         * @param msg A localized message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        public BadClassFile(ClassSymbol c, Object cname, Object msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            super(c, Log.getLocalizedString("bad.class.file.header",
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                                            cname, msg));
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    public BadClassFile badClassFile(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        return new BadClassFile (
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            currentOwner.enclClass(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            currentClassFile,
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            Log.getLocalizedString(key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
 * Buffer Access
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    /** Read a character.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    char nextChar() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /** Read an integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    int nextInt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            ((buf[bp++] & 0xFF) << 24) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            ((buf[bp++] & 0xFF) << 16) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            ((buf[bp++] & 0xFF) << 8) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            (buf[bp++] & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    /** Extract a character at position bp from buf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    char getChar(int bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    /** Extract an integer at position bp from buf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    int getInt(int bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            ((buf[bp] & 0xFF) << 24) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            ((buf[bp+1] & 0xFF) << 16) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            ((buf[bp+2] & 0xFF) << 8) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            (buf[bp+3] & 0xFF);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    /** Extract a long integer at position bp from buf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    long getLong(int bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        DataInputStream bufin =
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            return bufin.readLong();
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    /** Extract a float at position bp from buf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    float getFloat(int bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        DataInputStream bufin =
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            new DataInputStream(new ByteArrayInputStream(buf, bp, 4));
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            return bufin.readFloat();
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    /** Extract a double at position bp from buf.
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    double getDouble(int bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        DataInputStream bufin =
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            return bufin.readDouble();
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
 * Constant Pool Access
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    /** Index all constant pool entries, writing their start addresses into
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
     *  poolIdx.
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    void indexPool() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        poolIdx = new int[nextChar()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        poolObj = new Object[poolIdx.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        while (i < poolIdx.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            poolIdx[i++] = bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            byte tag = buf[bp++];
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            switch (tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            case CONSTANT_Utf8: case CONSTANT_Unicode: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                int len = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                bp = bp + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            case CONSTANT_Class:
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            case CONSTANT_String:
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                bp = bp + 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            case CONSTANT_Fieldref:
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            case CONSTANT_Methodref:
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            case CONSTANT_InterfaceMethodref:
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            case CONSTANT_NameandType:
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            case CONSTANT_Integer:
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            case CONSTANT_Float:
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                bp = bp + 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            case CONSTANT_Long:
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            case CONSTANT_Double:
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                bp = bp + 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                throw badClassFile("bad.const.pool.tag.at",
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                                   Byte.toString(tag),
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                                   Integer.toString(bp -1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    /** Read constant pool entry at start address i, use pool as a cache.
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    Object readPool(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        Object result = poolObj[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        if (result != null) return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        int index = poolIdx[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        if (index == 0) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        byte tag = buf[index];
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        switch (tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        case CONSTANT_Utf8:
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        case CONSTANT_Unicode:
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            throw badClassFile("unicode.str.not.supported");
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        case CONSTANT_Class:
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            poolObj[i] = readClassOrType(getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        case CONSTANT_String:
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            // FIXME: (footprint) do not use toString here
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            poolObj[i] = readName(getChar(index + 1)).toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        case CONSTANT_Fieldref: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            ClassSymbol owner = readClassSymbol(getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            NameAndType nt = (NameAndType)readPool(getChar(index + 3));
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            poolObj[i] = new VarSymbol(0, nt.name, nt.type, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        case CONSTANT_Methodref:
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        case CONSTANT_InterfaceMethodref: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
            ClassSymbol owner = readClassSymbol(getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            NameAndType nt = (NameAndType)readPool(getChar(index + 3));
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            poolObj[i] = new MethodSymbol(0, nt.name, nt.type, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        case CONSTANT_NameandType:
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            poolObj[i] = new NameAndType(
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                readName(getChar(index + 1)),
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                readType(getChar(index + 3)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        case CONSTANT_Integer:
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            poolObj[i] = getInt(index + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
        case CONSTANT_Float:
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            poolObj[i] = new Float(getFloat(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        case CONSTANT_Long:
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            poolObj[i] = new Long(getLong(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        case CONSTANT_Double:
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            poolObj[i] = new Double(getDouble(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            throw badClassFile("bad.const.pool.tag", Byte.toString(tag));
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        return poolObj[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    /** Read signature and convert to type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    Type readType(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        int index = poolIdx[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return sigToType(buf, index + 3, getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    /** If name is an array type or class signature, return the
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     *  corresponding type; otherwise return a ClassSymbol with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    Object readClassOrType(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        int index =  poolIdx[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        int len = getChar(index + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        int start = index + 3;
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        assert buf[start] == '[' || buf[start + len - 1] != ';';
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        // by the above assertion, the following test can be
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        // simplified to (buf[start] == '[')
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        return (buf[start] == '[' || buf[start + len - 1] == ';')
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            ? (Object)sigToType(buf, start, len)
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            : (Object)enterClass(names.fromUtf(internalize(buf, start,
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                                                           len)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    /** Read signature and convert to type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    List<Type> readTypeParams(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        int index = poolIdx[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        return sigToTypeParams(buf, index + 3, getChar(index + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    /** Read class entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    ClassSymbol readClassSymbol(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        return (ClassSymbol) (readPool(i));
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    /** Read name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    Name readName(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        return (Name) (readPool(i));
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
 * Reading Types
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    /** The unread portion of the currently read type is
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     *  signature[sigp..siglimit-1].
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    byte[] signature;
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
    int sigp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
    int siglimit;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    boolean sigEnterPhase = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    /** Convert signature to type, where signature is a name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    Type sigToType(Name sig) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        return sig == null
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            ? null
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            : sigToType(sig.table.names, sig.index, sig.len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    /** Convert signature to type, where signature is a byte array segment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    Type sigToType(byte[] sig, int offset, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        signature = sig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        sigp = offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        siglimit = offset + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        return sigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
    /** Convert signature to type, where signature is implicit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
    Type sigToType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
        switch ((char) signature[sigp]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        case 'T':
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            int start = sigp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            while (signature[sigp] != ';') sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            return sigEnterPhase
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                ? Type.noType
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                : findTypeVar(names.fromUtf(signature, start, sigp - 1 - start));
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        case '+': {
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            Type t = sigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
            return new WildcardType(t, BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
                                    syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        case '*':
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
            return new WildcardType(syms.objectType, BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
                                    syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        case '-': {
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
            Type t = sigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            return new WildcardType(t, BoundKind.SUPER,
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
                                    syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        case 'B':
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
            return syms.byteType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        case 'C':
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            return syms.charType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        case 'D':
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            return syms.doubleType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        case 'F':
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
            return syms.floatType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        case 'I':
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
            return syms.intType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        case 'J':
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
            return syms.longType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        case 'L':
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                // int oldsigp = sigp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                Type t = classSigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                if (sigp < siglimit && signature[sigp] == '.')
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                    throw badClassFile("deprecated inner class signature syntax " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                                       "(please recompile from source)");
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
                System.err.println(" decoded " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
                                   new String(signature, oldsigp, sigp-oldsigp) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                                   " => " + t + " outer " + t.outer());
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
                */
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        case 'S':
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            return syms.shortType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        case 'V':
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            return syms.voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        case 'Z':
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            return syms.booleanType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        case '[':
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            return new ArrayType(sigToType(), syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        case '(':
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            List<Type> argtypes = sigToTypes(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            Type restype = sigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            List<Type> thrown = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            while (signature[sigp] == '^') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                thrown = thrown.prepend(sigToType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
            return new MethodType(argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                                  restype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
                                  thrown.reverse(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                                  syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        case '<':
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            typevars = typevars.dup(currentOwner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            Type poly = new ForAll(sigToTypeParams(), sigToType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            typevars = typevars.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            return poly;
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            throw badClassFile("bad.signature",
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                               Convert.utf2string(signature, sigp, 10));
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
    byte[] signatureBuffer = new byte[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    int sbp = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    /** Convert class signature to type, where signature is implicit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
    Type classSigToType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        if (signature[sigp] != 'L')
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            throw badClassFile("bad.class.signature",
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
                               Convert.utf2string(signature, sigp, 10));
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
        sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        Type outer = Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
        int startSbp = sbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            final byte c = signature[sigp++];
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
            switch (c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            case ';': {         // end
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
                ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                                                         startSbp,
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
                                                         sbp - startSbp));
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                if (outer == Type.noType)
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                    outer = t.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
                    outer = new ClassType(outer, List.<Type>nil(), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
                sbp = startSbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                return outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
            case '<':           // generic arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                                                         startSbp,
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
                                                         sbp - startSbp));
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                outer = new ClassType(outer, sigToTypes('>'), t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
                        boolean completed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
                        public Type getEnclosingType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
                            if (!completed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                                completed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                                tsym.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                                Type enclosingType = tsym.type.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
                                if (enclosingType != Type.noType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                                    List<Type> typeArgs =
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                                        super.getEnclosingType().allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
                                    List<Type> typeParams =
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                                        enclosingType.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                                    if (typeParams.length() != typeArgs.length()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
                                        // no "rare" types
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                                        super.setEnclosingType(types.erasure(enclosingType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
                                        super.setEnclosingType(types.subst(enclosingType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
                                                                           typeParams,
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                                                                           typeArgs));
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
                                    super.setEnclosingType(Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
                                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
                            return super.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
                        public void setEnclosingType(Type outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
                            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
                    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
                switch (signature[sigp++]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
                case ';':
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
                    if (sigp < signature.length && signature[sigp] == '.') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
                        // support old-style GJC signatures
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                        // The signature produced was
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                        // Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>;
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                        // rather than say
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                        // Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>;
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
                        // so we skip past ".Lfoo/Outer$"
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
                        sigp += (sbp - startSbp) + // "foo/Outer"
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
                            3;  // ".L" and "$"
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                        signatureBuffer[sbp++] = (byte)'$';
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
                        sbp = startSbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
                        return outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
                case '.':
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
                    signatureBuffer[sbp++] = (byte)'$';
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
                    throw new AssertionError(signature[sigp-1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            case '.':
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                signatureBuffer[sbp++] = (byte)'$';
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            case '/':
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
                signatureBuffer[sbp++] = (byte)'.';
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                signatureBuffer[sbp++] = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                continue;
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
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
    /** Convert (implicit) signature to list of types
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
     *  until `terminator' is encountered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
    List<Type> sigToTypes(char terminator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        List<Type> head = List.of(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        List<Type> tail = head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        while (signature[sigp] != terminator)
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            tail = tail.setTail(List.of(sigToType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        return head.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    /** Convert signature to type parameters, where signature is a name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    List<Type> sigToTypeParams(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        return sigToTypeParams(name.table.names, name.index, name.len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    /** Convert signature to type parameters, where signature is a byte
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
     *  array segment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
    List<Type> sigToTypeParams(byte[] sig, int offset, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        signature = sig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        sigp = offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
        siglimit = offset + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        return sigToTypeParams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
    /** Convert signature to type parameters, where signature is implicit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
    List<Type> sigToTypeParams() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
        List<Type> tvars = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        if (signature[sigp] == '<') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
            int start = sigp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
            sigEnterPhase = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
            while (signature[sigp] != '>')
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
                tvars = tvars.prepend(sigToTypeParam());
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            sigEnterPhase = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            sigp = start;
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            while (signature[sigp] != '>')
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                sigToTypeParam();
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        return tvars.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
    /** Convert (implicit) signature to type parameter.
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
    Type sigToTypeParam() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        int start = sigp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        while (signature[sigp] != ':') sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
        Name name = names.fromUtf(signature, start, sigp - start);
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
        TypeVar tvar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
        if (sigEnterPhase) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
            tvar = new TypeVar(name, currentOwner, syms.botType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
            typevars.enter(tvar.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
            tvar = (TypeVar)findTypeVar(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
        List<Type> bounds = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
        Type st = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
        if (signature[sigp] == ':' && signature[sigp+1] == ':') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
            st = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
        while (signature[sigp] == ':') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
            sigp++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
            bounds = bounds.prepend(sigToType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        if (!sigEnterPhase) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
            types.setBounds(tvar, bounds.reverse(), st);
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        return tvar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
    /** Find type variable with given name in `typevars' scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
    Type findTypeVar(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        Scope.Entry e = typevars.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        if (e.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            return e.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
            if (readingClassAttr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                // While reading the class attribute, the supertypes
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                // might refer to a type variable from an enclosing element
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                // (method or class).
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                // If the type variable is defined in the enclosing class,
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                // we can actually find it in
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                // currentOwner.owner.type.getTypeArguments()
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
                // However, until we have read the enclosing method attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
                // we don't know for sure if this owner is correct.  It could
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
                // be a method and there is no way to tell before reading the
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                // enclosing method attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
                TypeVar t = new TypeVar(name, currentOwner, syms.botType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
                missingTypeVariables = missingTypeVariables.prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
                // System.err.println("Missing type var " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
            throw badClassFile("undecl.type.var", name);
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
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
 * Reading Attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
    /** Report unrecognized attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
    void unrecognized(Name attrName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        if (checkClassFile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
            printCCF("ccf.unrecognized.attribute", attrName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    /** Read member attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
    void readMemberAttr(Symbol sym, Name attrName, int attrLen) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        //- System.err.println(" z " + sym + ", " + attrName + ", " + attrLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
        if (attrName == names.ConstantValue) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
            Object v = readPool(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            // Ignore ConstantValue attribute if field not final.
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
            if ((sym.flags() & FINAL) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
                ((VarSymbol)sym).setData(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        } else if (attrName == names.Code) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            if (readAllOfClassFile || saveParameterNames)
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
                ((MethodSymbol)sym).code = readCode(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
                bp = bp + attrLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        } else if (attrName == names.Exceptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
            int nexceptions = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            List<Type> thrown = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
            for (int j = 0; j < nexceptions; j++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
                thrown = thrown.prepend(readClassSymbol(nextChar()).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            if (sym.type.getThrownTypes().isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
                sym.type.asMethodType().thrown = thrown.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
        } else if (attrName == names.Synthetic) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            // bridge methods are visible when generics not enabled
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
            if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                sym.flags_field |= SYNTHETIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        } else if (attrName == names.Bridge) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            sym.flags_field |= BRIDGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            if (!allowGenerics)
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                sym.flags_field &= ~SYNTHETIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
        } else if (attrName == names.Deprecated) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
            sym.flags_field |= DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
        } else if (attrName == names.Varargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
            if (allowVarargs) sym.flags_field |= VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
        } else if (attrName == names.Annotation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            if (allowAnnotations) sym.flags_field |= ANNOTATION;
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
        } else if (attrName == names.Enum) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            sym.flags_field |= ENUM;
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        } else if (allowGenerics && attrName == names.Signature) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
            List<Type> thrown = sym.type.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            sym.type = readType(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
            //- System.err.println(" # " + sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                sym.type.asMethodType().thrown = thrown;
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        } else if (attrName == names.RuntimeVisibleAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            attachAnnotations(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        } else if (attrName == names.RuntimeInvisibleAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            attachAnnotations(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        } else if (attrName == names.RuntimeVisibleParameterAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            attachParameterAnnotations(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
        } else if (attrName == names.RuntimeInvisibleParameterAnnotations) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
            attachParameterAnnotations(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        } else if (attrName == names.LocalVariableTable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            int newbp = bp + attrLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
            if (saveParameterNames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
                // pick up parameter names from the variable table
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
                List<Name> parameterNames = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                int firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
                int endParam = firstParam + Code.width(sym.type.getParameterTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                int numEntries = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                for (int i=0; i<numEntries; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                    int start_pc = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                    int length = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                    int nameIndex = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                    int sigIndex = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                    int register = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                    if (start_pc == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                        firstParam <= register &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                        register < endParam) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
                        int index = firstParam;
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
                        for (Type t : sym.type.getParameterTypes()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
                            if (index == register) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
                                parameterNames = parameterNames.prepend(readName(nameIndex));
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
                                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                            index += Code.width(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                parameterNames = parameterNames.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                ((MethodSymbol)sym).savedParameterNames = parameterNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            bp = newbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
        } else if (attrName == names.AnnotationDefault) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
            attachAnnotationDefault(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
        } else if (attrName == names.EnclosingMethod) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
            int newbp = bp + attrLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
            readEnclosingMethodAttr(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
            bp = newbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
            unrecognized(attrName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            bp = bp + attrLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
    void readEnclosingMethodAttr(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
        // sym is a nested class with an "Enclosing Method" attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
        // remove sym from it's current owners scope and place it in
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        // the scope specified by the attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
        sym.owner.members().remove(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
        ClassSymbol self = (ClassSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
        ClassSymbol c = readClassSymbol(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
        NameAndType nt = (NameAndType)readPool(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
        MethodSymbol m = findMethod(nt, c.members_field, self.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
        if (nt != null && m == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
            throw badClassFile("bad.enclosing.method", self);
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
        self.name = simpleBinaryName(self.flatname, c.flatname) ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
        self.owner = m != null ? m : c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
        if (self.name.len == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
            self.fullname = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
            self.fullname = ClassSymbol.formFullName(self.name, self.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
        if (m != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
            ((ClassType)sym.type).setEnclosingType(m.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
        } else if ((self.flags_field & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
            ((ClassType)sym.type).setEnclosingType(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
            ((ClassType)sym.type).setEnclosingType(Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        enterTypevars(self);
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
        if (!missingTypeVariables.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
            ListBuffer<Type> typeVars =  new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
            for (Type typevar : missingTypeVariables) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                typeVars.append(findTypeVar(typevar.tsym.name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
            foundTypeVariables = typeVars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
            foundTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
    // See java.lang.Class
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
    private Name simpleBinaryName(Name self, Name enclosing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
        String simpleBinaryName = self.toString().substring(enclosing.toString().length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
        if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
            throw badClassFile("bad.enclosing.method", self);
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
        int index = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
        while (index < simpleBinaryName.length() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
               isAsciiDigit(simpleBinaryName.charAt(index)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
            index++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
        return names.fromString(simpleBinaryName.substring(index));
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
    private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
        if (nt == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
        MethodType type = nt.type.asMethodType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
        for (Scope.Entry e = scope.lookup(nt.name); e.scope != null; e = e.next())
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
            if (e.sym.kind == MTH && isSameBinaryType(e.sym.type.asMethodType(), type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                return (MethodSymbol)e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
        if (nt.name != names.init)
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
            // not a constructor
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
        if ((flags & INTERFACE) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
            // no enclosing instance
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
        if (nt.type.getParameterTypes().isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
            // no parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
        // A constructor of an inner class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
        // Remove the first argument (the enclosing instance)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
        nt.type = new MethodType(nt.type.getParameterTypes().tail,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                                 nt.type.getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                                 nt.type.getThrownTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                                 syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
        // Try searching again
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
        return findMethod(nt, scope, flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
    /** Similar to Types.isSameType but avoids completion */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
    private boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
        List<Type> types1 = types.erasure(mt1.getParameterTypes())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
            .prepend(types.erasure(mt1.getReturnType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
        List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        while (!types1.isEmpty() && !types2.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            if (types1.head.tsym != types2.head.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            types1 = types1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
            types2 = types2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
        return types1.isEmpty() && types2.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
     * Character.isDigit answers <tt>true</tt> to some non-ascii
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
     * digits.  This one does not.  <b>copied from java.lang.Class</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
    private static boolean isAsciiDigit(char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
        return '0' <= c && c <= '9';
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
    /** Read member attributes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
    void readMemberAttrs(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
        char ac = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
        for (int i = 0; i < ac; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
            Name attrName = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
            int attrLen = nextInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
            readMemberAttr(sym, attrName, attrLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
    /** Read class attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
    void readClassAttr(ClassSymbol c, Name attrName, int attrLen) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
        if (attrName == names.SourceFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
            Name n = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
            c.sourcefile = new SourceFileObject(n);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
        } else if (attrName == names.InnerClasses) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
            readInnerClasses(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
        } else if (allowGenerics && attrName == names.Signature) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
            readingClassAttr = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                ClassType ct1 = (ClassType)c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                assert c == currentOwner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                ct1.typarams_field = readTypeParams(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
                ct1.supertype_field = sigToType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
                ListBuffer<Type> is = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
                while (sigp != siglimit) is.append(sigToType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
                ct1.interfaces_field = is.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                readingClassAttr = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
            readMemberAttr(c, attrName, attrLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
    private boolean readingClassAttr = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
    private List<Type> missingTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
    private List<Type> foundTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
    /** Read class attributes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
    void readClassAttrs(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        char ac = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
        for (int i = 0; i < ac; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
            Name attrName = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
            int attrLen = nextInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            readClassAttr(c, attrName, attrLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
    /** Read code block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
    Code readCode(Symbol owner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
        nextChar(); // max_stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
        nextChar(); // max_locals
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
        final int  code_length = nextInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
        bp += code_length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
        final char exception_table_length = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
        bp += exception_table_length * 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
        readMemberAttrs(owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
 * Reading Java-language annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
    /** Attach annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
    void attachAnnotations(final Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
        int numAttributes = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
        if (numAttributes != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            ListBuffer<CompoundAnnotationProxy> proxies =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                new ListBuffer<CompoundAnnotationProxy>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
            for (int i = 0; i<numAttributes; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                CompoundAnnotationProxy proxy = readCompoundAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
                if (proxy.type.tsym == syms.proprietaryType.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                    sym.flags_field |= PROPRIETARY;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                    proxies.append(proxy);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            annotate.later(new AnnotationCompleter(sym, proxies.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
    /** Attach parameter annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
    void attachParameterAnnotations(final Symbol method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
        final MethodSymbol meth = (MethodSymbol)method;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
        int numParameters = buf[bp++] & 0xFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        List<VarSymbol> parameters = meth.params();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
        int pnum = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
        while (parameters.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
            attachAnnotations(parameters.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
            parameters = parameters.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
            pnum++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
        if (pnum != numParameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
            throw badClassFile("bad.runtime.invisible.param.annotations", meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
    /** Attach the default value for an annotation element.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
    void attachAnnotationDefault(final Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
        final MethodSymbol meth = (MethodSymbol)sym; // only on methods
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
        final Attribute value = readAttributeValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
        annotate.later(new AnnotationDefaultCompleter(meth, value));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
    Type readTypeOrClassSymbol(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
        // support preliminary jsr175-format class files
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
        if (buf[poolIdx[i]] == CONSTANT_Class)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
            return readClassSymbol(i).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
        return readType(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
    Type readEnumType(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
        // support preliminary jsr175-format class files
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
        int index = poolIdx[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
        int length = getChar(index + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
        if (buf[index + length + 2] != ';')
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
            return enterClass(readName(i)).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
        return readType(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
    CompoundAnnotationProxy readCompoundAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
        Type t = readTypeOrClassSymbol(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
        int numFields = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        ListBuffer<Pair<Name,Attribute>> pairs =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            new ListBuffer<Pair<Name,Attribute>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        for (int i=0; i<numFields; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            Name name = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
            Attribute value = readAttributeValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
            pairs.append(new Pair<Name,Attribute>(name, value));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
        return new CompoundAnnotationProxy(t, pairs.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
    Attribute readAttributeValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
        char c = (char) buf[bp++];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
        switch (c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
        case 'B':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
            return new Attribute.Constant(syms.byteType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
        case 'C':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
            return new Attribute.Constant(syms.charType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
        case 'D':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
            return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
        case 'F':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
            return new Attribute.Constant(syms.floatType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
        case 'I':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
            return new Attribute.Constant(syms.intType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
        case 'J':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
            return new Attribute.Constant(syms.longType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        case 'S':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            return new Attribute.Constant(syms.shortType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
        case 'Z':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
            return new Attribute.Constant(syms.booleanType, readPool(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        case 's':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
            return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
        case 'e':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
            return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
        case 'c':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
            return new Attribute.Class(types, readTypeOrClassSymbol(nextChar()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
        case '[': {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
            int n = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
            ListBuffer<Attribute> l = new ListBuffer<Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
            for (int i=0; i<n; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
                l.append(readAttributeValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
            return new ArrayAttributeProxy(l.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        case '@':
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            return readCompoundAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
            throw new AssertionError("unknown annotation tag '" + c + "'");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
    interface ProxyVisitor extends Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
        void visitEnumAttributeProxy(EnumAttributeProxy proxy);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
        void visitArrayAttributeProxy(ArrayAttributeProxy proxy);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
        void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
    static class EnumAttributeProxy extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
        Type enumType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
        Name enumerator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
        public EnumAttributeProxy(Type enumType, Name enumerator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
            super(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
            this.enumType = enumType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            this.enumerator = enumerator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
        public void accept(Visitor v) { ((ProxyVisitor)v).visitEnumAttributeProxy(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
            return "/*proxy enum*/" + enumType + "." + enumerator;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
    static class ArrayAttributeProxy extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
        List<Attribute> values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
        ArrayAttributeProxy(List<Attribute> values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
            super(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
            this.values = values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
        public void accept(Visitor v) { ((ProxyVisitor)v).visitArrayAttributeProxy(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
            return "{" + values + "}";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
    /** A temporary proxy representing a compound attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
    static class CompoundAnnotationProxy extends Attribute {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
        final List<Pair<Name,Attribute>> values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        public CompoundAnnotationProxy(Type type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
                                      List<Pair<Name,Attribute>> values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
            super(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
            this.values = values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
        public void accept(Visitor v) { ((ProxyVisitor)v).visitCompoundAnnotationProxy(this); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
            buf.append("@");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
            buf.append(type.tsym.getQualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
            buf.append("/*proxy*/{");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
            for (List<Pair<Name,Attribute>> v = values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
                 v.nonEmpty(); v = v.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
                Pair<Name,Attribute> value = v.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
                if (!first) buf.append(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
                buf.append(value.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
                buf.append("=");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
                buf.append(value.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
            buf.append("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
            return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
    class AnnotationDeproxy implements ProxyVisitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
        private ClassSymbol requestingOwner = currentOwner.kind == MTH
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
            ? currentOwner.enclClass() : (ClassSymbol)currentOwner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
        List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
            // also must fill in types!!!!
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
            ListBuffer<Attribute.Compound> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
                new ListBuffer<Attribute.Compound>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
            for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
                buf.append(deproxyCompound(l.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
            return buf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
        Attribute.Compound deproxyCompound(CompoundAnnotationProxy a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
            ListBuffer<Pair<Symbol.MethodSymbol,Attribute>> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
                new ListBuffer<Pair<Symbol.MethodSymbol,Attribute>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
            for (List<Pair<Name,Attribute>> l = a.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
                 l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
                 l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                MethodSymbol meth = findAccessMethod(a.type, l.head.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
                buf.append(new Pair<Symbol.MethodSymbol,Attribute>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                           (meth, deproxy(meth.type.getReturnType(), l.head.snd)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
            return new Attribute.Compound(a.type, buf.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
        MethodSymbol findAccessMethod(Type container, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
            CompletionFailure failure = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
                for (Scope.Entry e = container.tsym.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
                     e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
                     e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
                    Symbol sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
                    if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
                        return (MethodSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
            } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
                failure = ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
            // The method wasn't found: emit a warning and recover
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
            JavaFileObject prevSource = log.useSource(requestingOwner.classfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                if (failure == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
                    log.warning("annotation.method.not.found",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
                                container,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
                                name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                    log.warning("annotation.method.not.found.reason",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
                                container,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
                                name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
                                failure.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
                log.useSource(prevSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
            // Construct a new method type and symbol.  Use bottom
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
            // type (typeof null) as return type because this type is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
            // a subtype of all reference types and can be converted
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
            // to primitive types by unboxing.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
            MethodType mt = new MethodType(List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
                                           syms.botType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
                                           List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                                           syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            return new MethodSymbol(PUBLIC | ABSTRACT, name, mt, container.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
        Attribute result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
        Type type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
        Attribute deproxy(Type t, Attribute a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
            Type oldType = type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
                type = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                a.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
                type = oldType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
        // implement Attribute.Visitor below
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
        public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
            // assert value.type == type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            result = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
        public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            result = clazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            throw new AssertionError(); // shouldn't happen
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
        public void visitCompound(Attribute.Compound compound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
            throw new AssertionError(); // shouldn't happen
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
        public void visitArray(Attribute.Array array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
            throw new AssertionError(); // shouldn't happen
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
            throw new AssertionError(); // shouldn't happen
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
        public void visitEnumAttributeProxy(EnumAttributeProxy proxy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            // type.tsym.flatName() should == proxy.enumFlatName
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            TypeSymbol enumTypeSym = proxy.enumType.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
            VarSymbol enumerator = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
            for (Scope.Entry e = enumTypeSym.members().lookup(proxy.enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                 e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                 e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                if (e.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                    enumerator = (VarSymbol)e.sym;
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
            if (enumerator == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                log.error("unknown.enum.constant",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                          currentClassFile, enumTypeSym, proxy.enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
                result = new Attribute.Error(enumTypeSym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
                result = new Attribute.Enum(enumTypeSym.type, enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
        public void visitArrayAttributeProxy(ArrayAttributeProxy proxy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
            int length = proxy.values.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
            Attribute[] ats = new Attribute[length];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
            Type elemtype = types.elemtype(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
            int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
            for (List<Attribute> p = proxy.values; p.nonEmpty(); p = p.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                ats[i++] = deproxy(elemtype, p.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
            result = new Attribute.Array(type, ats);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
        public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
            result = deproxyCompound(proxy);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
    class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Annotator {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
        final MethodSymbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
        final Attribute value;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
        final JavaFileObject classFile = currentClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
            return " ClassReader store default for " + sym.owner + "." + sym + " is " + value;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
        AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
            this.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
            this.value = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
        // implement Annotate.Annotator.enterAnnotation()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
        public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
            JavaFileObject previousClassFile = currentClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                currentClassFile = classFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
                sym.defaultValue = deproxy(sym.type.getReturnType(), value);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
                currentClassFile = previousClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
    class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Annotator {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
        final Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
        final List<CompoundAnnotationProxy> l;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
        final JavaFileObject classFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
            return " ClassReader annotate " + sym.owner + "." + sym + " with " + l;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
        AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
            this.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
            this.l = l;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            this.classFile = currentClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
        // implement Annotate.Annotator.enterAnnotation()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
        public void enterAnnotation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
            JavaFileObject previousClassFile = currentClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
                currentClassFile = classFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
                List<Attribute.Compound> newList = deproxyCompoundList(l);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
                sym.attributes_field = ((sym.attributes_field == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
                                        ? newList
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
                                        : newList.prependList(sym.attributes_field));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
                currentClassFile = previousClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
 * Reading Symbols
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
    /** Read a field.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
    VarSymbol readField() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
        long flags = adjustFieldFlags(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
        Name name = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
        Type type = readType(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
        VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
        readMemberAttrs(v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
        return v;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
    /** Read a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
    MethodSymbol readMethod() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
        long flags = adjustMethodFlags(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
        Name name = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
        Type type = readType(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
        if (name == names.init && currentOwner.hasOuterInstance()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            // Sometimes anonymous classes don't have an outer
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
            // instance, however, there is no reliable way to tell so
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
            // we never strip this$n
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
            if (currentOwner.name.len != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                type = new MethodType(type.getParameterTypes().tail,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                                      type.getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                                      type.getThrownTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                                      syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
        MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
        Symbol prevOwner = currentOwner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
        currentOwner = m;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
            readMemberAttrs(m);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
            currentOwner = prevOwner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
        return m;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
    /** Skip a field or method
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
    void skipMember() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
        bp = bp + 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
        char ac = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
        for (int i = 0; i < ac; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            bp = bp + 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
            int attrLen = nextInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
            bp = bp + attrLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
    /** Enter type variables of this classtype and all enclosing ones in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
     *  `typevars'.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
    protected void enterTypevars(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
        if (t.getEnclosingType() != null && t.getEnclosingType().tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
            enterTypevars(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
        for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
            typevars.enter(xs.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
    protected void enterTypevars(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
        if (sym.owner.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
            enterTypevars(sym.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
            enterTypevars(sym.owner.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
        enterTypevars(sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
    /** Read contents of a given class symbol `c'. Both external and internal
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
     *  versions of an inner class are read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
    void readClass(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
        ClassType ct = (ClassType)c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
        // allocate scope for members
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
        c.members_field = new Scope(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
        // prepare type variable table
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
        typevars = typevars.dup(currentOwner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
        if (ct.getEnclosingType().tag == CLASS) enterTypevars(ct.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
        // read flags, or skip if this is an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
        long flags = adjustClassFlags(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
        if (c.owner.kind == PCK) c.flags_field = flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
        // read own class name and check that it matches
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
        ClassSymbol self = readClassSymbol(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
        if (c != self)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
            throw badClassFile("class.file.wrong.class",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
                               self.flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
        // class attributes must be read before class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
        // skip ahead to read class attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        int startbp = bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
        nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
        char interfaceCount = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
        bp += interfaceCount * 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
        char fieldCount = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
        for (int i = 0; i < fieldCount; i++) skipMember();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
        char methodCount = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
        for (int i = 0; i < methodCount; i++) skipMember();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
        readClassAttrs(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
        if (readAllOfClassFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
            for (int i = 1; i < poolObj.length; i++) readPool(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
            c.pool = new Pool(poolObj.length, poolObj);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
        // reset and read rest of classinfo
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
        bp = startbp;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
        int n = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
        if (ct.supertype_field == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
            ct.supertype_field = (n == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
                ? Type.noType
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
                : readClassSymbol(n).erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
        n = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        List<Type> is = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
        for (int i = 0; i < n; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
            Type _inter = readClassSymbol(nextChar()).erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
            is = is.prepend(_inter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
        if (ct.interfaces_field == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
            ct.interfaces_field = is.reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
        if (fieldCount != nextChar()) assert false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
        for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
        if (methodCount != nextChar()) assert false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
        for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
        typevars = typevars.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
    /** Read inner class info. For each inner/outer pair allocate a
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
     *  member class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
    void readInnerClasses(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
        int n = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        for (int i = 0; i < n; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
            nextChar(); // skip inner class symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
            ClassSymbol outer = readClassSymbol(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
            Name name = readName(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
            if (name == null) name = names.empty;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
            long flags = adjustClassFlags(nextChar());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
            if (outer != null) { // we have a member class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
                if (name == names.empty)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
                    name = names.one;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
                ClassSymbol member = enterClass(name, outer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
                if ((flags & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
                    ((ClassType)member.type).setEnclosingType(outer.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
                    if (member.erasure_field != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
                        ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
                if (c == outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
                    member.flags_field = flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
                    enterMember(c, member);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
    /** Read a class file.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
    private void readClassFile(ClassSymbol c) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
        int magic = nextInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
        if (magic != JAVA_MAGIC)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
            throw badClassFile("illegal.start.of.class.file");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
        int minorVersion = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
        int majorVersion = nextChar();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
        int maxMajor = Target.MAX().majorVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
        int maxMinor = Target.MAX().minorVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
        if (majorVersion > maxMajor ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
            majorVersion * 1000 + minorVersion <
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            Target.MIN().majorVersion * 1000 + Target.MIN().minorVersion)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
            if (majorVersion == (maxMajor + 1))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
                log.warning("big.major.version",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
                            currentClassFile,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                            majorVersion,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
                            maxMajor);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
                throw badClassFile("wrong.version",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
                                   Integer.toString(majorVersion),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
                                   Integer.toString(minorVersion),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
                                   Integer.toString(maxMajor),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
                                   Integer.toString(maxMinor));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
        else if (checkClassFile &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
                 majorVersion == maxMajor &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
                 minorVersion > maxMinor)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
            printCCF("found.later.version",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
                     Integer.toString(minorVersion));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
        indexPool();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
        if (signatureBuffer.length < bp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
            int ns = Integer.highestOneBit(bp) << 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
            signatureBuffer = new byte[ns];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
        readClass(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
 * Adjusting flags
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
    long adjustFieldFlags(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
        return flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
    long adjustMethodFlags(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
        if ((flags & ACC_BRIDGE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
            flags &= ~ACC_BRIDGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
            flags |= BRIDGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
            if (!allowGenerics)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
                flags &= ~SYNTHETIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
        if ((flags & ACC_VARARGS) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
            flags &= ~ACC_VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
            flags |= VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
        return flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
    long adjustClassFlags(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
        return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
 * Loading Classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
    /** Define a new class given its name and owner.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
    public ClassSymbol defineClass(Name name, Symbol owner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
        ClassSymbol c = new ClassSymbol(0, name, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
        if (owner.kind == PCK)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
            assert classes.get(c.flatname) == null : c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        c.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
        return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
    /** Create a new toplevel or member class symbol with given name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
     *  and owner and enter in `classes' unless already there.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
    public ClassSymbol enterClass(Name name, TypeSymbol owner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        Name flatname = TypeSymbol.formFlatName(name, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
        ClassSymbol c = classes.get(flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
        if (c == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
            c = defineClass(name, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
            classes.put(flatname, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
        } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
            // reassign fields of classes that might have been loaded with
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
            // their flat names.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
            c.owner.members().remove(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
            c.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
            c.owner = owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
            c.fullname = ClassSymbol.formFullName(name, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
        return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
     * Creates a new toplevel class symbol with given flat name and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
     * given class (or source) file.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
     * @param flatName a fully qualified binary class name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
     * @param classFile the class file or compilation unit defining
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
     * the class (may be {@code null})
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
     * @return a newly created class symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
     * @throws AssertionError if the class symbol already exists
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
    public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
        ClassSymbol cs = classes.get(flatName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
        if (cs != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
            String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
                                    cs.fullname,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
                                    cs.completer,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
                                    cs.classfile,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
                                    cs.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
            throw new AssertionError(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
        Name packageName = Convert.packagePart(flatName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
        PackageSymbol owner = packageName.isEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
                                ? syms.unnamedPackage
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
                                : enterPackage(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
        cs = defineClass(Convert.shortName(flatName), owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
        cs.classfile = classFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
        classes.put(flatName, cs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
        return cs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
    /** Create a new member or toplevel class symbol with given flat name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
     *  and enter in `classes' unless already there.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
    public ClassSymbol enterClass(Name flatname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
        ClassSymbol c = classes.get(flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
        if (c == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
            return enterClass(flatname, (JavaFileObject)null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
            return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
    private boolean suppressFlush = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
    /** Completion for classes to be loaded. Before a class is loaded
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
     *  we make sure its enclosing class (if any) is loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
    public void complete(Symbol sym) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
        if (sym.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
            ClassSymbol c = (ClassSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
            c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
            boolean suppressFlush = this.suppressFlush;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
            this.suppressFlush = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
                completeOwners(c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
                completeEnclosing(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
                this.suppressFlush = suppressFlush;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
            fillIn(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
        } else if (sym.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
            PackageSymbol p = (PackageSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
                fillIn(p);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
            } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
                throw new CompletionFailure(sym, ex.getLocalizedMessage()).initCause(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
        if (!filling && !suppressFlush)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
            annotate.flush(); // finish attaching annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
    /** complete up through the enclosing package. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
    private void completeOwners(Symbol o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
        if (o.kind != PCK) completeOwners(o.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
        o.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
     * Tries to complete lexically enclosing classes if c looks like a
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
     * nested class.  This is similar to completeOwners but handles
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
     * the situation when a nested class is accessed directly as it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
     * possible with the Tree API or javax.lang.model.*.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
    private void completeEnclosing(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
        if (c.owner.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
            Symbol owner = c.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
            for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
                Symbol encl = owner.members().lookup(name).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
                if (encl == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
                    encl = classes.get(TypeSymbol.formFlatName(name, owner));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
                if (encl != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
                    encl.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
    /** We can only read a single class file at a time; this
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
     *  flag keeps track of when we are currently reading a class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
     *  file.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
    private boolean filling = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
    /** Fill in definition of class `c' from corresponding class or
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
     *  source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
    private void fillIn(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
        if (completionFailureName == c.fullname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
            throw new CompletionFailure(c, "user-selected completion failure by class name");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
        currentOwner = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
        JavaFileObject classfile = c.classfile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
        if (classfile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
            JavaFileObject previousClassFile = currentClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
                assert !filling :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
                    "Filling " + classfile.toUri() +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
                    " during " + previousClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
                currentClassFile = classfile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
                if (verbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
                    printVerbose("loading", currentClassFile.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
                if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
                    filling = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
                        bp = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
                        buf = readInputStream(buf, classfile.openInputStream());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
                        readClassFile(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
                        if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
                            List<Type> missing = missingTypeVariables;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
                            List<Type> found = foundTypeVariables;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
                            missingTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
                            foundTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
                            filling = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
                            ClassType ct = (ClassType)currentOwner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
                            ct.supertype_field =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
                                types.subst(ct.supertype_field, missing, found);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
                            ct.interfaces_field =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
                                types.subst(ct.interfaces_field, missing, found);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
                        } else if (missingTypeVariables.isEmpty() !=
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
                                   foundTypeVariables.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
                            Name name = missingTypeVariables.head.tsym.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
                            throw badClassFile("undecl.type.var", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
                        missingTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
                        foundTypeVariables = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
                        filling = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
                    if (sourceCompleter != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                        sourceCompleter.complete(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
                        throw new IllegalStateException("Source completer required to read "
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
                                                        + classfile.toUri());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
            } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
                throw badClassFile("unable.to.access.file", ex.getMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
                currentClassFile = previousClassFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
            throw
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
                newCompletionFailure(c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
                                     Log.getLocalizedString("class.file.not.found",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
                                                            c.flatname));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
        private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
                buf = ensureCapacity(buf, s.available());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
                int r = s.read(buf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
                int bp = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
                while (r != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
                    bp += r;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
                    buf = ensureCapacity(buf, bp);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
                    r = s.read(buf, bp, buf.length - bp);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
                return buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
                    s.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
                } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
                    /* Ignore any errors, as this stream may have already
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
                     * thrown a related exception which is the one that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
                     * should be reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
                     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
        private static byte[] ensureCapacity(byte[] buf, int needed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
            if (buf.length < needed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
                byte[] old = buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
                buf = new byte[Integer.highestOneBit(needed) << 1];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
                System.arraycopy(old, 0, buf, 0, old.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
            return buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
        /** Static factory for CompletionFailure objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
         *  In practice, only one can be used at a time, so we share one
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
         *  to reduce the expense of allocating new exception objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
        private CompletionFailure newCompletionFailure(ClassSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
                                                       String localized) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
            if (!cacheCompletionFailure) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
                // log.warning("proc.messager",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
                //             Log.getLocalizedString("class.file.not.found", c.flatname));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
                // c.debug.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
                return new CompletionFailure(c, localized);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
                CompletionFailure result = cachedCompletionFailure;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
                result.sym = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
                result.errmsg = localized;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
        private CompletionFailure cachedCompletionFailure =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
            new CompletionFailure(null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
            cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
    /** Load a toplevel class with given fully qualified name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
     *  The class is entered into `classes' only if load was successful.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
    public ClassSymbol loadClass(Name flatname) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
        boolean absent = classes.get(flatname) == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
        ClassSymbol c = enterClass(flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
        if (c.members_field == null && c.completer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
                c.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
            } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
                if (absent) classes.remove(flatname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
                throw ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
        return c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
/************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
 * Loading Packages
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
 ***********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
    /** Check to see if a package exists, given its fully qualified name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
    public boolean packageExists(Name fullname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
        return enterPackage(fullname).exists();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
    /** Make a package, given its fully qualified name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
    public PackageSymbol enterPackage(Name fullname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
        PackageSymbol p = packages.get(fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
        if (p == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            assert !fullname.isEmpty() : "rootPackage missing!";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
            p = new PackageSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
                Convert.shortName(fullname),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
                enterPackage(Convert.packagePart(fullname)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
            p.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
            packages.put(fullname, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
        return p;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
    /** Make a package, given its unqualified name and enclosing package.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
    public PackageSymbol enterPackage(Name name, PackageSymbol owner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
        return enterPackage(TypeSymbol.formFullName(name, owner));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
    /** Include class corresponding to given class file in package,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
     *  unless (1) we already have one the same kind (.class or .java), or
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
     *         (2) we have one of the other kind, and the given class file
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
     *             is older.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
    protected void includeClassFile(PackageSymbol p, JavaFileObject file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
        if ((p.flags_field & EXISTS) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
            for (Symbol q = p; q != null && q.kind == PCK; q = q.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
                q.flags_field |= EXISTS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
        JavaFileObject.Kind kind = file.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
        int seen;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
        if (kind == JavaFileObject.Kind.CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
            seen = CLASS_SEEN;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
            seen = SOURCE_SEEN;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
        String binaryName = fileManager.inferBinaryName(currentLoc, file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
        int lastDot = binaryName.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
        Name classname = names.fromString(binaryName.substring(lastDot + 1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        boolean isPkgInfo = classname == names.package_info;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
        ClassSymbol c = isPkgInfo
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
            ? p.package_info
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
            : (ClassSymbol) p.members_field.lookup(classname).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
        if (c == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
            c = enterClass(classname, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
            if (c.classfile == null) // only update the file if's it's newly created
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
                c.classfile = file;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
            if (isPkgInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
                p.package_info = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
                if (c.owner == p)  // it might be an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
                    p.members_field.enter(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
        } else if (c.classfile != null && (c.flags_field & seen) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
            // if c.classfile == null, we are currently compiling this class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
            // and no further action is necessary.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
            // if (c.flags_field & seen) != 0, we have already encountered
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
            // a file of the same kind; again no further action is necessary.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
            if ((c.flags_field & (CLASS_SEEN | SOURCE_SEEN)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
                c.classfile = preferredFileObject(file, c.classfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
        c.flags_field |= seen;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
    /** Implement policy to choose to derive information from a source
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
     *  file or a class file when both are present.  May be overridden
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
     *  by subclasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
    protected JavaFileObject preferredFileObject(JavaFileObject a,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
                                           JavaFileObject b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
        if (preferSource)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
            return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
            long adate = a.getLastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
            long bdate = b.getLastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
            // 6449326: policy for bad lastModifiedTime in ClassReader
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
            //assert adate >= 0 && bdate >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
            return (adate > bdate) ? a : b;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
     * specifies types of files to be read when filling in a package symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
    protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
        return EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
     * this is used to support javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
    protected void extraFileActions(PackageSymbol pack, JavaFileObject fe) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
    protected Location currentLoc; // FIXME
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
    private boolean verbosePath = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
    /** Load directory of package into members scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
    private void fillIn(PackageSymbol p) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
        if (p.members_field == null) p.members_field = new Scope(p);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
        String packageName = p.fullname.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
        Set<JavaFileObject.Kind> kinds = getPackageFileKinds();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
        fillIn(p, PLATFORM_CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
               fileManager.list(PLATFORM_CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
                                packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
                                EnumSet.of(JavaFileObject.Kind.CLASS),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
                                false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
        Set<JavaFileObject.Kind> classKinds = EnumSet.copyOf(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
        classKinds.remove(JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
        boolean wantClassFiles = !classKinds.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
        Set<JavaFileObject.Kind> sourceKinds = EnumSet.copyOf(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
        sourceKinds.remove(JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
        boolean wantSourceFiles = !sourceKinds.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
        boolean haveSourcePath = fileManager.hasLocation(SOURCE_PATH);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
        if (verbose && verbosePath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
            if (fileManager instanceof StandardJavaFileManager) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
                StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
                if (haveSourcePath && wantSourceFiles) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
                    List<File> path = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
                    for (File file : fm.getLocation(SOURCE_PATH)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
                        path = path.prepend(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
                    printVerbose("sourcepath", path.reverse().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
                } else if (wantSourceFiles) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
                    List<File> path = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
                    for (File file : fm.getLocation(CLASS_PATH)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
                        path = path.prepend(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
                    printVerbose("sourcepath", path.reverse().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
                if (wantClassFiles) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
                    List<File> path = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
                    for (File file : fm.getLocation(PLATFORM_CLASS_PATH)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
                        path = path.prepend(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
                    for (File file : fm.getLocation(CLASS_PATH)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
                        path = path.prepend(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
                    printVerbose("classpath",  path.reverse().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
        if (wantSourceFiles && !haveSourcePath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
            fillIn(p, CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
                   fileManager.list(CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
                                    packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
                                    kinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
                                    false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
            if (wantClassFiles)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
                fillIn(p, CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
                       fileManager.list(CLASS_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
                                        packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
                                        classKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
                                        false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
            if (wantSourceFiles)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                fillIn(p, SOURCE_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
                       fileManager.list(SOURCE_PATH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
                                        packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
                                        sourceKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
                                        false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
        verbosePath = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
        private void fillIn(PackageSymbol p,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                            Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                            Iterable<JavaFileObject> files)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
            currentLoc = location;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
            for (JavaFileObject fo : files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
                switch (fo.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
                case SOURCE: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
                    // TODO pass binaryName to includeClassFile
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
                    String binaryName = fileManager.inferBinaryName(currentLoc, fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
                    String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
                    if (SourceVersion.isIdentifier(simpleName) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
                        simpleName.equals("package-info"))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
                        includeClassFile(p, fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
                    extraFileActions(p, fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
    /** Output for "-verbose" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
     *  @param key The key to look up the correct internationalized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
     *  @param arg An argument for substitution into the output string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
    private void printVerbose(String key, CharSequence arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
        Log.printLines(log.noticeWriter, Log.getLocalizedString("verbose." + key, arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
    /** Output for "-checkclassfile" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
     *  @param key The key to look up the correct internationalized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
     *  @param arg An argument for substitution into the output string.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
    private void printCCF(String key, Object arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
        Log.printLines(log.noticeWriter, Log.getLocalizedString(key, arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
    public interface SourceCompleter {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
        void complete(ClassSymbol sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
            throws CompletionFailure;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
     * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
     * The attribute is only the last component of the original filename, so is unlikely
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
     * to be valid as is, so operations other than those to access the name throw
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
     * UnsupportedOperationException
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
    private static class SourceFileObject extends BaseFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
        /** The file's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
        private Name name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
        public SourceFileObject(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
        public InputStream openInputStream() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        public OutputStream openOutputStream() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
        public Reader openReader() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
        public Writer openWriter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
        public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
            return name.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
        public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
        public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
            if (!(other instanceof SourceFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
            SourceFileObject o = (SourceFileObject) other;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
            return name.equals(o.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
            return name.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
        public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
            return true; // fail-safe mode
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
        public URI toUri() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
            return URI.create(name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
}