jdk/src/share/classes/com/sun/java/util/jar/pack/ClassWriter.java
author ksrini
Fri, 17 Aug 2012 08:28:48 -0700
changeset 13577 afcb909fea86
parent 12857 0a5f341c2a28
permissions -rw-r--r--
7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
     2
 * Copyright (c) 2001, 2012, 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: 5506
diff changeset
    28
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import com.sun.java.util.jar.pack.ConstantPool.Entry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import com.sun.java.util.jar.pack.ConstantPool.Index;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import com.sun.java.util.jar.pack.ConstantPool.NumberEntry;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    32
import com.sun.java.util.jar.pack.ConstantPool.MethodHandleEntry;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    33
import com.sun.java.util.jar.pack.ConstantPool.BootstrapMethodEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.java.util.jar.pack.Package.Class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.java.util.jar.pack.Package.InnerClass;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    36
import java.io.BufferedOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    37
import java.io.ByteArrayOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    38
import java.io.DataOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    39
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    40
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    41
import java.util.List;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    42
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Writer for a class file that is incorporated into a package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    47
class ClassWriter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    Package pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Class cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    DataOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    Index cpIndex;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    54
    Index bsmIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    ClassWriter(Class cls, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.pkg = cls.getPackage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.cls = cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.verbose = pkg.verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.out = new DataOutputStream(new BufferedOutputStream(out));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.cpIndex = ConstantPool.makeIndex(cls.toString(), cls.getCPMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.cpIndex.flattenSigs = true;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    63
        if (cls.hasBootstrapMethods()) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    64
            this.bsmIndex = ConstantPool.makeIndex(cpIndex.debugName+".BootstrapMethods",
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    65
                                                   cls.getBootstrapMethodMap());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    66
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Utils.log.fine("local CP="+(verbose > 2 ? cpIndex.dumpString() : cpIndex.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void writeShort(int x) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        out.writeShort(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private void writeInt(int x) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        out.writeInt(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Write a 2-byte int representing a CP entry, using the local cpIndex. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private void writeRef(Entry e) throws IOException {
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    81
        writeRef(e, cpIndex);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    82
    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    83
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    84
    /** Write a 2-byte int representing a CP entry, using the given cpIndex. */
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
    85
    private void writeRef(Entry e, Index cpIndex) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int i = (e == null) ? 0 : cpIndex.indexOf(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        writeShort(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    void write() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (verbose > 1)  Utils.log.fine("...writing "+cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            writeMagicNumbers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            writeConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            writeHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            writeMembers(false);  // fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            writeMembers(true);   // methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            writeAttributes(ATTR_CONTEXT_CLASS, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            /* Closing here will cause all the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
               streams to close, Causing the jar stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
               to close prematurely, instead we just flush.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
               out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                Utils.log.warning("Error on output of "+cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    void writeMagicNumbers() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        writeInt(cls.magic);
12857
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   116
        writeShort(cls.version.minor);
0a5f341c2a28 7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present
ksrini
parents: 12544
diff changeset
   117
        writeShort(cls.version.major);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    void writeConstantPool() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Entry[] cpMap = cls.cpMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        writeShort(cpMap.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < cpMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Entry e = cpMap[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            assert((e == null) == (i == 0 || cpMap[i-1] != null && cpMap[i-1].isDoubleWord()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (e == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            byte tag = e.getTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (verbose > 2)  Utils.log.fine("   CP["+i+"] = "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            out.write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                case CONSTANT_Signature:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   132
                    throw new AssertionError("CP should have Signatures remapped to Utf8");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                case CONSTANT_Utf8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    out.writeUTF(e.stringValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                case CONSTANT_Integer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    out.writeInt(((NumberEntry)e).numberValue().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                case CONSTANT_Float:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    float fval = ((NumberEntry)e).numberValue().floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    out.writeInt(Float.floatToRawIntBits(fval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                case CONSTANT_Long:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    out.writeLong(((NumberEntry)e).numberValue().longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                case CONSTANT_Double:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    double dval = ((NumberEntry)e).numberValue().doubleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    out.writeLong(Double.doubleToRawLongBits(dval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                case CONSTANT_Class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                case CONSTANT_String:
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   152
                case CONSTANT_MethodType:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    writeRef(e.getRef(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   155
                case CONSTANT_MethodHandle:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   156
                    MethodHandleEntry mhe = (MethodHandleEntry) e;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   157
                    out.writeByte(mhe.refKind);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   158
                    writeRef(mhe.getRef(0));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   159
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                case CONSTANT_Fieldref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                case CONSTANT_Methodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                case CONSTANT_InterfaceMethodref:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                case CONSTANT_NameandType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    writeRef(e.getRef(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    writeRef(e.getRef(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    break;
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   167
                case CONSTANT_InvokeDynamic:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   168
                    writeRef(e.getRef(0), bsmIndex);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   169
                    writeRef(e.getRef(1));
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   170
                    break;
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   171
                case CONSTANT_BootstrapMethod:
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   172
                    throw new AssertionError("CP should have BootstrapMethods moved to side-table");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    throw new IOException("Bad constant pool tag "+tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    void writeHeader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        writeShort(cls.flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        writeRef(cls.thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        writeRef(cls.superClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        writeShort(cls.interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < cls.interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            writeRef(cls.interfaces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    void writeMembers(boolean doMethods) throws IOException {
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   190
        List<? extends Class.Member> mems;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!doMethods)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            mems = cls.getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            mems = cls.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        writeShort(mems.size());
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   196
        for (Class.Member m : mems) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            writeMember(m, doMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void writeMember(Class.Member m, boolean doMethod) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (verbose > 2)  Utils.log.fine("writeMember "+m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        writeShort(m.flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        writeRef(m.getDescriptor().nameRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        writeRef(m.getDescriptor().typeRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        writeAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
13577
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   210
    private void reorderBSMandICS(Attribute.Holder h) {
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   211
        Attribute bsmAttr = h.getAttribute(Package.attrBootstrapMethodsEmpty);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   212
        if (bsmAttr == null) return;
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   213
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   214
        Attribute icsAttr = h.getAttribute(Package.attrInnerClassesEmpty);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   215
        if (icsAttr == null) return;
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   216
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   217
        int bsmidx = h.attributes.indexOf(bsmAttr);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   218
        int icsidx = h.attributes.indexOf(icsAttr);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   219
        if (bsmidx > icsidx) {
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   220
            h.attributes.remove(bsmAttr);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   221
            h.attributes.add(icsidx, bsmAttr);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   222
        }
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   223
        return;
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   224
    }
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   225
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // handy buffer for collecting attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    ByteArrayOutputStream buf    = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    DataOutputStream      bufOut = new DataOutputStream(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    void writeAttributes(int ctype, Attribute.Holder h) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (h.attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            writeShort(0);  // attribute size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
13577
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   235
        // there may be cases if an InnerClass attribute is explicit, then the
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   236
        // ordering could be wrong, fix the ordering before we write it out.
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   237
        if (h instanceof Package.Class)
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   238
            reorderBSMandICS(h);
afcb909fea86 7190750: (pack200) the java unpacker produces non spec. compliant classfile with lambda classfiles
ksrini
parents: 12857
diff changeset
   239
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        writeShort(h.attributes.size());
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   241
        for (Attribute a : h.attributes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            a.finishRefs(cpIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            writeRef(a.getNameRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (a.layout() == Package.attrCodeEmpty ||
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   245
                a.layout() == Package.attrBootstrapMethodsEmpty ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                a.layout() == Package.attrInnerClassesEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // These are hardwired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                DataOutputStream savedOut = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                assert(out != bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                buf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                out = bufOut;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   252
                if ("Code".equals(a.name())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    Class.Method m = (Class.Method) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    writeCode(m.code);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   255
                } else if ("BootstrapMethods".equals(a.name())) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   256
                    assert(h == cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   257
                    writeBootstrapMethods(cls);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   258
                } else if ("InnerClasses".equals(a.name())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    assert(h == cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    writeInnerClasses(cls);
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   261
                } else {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   262
                    throw new AssertionError();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                out = savedOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    Utils.log.fine("Attribute "+a.name()+" ["+buf.size()+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                writeInt(buf.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                buf.writeTo(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    Utils.log.fine("Attribute "+a.name()+" ["+a.size()+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                writeInt(a.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                out.write(a.bytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    void writeCode(Code code) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        code.finishRefs(cpIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        writeShort(code.max_stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        writeShort(code.max_locals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        writeInt(code.bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        out.write(code.bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int nh = code.getHandlerCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        writeShort(nh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (int i = 0; i < nh; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             writeShort(code.handler_start[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             writeShort(code.handler_end[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             writeShort(code.handler_catch[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
             writeRef(code.handler_class[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        writeAttributes(ATTR_CONTEXT_CODE, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
12544
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   295
    void writeBootstrapMethods(Class cls) throws IOException {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   296
        List<BootstrapMethodEntry> bsms = cls.getBootstrapMethods();
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   297
        writeShort(bsms.size());
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   298
        for (BootstrapMethodEntry e : bsms) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   299
            writeRef(e.bsmRef);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   300
            writeShort(e.argRefs.length);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   301
            for (Entry argRef : e.argRefs) {
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   302
                writeRef(argRef);
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   303
            }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   304
        }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   305
    }
5768f2e096de 6981776: Pack200 must support -target 7 bytecodes
ksrini
parents: 10115
diff changeset
   306
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    void writeInnerClasses(Class cls) throws IOException {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   308
        List<InnerClass> ics = cls.getInnerClasses();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        writeShort(ics.size());
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   310
        for (InnerClass ic : ics) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            writeRef(ic.thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            writeRef(ic.outerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            writeRef(ic.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            writeShort(ic.flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}