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