jdk/src/share/classes/com/sun/java/util/jar/pack/ClassReader.java
author ksrini
Mon, 29 Nov 2010 13:38:12 -0800
changeset 7192 445c518364c4
parent 7171 ee97f78e7482
child 7795 98021fc612af
permissions -rw-r--r--
7003227: (pack200) intermittent failures compiling pack200 Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
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
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    28
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    29
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.Entry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    31
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    32
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.java.util.jar.pack.Package.InnerClass;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    35
import java.io.DataInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    36
import java.io.FilterInputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    37
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    38
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    39
import java.util.ArrayList;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 7171
diff changeset
    40
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Reader for a class file that is being incorporated into a package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class ClassReader implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    Package pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    Class cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    long inPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    DataInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    Map attrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    Map attrCommands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    String unknownAttrCommand = "error";;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    ClassReader(Class cls, InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.pkg = cls.getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.cls = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.verbose = pkg.verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.in = new DataInputStream(new FilterInputStream(in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                int nr = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (nr >= 0)  inPos += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                int ch = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (ch >= 0)  inPos += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                long ns = super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (ns >= 0)  inPos += ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void setAttrDefs(Map attrDefs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.attrDefs = attrDefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void setAttrCommands(Map attrCommands) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.attrCommands = attrCommands;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private void skip(int n, String what) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Utils.log.warning("skipping "+n+" bytes of "+what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        long skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (skipped < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            long j = in.skip(n - skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            assert(j > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            skipped += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        assert(skipped == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /** Read a 2-byte int, and return the <em>global</em> CP entry for it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Entry readRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int i = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return i == 0 ? null : cls.cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private Entry readRef(byte tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        Entry e = readRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        assert(e != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        assert(e.tagMatches(tag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Entry readRefOrNull(byte tag) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Entry e = readRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        assert(e == null || e.tagMatches(tag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private Utf8Entry readUtf8Ref() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return (Utf8Entry) readRef(CONSTANT_Utf8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private ClassEntry readClassRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (ClassEntry) readRef(CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private ClassEntry readClassRefOrNull() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return (ClassEntry) readRefOrNull(CONSTANT_Class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private SignatureEntry readSignatureRef() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // The class file stores a Utf8, but we want a Signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Entry e = readRef(CONSTANT_Utf8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return ConstantPool.getSignatureEntry(e.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            readMagicNumbers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            readConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            readHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            readMembers(false);  // fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            readMembers(true);   // methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            readAttributes(ATTR_CONTEXT_CLASS, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            cls.finishReading();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            assert(0 >= in.read(new byte[1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (verbose > 0) Utils.log.warning("Erroneous data at input offset "+inPos+" of "+cls.file);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    void readMagicNumbers() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        cls.magic = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (cls.magic != JAVA_MAGIC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new Attribute.FormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                ("Bad magic number in class file "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 +Integer.toHexString(cls.magic),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                 ATTR_CONTEXT_CLASS, "magic-number", "pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        cls.minver = (short) readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        cls.majver = (short) readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //System.out.println("ClassFile.version="+cls.majver+"."+cls.minver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String bad = checkVersion(cls.majver, cls.minver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (bad != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new Attribute.FormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ("classfile version too "+bad+": "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                 +cls.majver+"."+cls.minver+" in "+cls.file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                 ATTR_CONTEXT_CLASS, "version", "pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private String checkVersion(int majver, int minver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (majver < pkg.min_class_majver ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            (majver == pkg.min_class_majver &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             minver < pkg.min_class_minver)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return "small";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (majver > pkg.max_class_majver ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            (majver == pkg.max_class_majver &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
             minver > pkg.max_class_minver)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return "large";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return null;  // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    void readConstantPool() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int length = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        //System.err.println("reading CP, length="+length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int[] fixups = new int[length*4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int fptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Entry[] cpMap = new Entry[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        cpMap[0] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (int i = 1; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            //System.err.println("reading CP elt, i="+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            int tag = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                case CONSTANT_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    cpMap[i] = ConstantPool.getUtf8Entry(in.readUTF());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        Comparable val = new Integer(in.readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        cpMap[i] = ConstantPool.getLiteralEntry(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        Comparable val = new Float(in.readFloat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        cpMap[i] = ConstantPool.getLiteralEntry(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        Comparable val = new Long(in.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        cpMap[i] = ConstantPool.getLiteralEntry(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        cpMap[++i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        Comparable val = new Double(in.readDouble());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        cpMap[i] = ConstantPool.getLiteralEntry(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        cpMap[++i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // just read the refs; do not attempt to resolve while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                case CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    fixups[fptr++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    fixups[fptr++] = -1;  // empty ref2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    fixups[fptr++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    fixups[fptr++] = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                default:
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   257
                    throw new ClassFormatException("Bad constant pool tag " +
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   258
                            tag + " in File: " + cls.file.nameString +
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   259
                            " at pos: " + inPos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // Fix up refs, which might be out of order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        while (fptr > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                Utils.log.fine("CP fixups ["+fptr/4+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int flimit = fptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            fptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (int fi = 0; fi < flimit; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                int cpi = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                int tag = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                int ref = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                int ref2 = fixups[fi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    Utils.log.fine("  cp["+cpi+"] = "+ConstantPool.tagName(tag)+"{"+ref+","+ref2+"}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (cpMap[ref] == null || ref2 >= 0 && cpMap[ref2] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    // Defer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    fixups[fptr++] = cpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    fixups[fptr++] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    fixups[fptr++] = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    fixups[fptr++] = ref2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    cpMap[cpi] = ConstantPool.getClassEntry(cpMap[ref].stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                case CONSTANT_String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    cpMap[cpi] = ConstantPool.getStringEntry(cpMap[ref].stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    ClassEntry      mclass = (ClassEntry)      cpMap[ref];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    DescriptorEntry mdescr = (DescriptorEntry) cpMap[ref2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    cpMap[cpi] = ConstantPool.getMemberEntry((byte)tag, mclass, mdescr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    Utf8Entry mname = (Utf8Entry) cpMap[ref];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    Utf8Entry mtype = (Utf8Entry) cpMap[ref2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    cpMap[cpi] = ConstantPool.getDescriptorEntry(mname, mtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            assert(fptr < flimit);  // Must make progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        cls.cpMap = cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    void readHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        cls.flags = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        cls.thisClass = readClassRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        cls.superClass = readClassRefOrNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int ni = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        cls.interfaces = new ClassEntry[ni];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        for (int i = 0; i < ni; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            cls.interfaces[i] = readClassRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    void readMembers(boolean doMethods) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int nm = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        for (int i = 0; i < nm; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            readMember(doMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    void readMember(boolean doMethod) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int    mflags = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Utf8Entry       mname = readUtf8Ref();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        SignatureEntry  mtype = readSignatureRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        DescriptorEntry descr = ConstantPool.getDescriptorEntry(mname, mtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Class.Member m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (!doMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            m = cls.new Field(mflags, descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            m = cls.new Method(mflags, descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        readAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                       m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    void readAttributes(int ctype, Attribute.Holder h) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int na = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (na == 0)  return;  // nothing to do here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            Utils.log.fine("readAttributes "+h+" ["+na+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        for (int i = 0; i < na; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            String name = readUtf8Ref().stringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            int length = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // See if there is a special command that applies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (attrCommands != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                Object lkey = Attribute.keyForLookup(ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                String cmd = (String) attrCommands.get(lkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (cmd == "pass") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    String message = "passing attribute bitwise in "+h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                        cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } else if (cmd == "error") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    String message = "attribute not allowed in "+h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                                        cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                } else if (cmd == "strip") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    skip(length, name+" attribute in "+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // Find canonical instance of the requested attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            Attribute a = Attribute.lookup(Package.attrDefs, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                Utils.log.fine("pkg_attribute_lookup "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                a = Attribute.lookup(this.attrDefs, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    Utils.log.fine("this "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                a = Attribute.lookup(null, ctype, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (verbose > 4 && a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    Utils.log.fine("null_attribute_lookup "+name+" = "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (a == null && length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // Any zero-length attr is "known"...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                // We can assume an empty attr. has an empty layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                // Handles markers like Enum, Bridge, Synthetic, Deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                a = Attribute.find(ctype, name, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            boolean isStackMap = (ctype == ATTR_CONTEXT_CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                  && (name.equals("StackMap") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                      name.equals("StackMapX")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (isStackMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // Known attribute but with a corner case format, "pass" it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                Code code = (Code) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                final int TOO_BIG = 0x10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (code.max_stack   >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    code.max_locals  >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    code.getLength() >= TOO_BIG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    name.endsWith("X")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    // No, we don't really know what to do this this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    // Do not compress the rare and strange "u4" and "X" cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                if (isStackMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    // Known attribute but w/o a format; pass it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    String message = "unsupported StackMap variant in "+h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                                        "pass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                } else if (unknownAttrCommand == "strip") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    // Skip the unknown attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    skip(length, "unknown "+name+" attribute in "+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                } else {
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   416
                    String message = " is unknown attribute in class " + h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    throw new Attribute.FormatException(message, ctype, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                                        unknownAttrCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (a.layout() == Package.attrCodeEmpty ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                a.layout() == Package.attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // These are hardwired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                long pos0 = inPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (a.name() == "Code") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    Class.Method m = (Class.Method) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    m.code = new Code(m);
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   428
                    try {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   429
                        readCode(m.code);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   430
                    } catch (Instruction.FormatException iie) {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   431
                        String message = iie.getMessage() + " in " + h;
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   432
                        throw new ClassReader.ClassFormatException(message, iie);
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   433
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    assert(h == cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    readInnerClasses(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                assert(length == inPos - pos0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                // Keep empty attribute a...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                byte[] bytes = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                in.readFully(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                a = a.addContent(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
6901
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   445
            if (a.size() == 0 && !a.layout().isEmpty()) {
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   446
                throw new ClassFormatException(name +
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   447
                        ": attribute length cannot be zero, in " + h);
68f3d74dbda1 6746111: Improve pack200 error message
ksrini
parents: 6900
diff changeset
   448
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            h.addAttribute(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                Utils.log.fine("read "+a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    void readCode(Code code) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        code.max_stack = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        code.max_locals = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        code.bytes = new byte[readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        in.readFully(code.bytes);
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   460
        Instruction.opcodeChecker(code.bytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int nh = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        code.setHandlerCount(nh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        for (int i = 0; i < nh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            code.handler_start[i] = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            code.handler_end[i]   = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            code.handler_catch[i] = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            code.handler_class[i] = readClassRefOrNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        readAttributes(ATTR_CONTEXT_CODE, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    void readInnerClasses(Class cls) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        int nc = readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        ArrayList ics = new ArrayList(nc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        for (int i = 0; i < nc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            InnerClass ic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                new InnerClass(readClassRef(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                               readClassRefOrNull(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                               (Utf8Entry)readRefOrNull(CONSTANT_Utf8),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                               readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            ics.add(ic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        cls.innerClasses = ics;  // set directly; do not use setInnerClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // (Later, ics may be transferred to the pkg.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   486
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   487
    static class ClassFormatException extends IOException {
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   488
        public ClassFormatException(String message) {
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   489
            super(message);
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   490
        }
7171
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   491
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   492
        public ClassFormatException(String message, Throwable cause) {
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   493
            super(message, cause);
ee97f78e7482 6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents: 6901
diff changeset
   494
        }
6900
a3ca67586333 6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents: 5506
diff changeset
   495
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
}