package org.aspectj.apache.bcel.classfile; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.generic.*; import java.io.*; /** * This class represents the constant pool, i.e., a table of constants, of * a parsed classfile. It may contain null references, due to the JVM * specification that skips an entry after an 8-byte constant (double, * long) entry. */ public class ConstantPool implements Node { private Constant[] pool; private int poolSize; // number of entries in the pool (could be < pool.length as the array is resized in 'chunks') public int getSize() { return poolSize; } public ConstantPool() { pool = new Constant[10]; poolSize=0; } public ConstantPool(Constant[] constants) { pool = constants; poolSize = (constants==null?0:constants.length); } ConstantPool(DataInputStream file) throws IOException { byte tag; poolSize = file.readUnsignedShort(); pool = new Constant[poolSize]; // pool[0] is unused by the compiler and may be used freely by the implementation for (int i=1; i= pool.length || index < 0) throw new ClassFormatException("Invalid constant pool reference: " + index + ". Constant pool size is: " + pool.length); return pool[index]; } /** * @return deep copy of this constant pool */ public ConstantPool copy() { ConstantPool c = null; Constant[] newConstants = new Constant[poolSize]; // use the correct size for (int i=1;i= pool.length) { Constant[] cs = pool; pool = new Constant[cs.length+8]; System.arraycopy(cs, 0, pool, 0, cs.length); } if (poolSize==0) poolSize = 1; // someone about to do something in here! } public int addFieldref(String class_name, String field_name, String signature) { int ret = lookupFieldref(class_name, field_name, signature); int class_index, name_and_type_index; if (ret != -1) return ret; adjustSize(); class_index = addClass(class_name); name_and_type_index = addNameAndType(field_name, signature); ret = poolSize; pool[poolSize++] = new ConstantFieldref(class_index, name_and_type_index); return ret; } public int lookupFieldref(String searchClassname, String searchFieldname, String searchSignature) { searchClassname = searchClassname.replace('.','/'); for (int i=1;i