jdk/src/share/classes/sun/tools/java/BinaryConstantPool.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 25522 10d789df41bb
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16854
diff changeset
     2
 * Copyright (c) 1994, 2013, 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 sun.tools.java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class is used to represent a constant table once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * it is read from a class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class BinaryConstantPool implements Constants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private byte types[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Object cpool[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    BinaryConstantPool(DataInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // JVM 4.1 ClassFile.constant_pool_count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        types = new byte[in.readUnsignedShort()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        cpool = new Object[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        for (int i = 1 ; i < cpool.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            int j = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // JVM 4.4 cp_info.tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            switch(types[i] = in.readByte()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
              case CONSTANT_UTF8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                cpool[i] = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
              case CONSTANT_INTEGER:
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
    63
                cpool[i] = in.readInt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
              case CONSTANT_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                cpool[i] = new Float(in.readFloat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
              case CONSTANT_LONG:
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 23010
diff changeset
    69
                cpool[i++] = in.readLong();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
              case CONSTANT_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                cpool[i++] = new Double(in.readDouble());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
              case CONSTANT_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
              case CONSTANT_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // JVM 4.4.3 CONSTANT_String_info.string_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                // or JVM 4.4.1 CONSTANT_Class_info.name_index
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
    79
                cpool[i] =in.readUnsignedShort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
              case CONSTANT_FIELD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
              case CONSTANT_METHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
              case CONSTANT_INTERFACEMETHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
              case CONSTANT_NAMEANDTYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                // JVM 4.4.2 CONSTANT_*ref_info.class_index & name_and_type_index
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
    87
                cpool[i] = (in.readUnsignedShort() << 16) | in.readUnsignedShort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
16854
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    90
              case CONSTANT_METHODHANDLE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    91
                cpool[i] = readBytes(in, 3);
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    92
                break;
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    93
              case CONSTANT_METHODTYPE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    94
                cpool[i] = readBytes(in, 2);
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    95
                break;
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    96
              case CONSTANT_INVOKEDYNAMIC:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    97
                cpool[i] = readBytes(in, 4);
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    98
                break;
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
              case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                throw new ClassFormatError("invalid constant type: " + (int)types[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
16854
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   107
    private byte[] readBytes(DataInputStream in, int cnt) throws IOException {
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   108
        byte[] b = new byte[cnt];
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   109
        in.readFully(b);
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   110
        return b;
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   111
    }
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   112
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * get a integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public int getInteger(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return (n == 0) ? 0 : ((Number)cpool[n]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * get a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public Object getValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (n == 0) ? null : cpool[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * get a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public String getString(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (n == 0) ? null : (String)cpool[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * get an identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public Identifier getIdentifier(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (n == 0) ? null : Identifier.lookup(getString(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * get class declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public ClassDeclaration getDeclarationFromName(Environment env, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (n == 0) ? null : env.getClassDeclaration(Identifier.lookup(getString(n).replace('/','.')));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * get class declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public ClassDeclaration getDeclaration(Environment env, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return (n == 0) ? null : getDeclarationFromName(env, getInteger(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * get a type from a type signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Type getType(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return Type.tType(getString(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * get the type of constant given an index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int getConstantType(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return types[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * get the n-th constant from the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public Object getConstant(int n, Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int constant_type = getConstantType(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        switch (constant_type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            case CONSTANT_INTEGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            case CONSTANT_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            case CONSTANT_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            case CONSTANT_DOUBLE:
16854
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   179
            case CONSTANT_METHODHANDLE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   180
            case CONSTANT_METHODTYPE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   181
            case CONSTANT_INVOKEDYNAMIC:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return getValue(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            case CONSTANT_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return getDeclaration(env, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            case CONSTANT_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                return getString(getInteger(n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            case CONSTANT_FIELD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            case CONSTANT_METHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            case CONSTANT_INTERFACEMETHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    int key = getInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    ClassDefinition clazz =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        getDeclaration(env, key >> 16).getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    int name_and_type = getInteger(key & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    Identifier id = getIdentifier(name_and_type >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    Type type = getType(name_and_type & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    for (MemberDefinition field = clazz.getFirstMatch(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                         field != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                         field = field.getNextMatch()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        Type field_type = field.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        if ((constant_type == CONSTANT_FIELD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            ? (field_type == type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            : (field_type.equalArguments(type)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                throw new ClassFormatError("invalid constant type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                              constant_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Get a list of dependencies, ie: all the classes referenced in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * constant pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   225
    public Vector<ClassDeclaration> getDependencies(Environment env) {
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   226
        Vector<ClassDeclaration> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        for (int i = 1 ; i < cpool.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            switch(types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
              case CONSTANT_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                v.addElement(getDeclarationFromName(env, getInteger(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   237
    Hashtable<Object, Integer> indexHashObject;
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   238
    Hashtable<Object, Integer> indexHashAscii;
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   239
    Vector<String> MoreStuff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Find the index of an Object in the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public int indexObject(Object obj, Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (indexHashObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            createIndexHash(env);
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   247
        Integer result = indexHashObject.get(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new IndexOutOfBoundsException("Cannot find object " + obj + " of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                obj.getClass() + " in constant pool");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return result.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Find the index of an ascii string in the constant pool.  If it's not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * the constant pool, then add it at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int indexString(String string, Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (indexHashObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            createIndexHash(env);
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   261
        Integer result = indexHashAscii.get(string);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (result == null) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   263
            if (MoreStuff == null) MoreStuff = new Vector<>();
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   264
            result = cpool.length + MoreStuff.size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            MoreStuff.addElement(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            indexHashAscii.put(string, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return result.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Create a hash table of all the items in the constant pool that could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * possibly be referenced from the outside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public void createIndexHash(Environment env) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   277
        indexHashObject = new Hashtable<>();
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   278
        indexHashAscii = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        for (int i = 1; i < cpool.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (types[i] == CONSTANT_UTF8) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   281
                indexHashAscii.put(cpool[i], i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                try {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25186
diff changeset
   284
                    indexHashObject.put(getConstant(i, env), i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                } catch (ClassFormatError e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Write out the contents of the constant pool, including any additions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * that have been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public void write(DataOutputStream out, Environment env) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int length = cpool.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (MoreStuff != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            length += MoreStuff.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        out.writeShort(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        for (int i = 1 ; i < cpool.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int type = types[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            Object x = cpool[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            out.writeByte(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                case CONSTANT_UTF8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    out.writeUTF((String) x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                case CONSTANT_INTEGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    out.writeInt(((Number)x).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                case CONSTANT_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    out.writeFloat(((Number)x).floatValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                case CONSTANT_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    out.writeLong(((Number)x).longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                case CONSTANT_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    out.writeDouble(((Number)x).doubleValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                case CONSTANT_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                case CONSTANT_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    out.writeShort(((Number)x).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                case CONSTANT_FIELD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                case CONSTANT_METHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                case CONSTANT_INTERFACEMETHOD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                case CONSTANT_NAMEANDTYPE: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    int value = ((Number)x).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    out.writeShort(value >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    out.writeShort(value & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
16854
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   335
                case CONSTANT_METHODHANDLE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   336
                case CONSTANT_METHODTYPE:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   337
                case CONSTANT_INVOKEDYNAMIC:
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   338
                    out.write((byte[])x, 0, ((byte[])x).length);
9371f5046d02 8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents: 5506
diff changeset
   339
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                     throw new ClassFormatError("invalid constant type: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                                   + (int)types[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        for (int i = cpool.length; i < length; i++) {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 25522
diff changeset
   346
            String string = MoreStuff.elementAt(i - cpool.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            out.writeByte(CONSTANT_UTF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            out.writeUTF(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}