jdk/src/share/classes/com/sun/java/util/jar/pack/Package.java
author ksrini
Sat, 19 Jun 2010 17:42:39 -0700
changeset 5811 f4d1f45c0058
parent 5506 202f599c92aa
child 6313 470912c9e214
permissions -rw-r--r--
6712743: pack200: should default to 150.7 pack format for classfiles without any classes. Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5811
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.java.util.jar.pack.ConstantPool.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Define the main data structure transmitted by pack/unpack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class Package implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    int verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        PropMap pmap = Utils.currentPropMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (pmap != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            verbose = pmap.getInteger(Utils.DEBUG_VERBOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int magic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int package_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int package_majver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int default_modtime = NO_MODTIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int default_options = 0;  // FO_DEFLATE_HINT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    short default_class_majver = -1; // fill in later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    short default_class_minver = 0;  // fill in later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // These fields can be adjusted by driver properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    short min_class_majver = JAVA_MIN_CLASS_MAJOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    short min_class_minver = JAVA_MIN_CLASS_MINOR_VERSION;
5811
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
    60
    short max_class_majver = JAVA7_MAX_CLASS_MAJOR_VERSION;
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
    61
    short max_class_minver = JAVA7_MAX_CLASS_MINOR_VERSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    short observed_max_class_majver = min_class_majver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    short observed_max_class_minver = min_class_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // What constants are used in this unit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    ConstantPool.IndexGroup cp = new ConstantPool.IndexGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    Package() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        magic          = JAVA_PACKAGE_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        package_minver = -1;  // fill in later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        package_majver = 0;   // fill in later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        cp = new ConstantPool.IndexGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        classes.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        files.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int getPackageVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return (package_majver << 16) + (int)package_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Special empty versions of Code and InnerClasses, used for markers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final Attribute.Layout attrCodeEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static final Attribute.Layout attrInnerClassesEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final Attribute.Layout attrSourceFileSpecial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final Map attrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        HashMap ad = new HashMap(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        attrCodeEmpty = Attribute.define(ad, ATTR_CONTEXT_METHOD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                         "Code", "").layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        attrInnerClassesEmpty = Attribute.define(ad, ATTR_CONTEXT_CLASS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                                 "InnerClasses", "").layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        attrSourceFileSpecial = Attribute.define(ad, ATTR_CONTEXT_CLASS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                 "SourceFile", "RUNH").layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        attrDefs = Collections.unmodifiableMap(ad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int getDefaultClassVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return (default_class_majver << 16) + (char)default_class_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Return the highest version number of all classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *  or 0 if there are no classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    int getHighestClassVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int res = 0;  // initial low value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            Class cls = (Class) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int ver = cls.getVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (res < ver)  res = ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Convenience function to choose an archive version based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  on the class file versions observed within the archive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void choosePackageVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        assert(package_majver <= 0);  // do not call this twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int classver = getHighestClassVersion();
5811
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   125
        if (classver == 0 || (classver >>> 16) < JAVA6_MAX_CLASS_MAJOR_VERSION) {
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   126
            // There are only old classfiles in this segment or resources
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            package_majver = JAVA5_PACKAGE_MAJOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            package_minver = JAVA5_PACKAGE_MINOR_VERSION;
5811
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   129
        } else if ((classver >>> 16) == JAVA6_MAX_CLASS_MAJOR_VERSION) {
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   130
            package_majver = JAVA6_PACKAGE_MAJOR_VERSION;
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   131
            package_minver = JAVA6_PACKAGE_MINOR_VERSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
5811
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   133
            // Normal case.  Use the newest archive format, when available
f4d1f45c0058 6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents: 5506
diff changeset
   134
            // TODO: replace the following with JAVA7* when the need arises
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            package_majver = JAVA6_PACKAGE_MAJOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            package_minver = JAVA6_PACKAGE_MINOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // What Java classes are in this unit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // Fixed 6211177, converted to throw IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void checkVersion() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (magic != JAVA_PACKAGE_MAGIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            String gotMag = Integer.toHexString(magic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            String expMag = Integer.toHexString(JAVA_PACKAGE_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new IOException("Unexpected package magic number: got "+gotMag+"; expected "+expMag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ((package_majver != JAVA6_PACKAGE_MAJOR_VERSION  &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             package_majver != JAVA5_PACKAGE_MAJOR_VERSION) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             (package_minver != JAVA6_PACKAGE_MINOR_VERSION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
             package_minver != JAVA5_PACKAGE_MINOR_VERSION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            String gotVer = package_majver+"."+package_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            String expVer = JAVA6_PACKAGE_MAJOR_VERSION+"."+JAVA6_PACKAGE_MINOR_VERSION+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            " OR "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            JAVA5_PACKAGE_MAJOR_VERSION+"."+JAVA5_PACKAGE_MINOR_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IOException("Unexpected package minor version: got "+gotVer+"; expected "+expVer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    ArrayList classes = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public List getClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    class Class extends Attribute.Holder implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public Package getPackage() { return Package.this; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Optional file characteristics and data source (a "class stub")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        File file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // File header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int magic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        short minver, majver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Local constant pool (one-way mapping of index => package cp).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Entry[] cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // Class header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        //int flags;  // in Attribute.Holder.this.flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        ClassEntry thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ClassEntry superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        ClassEntry[] interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // Class parts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ArrayList fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ArrayList methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        //ArrayList attributes;  // in Attribute.Holder.this.attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Note that InnerClasses may be collected at the package level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ArrayList innerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Class(int flags, ClassEntry thisClass, ClassEntry superClass, ClassEntry[] interfaces) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            this.magic      = JAVA_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            this.minver     = default_class_minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            this.majver     = default_class_majver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            this.flags      = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            this.thisClass  = thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            this.superClass = superClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            this.interfaces = interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            boolean added = classes.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            assert(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Class(String classFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // A blank class; must be read with a ClassReader, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            initFile(newStub(classFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        List getFields() { return fields == null ? noFields : fields; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        List getMethods() { return methods == null ? noMethods : methods; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return thisClass.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return (majver << 16) + (char)minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String getVersionString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return versionStringOf(majver, minver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Note:  equals and hashCode are identity-based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Class that = (Class)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            String n0 = this.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            String n1 = that.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return n0.compareTo(n1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        String getObviousSourceFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return Package.getObviousSourceFile(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        private void transformSourceFile(boolean minimize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // Replace "obvious" SourceFile by null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Attribute olda = getAttribute(attrSourceFileSpecial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (olda == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return;  // no SourceFile attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            String obvious = getObviousSourceFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            ArrayList ref = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            olda.visitRefs(this, VRM_PACKAGE, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Utf8Entry sfName = (Utf8Entry) ref.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Attribute a = olda;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (sfName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (minimize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    // A pair of zero bytes.  Cannot use predef. layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    a = Attribute.find(ATTR_CONTEXT_CLASS, "SourceFile", "H");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    a = a.addContent(new byte[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    // Expand null attribute to the obvious string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    byte[] bytes = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    sfName = getRefString(obvious);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    Object f = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    f = Fixups.add(f, bytes, 0, Fixups.U2_FORMAT, sfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    a = attrSourceFileSpecial.addContent(bytes, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            } else if (obvious.equals(sfName.stringValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (minimize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    // Replace by an all-zero attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    a = attrSourceFileSpecial.addContent(new byte[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (a != olda) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    Utils.log.fine("recoding obvious SourceFile="+obvious);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                List newAttrs = new ArrayList(getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                int where = newAttrs.indexOf(olda);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                newAttrs.set(where, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                setAttributes(newAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        void minimizeSourceFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            transformSourceFile(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        void expandSourceFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            transformSourceFile(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        protected Entry[] getCPMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        protected void setCPMap(Entry[] cpMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            this.cpMap = cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        boolean hasInnerClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return innerClasses != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        List getInnerClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return innerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public void setInnerClasses(Collection ics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            innerClasses = (ics == null) ? null : new ArrayList(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // Edit the attribute list, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            Attribute a = getAttribute(attrInnerClassesEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (innerClasses != null && a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                addAttribute(attrInnerClassesEmpty.canonicalInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            else if (innerClasses == null && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                removeAttribute(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /** Given a global map of ICs (keyed by thisClass),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         *  compute the subset of its Map.values which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         *  required to be present in the local InnerClasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         *  attribute.  Perform this calculation without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         *  reference to any actual InnerClasses attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         *  The order of the resulting list is consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         *  with that of Package.this.allInnerClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        public List computeGloballyImpliedICs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            HashSet cpRefs = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            {   // This block temporarily displaces this.innerClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ArrayList innerClassesSaved = innerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                innerClasses = null;  // ignore for the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                visitRefs(VRM_CLASSIC, cpRefs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                innerClasses = innerClassesSaved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            ConstantPool.completeReferencesIn(cpRefs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            HashSet icRefs = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            for (Iterator i = cpRefs.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                Entry e = (Entry) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // Restrict cpRefs to InnerClasses entries only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (!(e instanceof ClassEntry))  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // For every IC reference, add its outers also.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    InnerClass ic = getGlobalInnerClass(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if (ic == null)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    if (!icRefs.add(e))  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    e = ic.outerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // If we add A$B$C to the mix, we must also add A$B.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // This loop is structured this way so as to accumulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // entries into impliedICs in an order which reflects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // the order of allInnerClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            ArrayList impliedICs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            for (Iterator i = allInnerClasses.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                InnerClass ic = (InnerClass) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                // This one is locally relevant if it describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // a member of the current class, or if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                // class uses it somehow.  In the particular case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                // where thisClass is an inner class, it will already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                // be a member of icRefs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (icRefs.contains(ic.thisClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    || ic.outerClass == this.thisClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    // Add every relevant class to the IC attribute:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        Utils.log.fine("Relevant IC: "+ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    impliedICs.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return impliedICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // Helper for both minimizing and expanding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // Computes a symmetric difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        private List computeICdiff() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            List impliedICs = computeGloballyImpliedICs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            List actualICs  = getInnerClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (actualICs == null)  actualICs = Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // Symmetric difference is calculated from I, A like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            //  diff = (I+A) - (I*A)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            // Note that the center C is unordered, but the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // preserves the original ordering of I and A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // Class file rules require that outers precede inners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // So, add I before A, in case A$B$Z is local, but A$B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // is implicit.  The reverse is never the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (actualICs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return impliedICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // Diff is I since A is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (impliedICs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                return actualICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                // Diff is A since I is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // (I*A) is non-trivial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            HashSet center = new HashSet(actualICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            center.retainAll(new HashSet(impliedICs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            impliedICs.addAll(actualICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            impliedICs.removeAll(center);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // Diff is now I^A = (I+A)-(I*A).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return impliedICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        /** When packing, anticipate the effect of expandLocalICs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         *  Replace the local ICs by their symmetric difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         *  with the globally implied ICs for this class; if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         *  difference is empty, remove the local ICs altogether.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         *  An empty local IC attribute is reserved to signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         *  the unpacker to delete the attribute altogether,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         *  so a missing local IC attribute signals the unpacker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         *  to use the globally implied ICs changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        void minimizeLocalICs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            List diff = computeICdiff();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            List actualICs = innerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            List localICs;  // will be the diff, modulo edge cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (diff.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                // No diff, so transmit no attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                localICs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (actualICs != null && actualICs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    // Odd case:  No implied ICs, and a zero length attr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    // Do not support it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        Utils.log.info("Warning: Dropping empty InnerClasses attribute from "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            } else if (actualICs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // No local IC attribute, even though some are implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                // Signal with trivial attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                localICs = Collections.EMPTY_LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // Transmit a non-empty diff, which will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // a local ICs attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                localICs = diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // Reduce the set to the symmetric difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            setInnerClasses(localICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (verbose > 1 && localICs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                Utils.log.fine("keeping local ICs in "+this+": "+localICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        /** When unpacking, undo the effect of minimizeLocalICs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         *  Must return negative if any IC tuples may have been deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         *  Otherwise, return positive if any IC tuples were added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        int expandLocalICs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            List localICs = innerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            List actualICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            int changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (localICs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                // Diff was empty.  (Common case.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                List impliedICs = computeGloballyImpliedICs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (impliedICs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    actualICs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    changed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    actualICs = impliedICs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    changed = 1;  // added more tuples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } else if (localICs.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // It was a non-empty diff, but the local ICs were absent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                actualICs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                changed = 0;  // [] => null, no tuple change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                // Non-trivial diff was transmitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                actualICs = computeICdiff();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                // If we only added more ICs, return +1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                changed = actualICs.containsAll(localICs)? +1: -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            setInnerClasses(actualICs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        public abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        class Member extends Attribute.Holder implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            DescriptorEntry descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            protected Member(int flags, DescriptorEntry descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                this.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                this.descriptor = descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            public Class thisClass() { return Class.this; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            public DescriptorEntry getDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                return descriptor.nameRef.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                return descriptor.typeRef.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            protected Entry[] getCPMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                return cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (verbose > 2)  Utils.log.fine("visitRefs "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                // Careful:  The descriptor is used by the package,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                // but the classfile breaks it into component refs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (mode == VRM_CLASSIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    refs.add(descriptor.nameRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    refs.add(descriptor.typeRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    refs.add(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                // Handle attribute list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                super.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                return Class.this + "." + descriptor.prettyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        class Field extends Member {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            // Order is significant for fields:  It is visible to reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            int order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            public Field(int flags, DescriptorEntry descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                super(flags, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                assert(!descriptor.isMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                if (fields == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    fields = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                boolean added = fields.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                assert(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                order = fields.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            public byte getLiteralTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                return descriptor.getLiteralTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                Field that = (Field)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return this.order - that.order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        class Method extends Member {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // Code attribute is specially hardwired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            Code code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            public Method(int flags, DescriptorEntry descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                super(flags, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                assert(descriptor.isMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (methods == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    methods = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                boolean added = methods.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                assert(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                super.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (code != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    code.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            public int getArgumentSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                int argSize  = descriptor.typeRef.computeSize(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                int thisSize = Modifier.isStatic(flags) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return thisSize + argSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            // Sort methods in a canonical order (by type, then by name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                Method that = (Method)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                return this.getDescriptor().compareTo(that.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            public void strip(String attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (attrName == "Code")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    code = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (code != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    code.strip(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                super.strip(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                super.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (code != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    if (mode == VRM_CLASSIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        refs.add(getRefString("Code"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    code.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            super.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            for (int isM = 0; isM <= 1; isM++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                ArrayList members = (isM == 0) ? fields : methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                if (members == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                members.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                for (Iterator i = members.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    Member m = (Member)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    m.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (innerClasses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                innerClasses.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        public void strip(String attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (attrName == "InnerClass")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                innerClasses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            for (int isM = 0; isM <= 1; isM++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                ArrayList members = (isM == 0) ? fields : methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (members == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                for (Iterator i = members.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    Member m = (Member)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    m.strip(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            super.strip(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (verbose > 2)  Utils.log.fine("visitRefs "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            refs.add(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            refs.add(superClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                refs.add(interfaces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            for (int isM = 0; isM <= 1; isM++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                ArrayList members = (isM == 0) ? fields : methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                if (members == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                for (Iterator i = members.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    Member m = (Member)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        m.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            Utils.log.warning("Error scanning "+m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            visitInnerClassRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // Handle attribute list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            super.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        protected void visitInnerClassRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            Package.visitInnerClassRefs(innerClasses, mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        // Hook called by ClassReader when it's done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        void finishReading() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            maybeChooseFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        public void initFile(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            assert(this.file == null);  // set-once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                // Build a trivial stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                file = newStub(canonicalFileName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            this.file = file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            assert(file.isClassStub());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            file.stubClass = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            maybeChooseFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public void maybeChooseFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (thisClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                return;  // do not choose yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            String canonName = canonicalFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (file.nameString.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                file.nameString = canonName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (file.nameString.equals(canonName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                // The file name is predictable.  Transmit "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                file.name = getRefString("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            // If name has not yet been looked up, find it now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            if (file.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                file.name = getRefString(file.nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        public String canonicalFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (thisClass == null)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return thisClass.stringValue() + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        public java.io.File getFileName(java.io.File parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            String name = file.name.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if (name.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                name = canonicalFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            String fname = name.replace('/', java.io.File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return new java.io.File(parent, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        public java.io.File getFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return getFileName(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return thisClass.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    void addClass(Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        assert(c.getPackage() == this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        boolean added = classes.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        assert(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        // Make sure the class is represented in the total file order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (c.file == null)  c.initFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        addFile(c.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    // What non-class files are in this unit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    ArrayList files = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    public List getFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public List getClassStubs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        ArrayList classStubs = new ArrayList(classes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            Class cls = (Class) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            assert(cls.file.isClassStub());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            classStubs.add(cls.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return classStubs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    class File implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        String nameString;  // true name of this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        Utf8Entry name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        int modtime = NO_MODTIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        int options = 0;  // random flag bits, such as deflate_hint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        Class stubClass;  // if this is a stub, here's the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        ArrayList prepend = new ArrayList();  // list of byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        java.io.ByteArrayOutputStream append = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        File(Utf8Entry name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            this.nameString = name.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // caller must fill in contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        File(String nameString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            nameString = fixupFileName(nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            this.name = getRefString(nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            this.nameString = name.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // JAR directory.  Useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return nameString.endsWith("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        public boolean isClassStub() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return (options & FO_IS_CLASS_STUB) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        public Class getStubClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            assert(isClassStub());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            assert(stubClass != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return stubClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public boolean isTrivialClassStub() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            return isClassStub()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                && name.stringValue().equals("")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                && (modtime == NO_MODTIME || modtime == default_modtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                && (options &~ FO_IS_CLASS_STUB) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        // The nameString is the key.  Ignore other things.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        // (Note:  The name might be "", in the case of a trivial class stub.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            File that = (File)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return that.nameString == this.nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return nameString.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // Simple alphabetic sort.  PackageWriter uses a better comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            File that = (File)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return this.nameString.compareTo(that.nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            return nameString+"{"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                +(isClassStub()?"*":"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                +(BandStructure.testBit(options,FO_DEFLATE_HINT)?"@":"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                +(modtime==NO_MODTIME?"":"M"+modtime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                +(getFileLength()==0?"":"["+getFileLength()+"]")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                +"}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        public java.io.File getFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            return getFileName(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        public java.io.File getFileName(java.io.File parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            String name = this.nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            //if (name.startsWith("./"))  name = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            String fname = name.replace('/', java.io.File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            return new java.io.File(parent, fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        public void addBytes(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            addBytes(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        public void addBytes(byte[] bytes, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (((append.size() | len) << 2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                prepend.add(append.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                append.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            append.write(bytes, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        public long getFileLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            long len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (prepend == null && append == null)  return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            for (Iterator i = prepend.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                byte[] block = (byte[]) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                len += block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            len += append.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public void writeTo(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (prepend == null && append == null)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            for (Iterator i = prepend.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                byte[] block = (byte[]) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                out.write(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            append.writeTo(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        public void readFrom(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            byte[] buf = new byte[1 << 16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            while ((nr = in.read(buf)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                addBytes(buf, 0, nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        public InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            InputStream in = new ByteArrayInputStream(append.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (prepend.size() == 0)  return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            ArrayList isa = new ArrayList(prepend.size()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            for (Iterator i = prepend.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                byte[] bytes = (byte[]) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                isa.add(new ByteArrayInputStream(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            isa.add(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return new SequenceInputStream(Collections.enumeration(isa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            assert(name != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            refs.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    File newStub(String classFileNameString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        File stub = new File(classFileNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        stub.options |= FO_IS_CLASS_STUB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        stub.prepend = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        stub.append = null;  // do not collect data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private static String fixupFileName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        String fname = name.replace(java.io.File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (fname.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            throw new IllegalArgumentException("absolute file name "+fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return fname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    void addFile(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        boolean added = files.add(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        assert(added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    // Is there a globally declared table of inner classes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    ArrayList allInnerClasses = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    HashMap   allInnerClassesByThis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    List getAllInnerClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return allInnerClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    void setAllInnerClasses(Collection ics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        assert(ics != allInnerClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        allInnerClasses.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        allInnerClasses.addAll(ics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // Make an index:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        allInnerClassesByThis = new HashMap(allInnerClasses.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        for (Iterator i = allInnerClasses.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            InnerClass ic = (InnerClass) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            Object pic = allInnerClassesByThis.put(ic.thisClass, ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            assert(pic == null);  // caller must ensure key uniqueness!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /** Return a global inner class record for the given thisClass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    InnerClass getGlobalInnerClass(Entry thisClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        assert(thisClass instanceof ClassEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return (InnerClass) allInnerClassesByThis.get(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    class InnerClass implements Comparable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        final ClassEntry thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        final ClassEntry outerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        final Utf8Entry name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        final int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // Can name and outerClass be derived from thisClass?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        final boolean predictable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        // About 30% of inner classes are anonymous (in rt.jar).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // About 60% are class members; the rest are named locals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // Nearly all have predictable outers and names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        InnerClass(ClassEntry thisClass, ClassEntry outerClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                   Utf8Entry name, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            this.thisClass = thisClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            this.outerClass = outerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            this.flags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            this.predictable = computePredictable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        private boolean computePredictable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            //System.out.println("computePredictable "+outerClass+" "+this.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            String[] parse = parseInnerClassName(thisClass.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            if (parse == null)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            String pkgOuter = parse[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            //String number = parse[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            String name     = parse[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            String haveName  = (this.name == null)  ? null : this.name.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            String haveOuter = (outerClass == null) ? null : outerClass.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            boolean predictable = (name == haveName && pkgOuter == haveOuter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            //System.out.println("computePredictable => "+predictable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            return predictable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (o == null)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            InnerClass that = (InnerClass)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return eq(this.thisClass, that.thisClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                && eq(this.outerClass, that.outerClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                && eq(this.name, that.name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                && this.flags == that.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        private static boolean eq(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            return (x == null)? y == null: x.equals(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return thisClass.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        public int compareTo(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            InnerClass that = (InnerClass)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return this.thisClass.compareTo(that.thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            refs.add(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (mode == VRM_CLASSIC || !predictable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                // If the name can be demangled, the package omits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                // the products of demangling.  Otherwise, include them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                refs.add(outerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                refs.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            return thisClass.stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    // Helper for building InnerClasses attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    void visitInnerClassRefs(Collection innerClasses, int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (innerClasses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            return;  // no attribute; nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (mode == VRM_CLASSIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            refs.add(getRefString("InnerClasses"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        if (innerClasses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            // Count the entries themselves:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            for (Iterator i = innerClasses.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                InnerClass c = (InnerClass) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                c.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    static String[] parseInnerClassName(String n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        //System.out.println("parseInnerClassName "+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        String pkgOuter, number, name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        int dollar1, dollar2;  // pointers to $ in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        // parse n = (<pkg>/)*<outer>($<number>)?($<name>)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        int nlen = n.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        int pkglen = lastIndexOf(SLASH_MIN,  SLASH_MAX,  n, n.length()) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        dollar2    = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, n.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        if (dollar2 < pkglen)  return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (isDigitString(n, dollar2+1, nlen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            // n = (<pkg>/)*<outer>$<number>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            number = n.substring(dollar2+1, nlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            dollar1 = dollar2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        } else if ((dollar1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, dollar2-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                   > pkglen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                   && isDigitString(n, dollar1+1, dollar2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            // n = (<pkg>/)*<outer>$<number>$<name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            number = n.substring(dollar1+1, dollar2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            name = n.substring(dollar2+1, nlen).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            // n = (<pkg>/)*<outer>$<name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            dollar1 = dollar2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            number = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            name = n.substring(dollar2+1, nlen).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        if (number == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            pkgOuter = n.substring(0, dollar1).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            pkgOuter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        //System.out.println("parseInnerClassName parses "+pkgOuter+" "+number+" "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        return new String[] { pkgOuter, number, name };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    private static final int SLASH_MIN = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    private static final int SLASH_MAX = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    private static final int DOLLAR_MIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    private static final int DOLLAR_MAX = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        assert(lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, "x$$y$", 4) == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        assert(lastIndexOf(SLASH_MIN,  SLASH_MAX,  "x//y/", 4) == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    private static int lastIndexOf(int chMin, int chMax, String str, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        for (int i = pos; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            int ch = str.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            if (ch >= chMin && ch <= chMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    private static boolean isDigitString(String x, int beg, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if (beg == end)  return false;  // null string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        for (int i = beg; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            char ch = x.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (!(ch >= '0' && ch <= '9'))  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    static String getObviousSourceFile(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        String n = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        int pkglen = lastIndexOf(SLASH_MIN,  SLASH_MAX,  n, n.length()) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        n = n.substring(pkglen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        int cutoff = n.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            // Work backwards, finding all '$', '#', etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            int dollar2 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, cutoff-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            if (dollar2 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            cutoff = dollar2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            if (cutoff == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        String obvious = n.substring(0, cutoff)+".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        return obvious;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        assert(getObviousSourceFile("foo").equals("foo.java"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        assert(getObviousSourceFile("foo/bar").equals("bar.java"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        assert(getObviousSourceFile("foo/bar$baz").equals("bar.java"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        assert(getObviousSourceFile("foo/bar#baz#1").equals("bar.java"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        assert(getObviousSourceFile("foo.bar.baz#1").equals("baz.java"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    static Utf8Entry getRefString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        return ConstantPool.getUtf8Entry(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    static LiteralEntry getRefLiteral(Comparable s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        return ConstantPool.getLiteralEntry(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    void stripAttributeKind(String what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        // what is one of { Debug, Compile, Constant, Exceptions, InnerClasses }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (verbose > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            Utils.log.info("Stripping "+what.toLowerCase()+" data and attributes...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (what == "Debug") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            strip("SourceFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            strip("LineNumberTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            strip("LocalVariableTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            strip("LocalVariableTypeTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (what == "Compile") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // Keep the inner classes normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // Although they have no effect on execution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            // the Reflection API exposes them, and JCK checks them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // NO: // strip("InnerClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            strip("Deprecated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            strip("Synthetic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (what == "Exceptions") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            // Keep the exceptions normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            // Although they have no effect on execution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            // the Reflection API exposes them, and JCK checks them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            strip("Exceptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (what == "Constant") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            stripConstantFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        classes.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            Class c = (Class)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            c.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        files.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    public void strip(String attrName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            Class c = (Class)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            c.strip(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public static String versionStringOf(int majver, int minver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        return majver+"."+minver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public static String versionStringOf(int version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return versionStringOf(version >>> 16, (char)version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    public void stripConstantFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            Class c = (Class) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            for (Iterator j = c.fields.iterator(); j.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                Class.Field f = (Class.Field) j.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                if (Modifier.isFinal(f.flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    // do not strip non-static finals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    && Modifier.isStatic(f.flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                    && f.getAttribute("ConstantValue") != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                    && !f.getName().startsWith("serial")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                        Utils.log.fine(">> Strip "+this+" ConstantValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                        j.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    protected void visitRefs(int mode, Collection refs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            Class c = (Class)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            c.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (mode != VRM_CLASSIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            for (Iterator i = files.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                File f = (File)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                f.visitRefs(mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            visitInnerClassRefs(allInnerClasses, mode, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    // Use this before writing the package file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    // It sorts files into a new order which seems likely to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    // compress better.  It also moves classes to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    // file order.  It also removes JAR directory entries, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    // are useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    void reorderFiles(boolean keepClassOrder, boolean stripDirectories) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        // First reorder the classes, if that is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (!keepClassOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            // In one test with rt.jar, this trick gained 0.7%
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            Collections.sort(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        // Remove stubs from resources; maybe we'll add them on at the end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // if there are some non-trivial ones.  The best case is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // modtimes and options are not transmitted, and the stub files
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        // for class files do not need to be transmitted at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // Also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        List stubs = getClassStubs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        for (Iterator i = files.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            File file = (File) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            if (file.isClassStub() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                (stripDirectories && file.isDirectory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        // Sort the remaining non-class files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        // We sort them by file type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        // This keeps files of similar format near each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // Put class files at the end, keeping their fixed order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        // Be sure the JAR file's required manifest stays at the front. (4893051)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        Collections.sort(files, new Comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                public int compare(Object o0, Object o1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    File r0 = (File) o0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    File r1 = (File) o1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    // Get the file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    String f0 = r0.nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    String f1 = r1.nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    if (f0.equals(f1)) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    if (JarFile.MANIFEST_NAME.equals(f0))  return 0-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    if (JarFile.MANIFEST_NAME.equals(f1))  return 1-0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    // Extract file basename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    String n0 = f0.substring(1+f0.lastIndexOf('/'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    String n1 = f1.substring(1+f1.lastIndexOf('/'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    // Extract basename extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    String x0 = n0.substring(1+n0.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    String x1 = n1.substring(1+n1.lastIndexOf('.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    int r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    // Primary sort key is file extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    r = x0.compareTo(x1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    if (r != 0)  return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    r = f0.compareTo(f1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // Add back the class stubs after sorting, before trimStubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        files.addAll(stubs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    void trimStubs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        // Restore enough non-trivial stubs to carry the needed class modtimes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        for (ListIterator i = files.listIterator(files.size()); i.hasPrevious(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            File file = (File) i.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            if (!file.isTrivialClassStub()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    Utils.log.fine("Keeping last non-trivial "+file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                Utils.log.fine("Removing trivial "+file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        if (verbose > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            Utils.log.info("Transmitting "+files.size()+" files, including per-file data for "+getClassStubs().size()+" classes out of "+classes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    // Use this before writing the package file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    void buildGlobalConstantPool(Set requiredEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            Utils.log.fine("Checking for unused CP entries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        requiredEntries.add(getRefString(""));  // uconditionally present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        visitRefs(VRM_PACKAGE, requiredEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        ConstantPool.completeReferencesIn(requiredEntries, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            Utils.log.fine("Sorting CP entries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        Index   cpAllU = ConstantPool.makeIndex("unsorted", requiredEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        Index[] byTagU = ConstantPool.partitionByTag(cpAllU);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        for (int i = 0; i < ConstantPool.TAGS_IN_ORDER.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            byte tag = ConstantPool.TAGS_IN_ORDER[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            // Work on all entries of a given kind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            Index ix = byTagU[tag];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            if (ix == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            ConstantPool.sort(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            cp.initIndexByTag(tag, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            byTagU[tag] = null;  // done with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        for (int i = 0; i < byTagU.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            assert(byTagU[i] == null);  // all consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        for (int i = 0; i < ConstantPool.TAGS_IN_ORDER.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            byte tag = ConstantPool.TAGS_IN_ORDER[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            Index ix = cp.getIndexByTag(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            assert(ix.assertIsSorted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            if (verbose > 2)  Utils.log.fine(ix.dumpString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    // Use this before writing the class files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    void ensureAllClassFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        HashSet fileSet = new HashSet(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        for (Iterator i = classes.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            Class cls = (Class) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            // Add to the end of ths list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            if (!fileSet.contains(cls.file))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                files.add(cls.file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    static final List noObjects = Arrays.asList(new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    static final List noFields = Arrays.asList(new Class.Field[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    static final List noMethods = Arrays.asList(new Class.Method[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    static final List noInnerClasses = Arrays.asList(new InnerClass[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
}