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