jaxp/src/share/classes/com/sun/org/apache/bcel/internal/generic/ConstantPoolGen.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
package com.sun.org.apache.bcel.internal.generic;
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
/* ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * The Apache Software License, Version 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * Redistribution and use in source and binary forms, with or without
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * modification, are permitted provided that the following conditions
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * are met:
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * 1. Redistributions of source code must retain the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 *    notice, this list of conditions and the following disclaimer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 * 2. Redistributions in binary form must reproduce the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 *    notice, this list of conditions and the following disclaimer in
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 *    the documentation and/or other materials provided with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 *    distribution.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
 * 3. The end-user documentation included with the redistribution,
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
 *    if any, must include the following acknowledgment:
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 *       "This product includes software developed by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *        Apache Software Foundation (http://www.apache.org/)."
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 *    Alternately, this acknowledgment may appear in the software itself,
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 *    if and wherever such third-party acknowledgments normally appear.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * 4. The names "Apache" and "Apache Software Foundation" and
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *    "Apache BCEL" must not be used to endorse or promote products
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *    derived from this software without prior written permission. For
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *    written permission, please contact apache@apache.org.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * 5. Products derived from this software may not be called "Apache",
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *    "Apache BCEL", nor may "Apache" appear in their name, without
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *    prior written permission of the Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * SUCH DAMAGE.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * This software consists of voluntary contributions made by many
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * individuals on behalf of the Apache Software Foundation.  For more
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * information on the Apache Software Foundation, please see
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * <http://www.apache.org/>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import com.sun.org.apache.bcel.internal.classfile.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import java.util.HashMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 * This class is used to build up a constant pool. The user adds
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * constants via `addXXX' methods, `addString', `addClass',
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * etc.. These methods return an index into the constant
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * pool. Finally, `getFinalConstantPool()' returns the constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * built up. Intermediate versions of the constant pool can be
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * obtained with `getConstantPool()'. A constant pool has capacity for
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * Constants.MAX_SHORT entries. Note that the first (0) is used by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * JVM and that Double and Long constants need two slots.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
 * @see Constant
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
public class ConstantPoolGen implements java.io.Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
  protected int        size      = 1024; // Inital size, sufficient in most cases
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  protected Constant[] constants = new Constant[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  protected int        index     = 1; // First entry (0) used by JVM
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  private static final String METHODREF_DELIM  = ":";
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  private static final String IMETHODREF_DELIM = "#";
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  private static final String FIELDREF_DELIM   = "&";
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  private static final String NAT_DELIM        = "%";
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  private static class Index implements java.io.Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    int index;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    Index(int i) { index = i; }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   * Initialize with given array of constants.
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   * @param c array of given constants, new ones will be appended
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
  public ConstantPoolGen(Constant[] cs) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    if(cs.length > size) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      size      = cs.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
      constants = new Constant[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    System.arraycopy(cs, 0, constants, 0, cs.length);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    if(cs.length > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      index = cs.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    for(int i=1; i < index; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      Constant c = constants[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      if(c instanceof ConstantString) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        ConstantString s  = (ConstantString)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        ConstantUtf8   u8 = (ConstantUtf8)constants[s.getStringIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        string_table.put(u8.getBytes(), new Index(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
      } else if(c instanceof ConstantClass) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        ConstantClass s  = (ConstantClass)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        ConstantUtf8  u8 = (ConstantUtf8)constants[s.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        class_table.put(u8.getBytes(), new Index(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
      } else if(c instanceof ConstantNameAndType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        ConstantNameAndType n    = (ConstantNameAndType)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        ConstantUtf8        u8   = (ConstantUtf8)constants[n.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        ConstantUtf8        u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        n_a_t_table.put(u8.getBytes() + NAT_DELIM + u8_2.getBytes(), new Index(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
       } else if(c instanceof ConstantUtf8) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
         ConstantUtf8 u = (ConstantUtf8)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
         utf8_table.put(u.getBytes(), new Index(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
      } else if(c instanceof ConstantCP) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        ConstantCP          m     = (ConstantCP)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        ConstantClass       clazz = (ConstantClass)constants[m.getClassIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        ConstantNameAndType n     = (ConstantNameAndType)constants[m.getNameAndTypeIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        ConstantUtf8 u8         = (ConstantUtf8)constants[clazz.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        String       class_name = u8.getBytes().replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        u8 = (ConstantUtf8)constants[n.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        String method_name = u8.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        u8 = (ConstantUtf8)constants[n.getSignatureIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        String signature = u8.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        String delim = METHODREF_DELIM;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        if(c instanceof ConstantInterfaceMethodref)
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
          delim = IMETHODREF_DELIM;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        else if(c instanceof ConstantFieldref)
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
          delim = FIELDREF_DELIM;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        cp_table.put(class_name + delim + method_name + delim + signature, new Index(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   * Initialize with given constant pool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
  public ConstantPoolGen(ConstantPool cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    this(cp.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * Create empty constant pool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
  public ConstantPoolGen() {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  /** Resize internal array of constants.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  protected void adjustSize() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    if(index + 3 >= size) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
      Constant[] cs = constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
      size      *= 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
      constants  = new Constant[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
      System.arraycopy(cs, 0, constants, 0, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  private HashMap string_table = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
   * Look for ConstantString in ConstantPool containing String `str'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
   * @param str String to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  public int lookupString(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    Index index = (Index)string_table.get(str);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   * Add a new String constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   * @param str String to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  public int addString(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
    int ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    if((ret = lookupString(str)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    int utf8 = addUtf8(str);
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    ConstantString s  = new ConstantString(utf8);
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    constants[index++] = s;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    string_table.put(str, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
  private HashMap class_table = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   * Look for ConstantClass in ConstantPool named `str'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
   * @param str String to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
  public int lookupClass(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    Index index = (Index)class_table.get(str.replace('.', '/'));
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
  private int addClass_(String clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
    int    ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    if((ret = lookupClass(clazz)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    ConstantClass c = new ConstantClass(addUtf8(clazz));
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    constants[index++] = c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    class_table.put(clazz, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
   * Add a new Class reference to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   * @param str Class to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
  public int addClass(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    return addClass_(str.replace('.', '/'));
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   * Add a new Class reference to the ConstantPool for a given type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   * @param str Class to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
  public int addClass(ObjectType type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    return addClass(type.getClassName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   * Add a reference to an array class (e.g. String[][]) as needed by MULTIANEWARRAY
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   * instruction, e.g. to the ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
   * @param type type of array class
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
  public int addArrayClass(ArrayType type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    return addClass_(type.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
   * Look for ConstantInteger in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * @param n integer number to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
  public int lookupInteger(int n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
    for(int i=1; i < index; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
      if(constants[i] instanceof ConstantInteger) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
        ConstantInteger c = (ConstantInteger)constants[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
        if(c.getBytes() == n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
          return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   * Add a new Integer constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   * @param n integer number to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
  public int addInteger(int n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    if((ret = lookupInteger(n)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
    constants[index++] = new ConstantInteger(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
   * Look for ConstantFloat in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
   * @param n Float number to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
  public int lookupFloat(float n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
    int bits = Float.floatToIntBits(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    for(int i=1; i < index; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
      if(constants[i] instanceof ConstantFloat) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        ConstantFloat c = (ConstantFloat)constants[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
        if(Float.floatToIntBits(c.getBytes()) == bits)
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
          return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
   * Add a new Float constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
   * @param n Float number to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
  public int addFloat(float n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
    if((ret = lookupFloat(n)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
    constants[index++] = new ConstantFloat(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
  private HashMap utf8_table = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
   * Look for ConstantUtf8 in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
   * @param n Utf8 string to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
  public int lookupUtf8(String n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    Index index = (Index)utf8_table.get(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
   * Add a new Utf8 constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
   * @param n Utf8 string to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
  public int addUtf8(String n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    if((ret = lookupUtf8(n)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
    constants[index++] = new ConstantUtf8(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    utf8_table.put(n, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
   * Look for ConstantLong in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
   * @param n Long number to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
  public int lookupLong(long n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
    for(int i=1; i < index; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
      if(constants[i] instanceof ConstantLong) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
        ConstantLong c = (ConstantLong)constants[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        if(c.getBytes() == n)
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
          return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
   * Add a new long constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
   * @param n Long number to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
  public int addLong(long n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
    if((ret = lookupLong(n)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
    constants[index] = new ConstantLong(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    index += 2;   // Wastes one entry according to spec
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
   * Look for ConstantDouble in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
   * @param n Double number to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
  public int lookupDouble(double n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    long bits = Double.doubleToLongBits(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
    for(int i=1; i < index; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
      if(constants[i] instanceof ConstantDouble) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
        ConstantDouble c = (ConstantDouble)constants[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
        if(Double.doubleToLongBits(c.getBytes()) == bits)
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
          return i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
   * Add a new double constant to the ConstantPool, if it is not already in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
   * @param n Double number to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
  public int addDouble(double n) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
    if((ret = lookupDouble(n)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
    constants[index] = new ConstantDouble(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
    index += 2;   // Wastes one entry according to spec
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
  private HashMap n_a_t_table = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
   * Look for ConstantNameAndType in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
   * @param name of variable/method
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
   * @param signature of variable/method
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
  public int lookupNameAndType(String name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
    Index index = (Index)n_a_t_table.get(name + NAT_DELIM + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
   * Add a new NameAndType constant to the ConstantPool if it is not already
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
   * in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
   * @param n NameAndType string to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
  public int addNameAndType(String name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
    int  name_index, signature_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
    if((ret = lookupNameAndType(name, signature)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
    name_index      = addUtf8(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
    signature_index = addUtf8(signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
    constants[index++] = new ConstantNameAndType(name_index, signature_index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
    n_a_t_table.put(name + NAT_DELIM + signature, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
  private HashMap cp_table = new HashMap();
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
   * Look for ConstantMethodref in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
   * @param class_name Where to find method
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
   * @param method_name Guess what
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
   * @param signature return and argument types
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
  public int lookupMethodref(String class_name, String method_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
    Index index = (Index)cp_table.get(class_name + METHODREF_DELIM + method_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
                                      METHODREF_DELIM + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
  public int lookupMethodref(MethodGen method) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
    return lookupMethodref(method.getClassName(), method.getName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
                          method.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
   * Add a new Methodref constant to the ConstantPool, if it is not already
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
   * in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
   * @param n Methodref string to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
  public int addMethodref(String class_name, String method_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
    int  ret, class_index, name_and_type_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
    if((ret = lookupMethodref(class_name, method_name, signature)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
    name_and_type_index = addNameAndType(method_name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
    class_index         = addClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
    constants[index++] = new ConstantMethodref(class_index, name_and_type_index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
    cp_table.put(class_name + METHODREF_DELIM + method_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
                 METHODREF_DELIM + signature, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
  public int addMethodref(MethodGen method) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
    return addMethodref(method.getClassName(), method.getName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
                        method.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
   * Look for ConstantInterfaceMethodref in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
   * @param class_name Where to find method
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
   * @param method_name Guess what
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
   * @param signature return and argument types
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
  public int lookupInterfaceMethodref(String class_name, String method_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
    Index index = (Index)cp_table.get(class_name + IMETHODREF_DELIM + method_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
                                      IMETHODREF_DELIM + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
  public int lookupInterfaceMethodref(MethodGen method) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
    return lookupInterfaceMethodref(method.getClassName(), method.getName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
                                    method.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
   * Add a new InterfaceMethodref constant to the ConstantPool, if it is not already
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
   * in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
   * @param n InterfaceMethodref string to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
  public int addInterfaceMethodref(String class_name, String method_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
    int ret, class_index, name_and_type_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
    if((ret = lookupInterfaceMethodref(class_name, method_name, signature)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
    class_index         = addClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
    name_and_type_index = addNameAndType(method_name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
    constants[index++] = new ConstantInterfaceMethodref(class_index, name_and_type_index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
    cp_table.put(class_name + IMETHODREF_DELIM + method_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
                 IMETHODREF_DELIM + signature, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
  public int addInterfaceMethodref(MethodGen method) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
    return addInterfaceMethodref(method.getClassName(), method.getName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
                                 method.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
   * Look for ConstantFieldref in ConstantPool.
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
   * @param class_name Where to find method
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
   * @param field_name Guess what
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
   * @param signature return and argument types
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
   * @return index on success, -1 otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
  public int lookupFieldref(String class_name, String field_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
    Index index = (Index)cp_table.get(class_name + FIELDREF_DELIM + field_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
                                      FIELDREF_DELIM + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
    return (index != null)? index.index : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
   * Add a new Fieldref constant to the ConstantPool, if it is not already
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
   * in there.
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
   * @param n Fieldref string to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
   * @return index of entry
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
  public int addFieldref(String class_name, String field_name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
    int  ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
    int  class_index, name_and_type_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
    if((ret = lookupFieldref(class_name, field_name, signature)) != -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
      return ret; // Already in CP
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
    adjustSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
    class_index         = addClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
    name_and_type_index = addNameAndType(field_name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
    ret = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
    constants[index++] = new ConstantFieldref(class_index, name_and_type_index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
    cp_table.put(class_name + FIELDREF_DELIM + field_name + FIELDREF_DELIM + signature, new Index(ret));
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
    return ret;
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
   * @param i index in constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
   * @return constant pool entry at index i
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
  public Constant getConstant(int i) { return constants[i]; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
   * Use with care!
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
   * @param i index in constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
   * @param c new constant pool entry at index i
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
  public void setConstant(int i, Constant c) { constants[i] = c; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
   * @return intermediate constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
  public ConstantPool getConstantPool() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
    return new ConstantPool(constants);
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
   * @return current size of constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
  public int getSize() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
    return index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
   * @return constant pool with proper length
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
  public ConstantPool getFinalConstantPool() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
    Constant[] cs = new Constant[index];
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
    System.arraycopy(constants, 0, cs, 0, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
    return new ConstantPool(cs);
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
   * @return String representation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
  public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
    StringBuffer buf = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
    for(int i=1; i < index; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
      buf.append(i + ")" + constants[i] + "\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
  /** Import constant from another ConstantPool and return new index.
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
  public int addConstant(Constant c, ConstantPoolGen cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
    Constant[] constants = cp.getConstantPool().getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
    switch(c.getTag()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
    case Constants.CONSTANT_String: {
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
      ConstantString s  = (ConstantString)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
      ConstantUtf8   u8 = (ConstantUtf8)constants[s.getStringIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
      return addString(u8.getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
    case Constants.CONSTANT_Class: {
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
      ConstantClass s  = (ConstantClass)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
      ConstantUtf8  u8 = (ConstantUtf8)constants[s.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
      return addClass(u8.getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
    case Constants.CONSTANT_NameAndType: {
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
      ConstantNameAndType n    = (ConstantNameAndType)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
      ConstantUtf8        u8   = (ConstantUtf8)constants[n.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
      ConstantUtf8        u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
      return addNameAndType(u8.getBytes(), u8_2.getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
    case Constants.CONSTANT_Utf8:
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
      return addUtf8(((ConstantUtf8)c).getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
    case Constants.CONSTANT_Double:
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
      return addDouble(((ConstantDouble)c).getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
    case Constants.CONSTANT_Float:
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
      return addFloat(((ConstantFloat)c).getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
    case Constants.CONSTANT_Long:
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
      return addLong(((ConstantLong)c).getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
    case Constants.CONSTANT_Integer:
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
      return addInteger(((ConstantInteger)c).getBytes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
    case Constants.CONSTANT_InterfaceMethodref: case Constants.CONSTANT_Methodref:
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
    case Constants.CONSTANT_Fieldref: {
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
      ConstantCP          m     = (ConstantCP)c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
      ConstantClass       clazz = (ConstantClass)constants[m.getClassIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
      ConstantNameAndType n     = (ConstantNameAndType)constants[m.getNameAndTypeIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
      ConstantUtf8 u8           = (ConstantUtf8)constants[clazz.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
      String       class_name   = u8.getBytes().replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
      u8 = (ConstantUtf8)constants[n.getNameIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
      String name = u8.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
      u8 = (ConstantUtf8)constants[n.getSignatureIndex()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
      String signature = u8.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
      switch(c.getTag()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
      case Constants.CONSTANT_InterfaceMethodref:
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
        return addInterfaceMethodref(class_name, name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
      case Constants.CONSTANT_Methodref:
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
        return addMethodref(class_name, name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
      case Constants.CONSTANT_Fieldref:
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
        return addFieldref(class_name, name, signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
      default: // Never reached
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
        throw new RuntimeException("Unknown constant type " + c);
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
    default: // Never reached
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
      throw new RuntimeException("Unknown constant type " + c);
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
}