FileDocCategorySizeDatePackage
ConstantPool.javaAPI DocApache Ant 1.7011749Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.depend.constantpool

ConstantPool

public class ConstantPool extends Object
The constant pool of a Java class. The constant pool is a collection of constants used in a Java class file. It stores strings, constant values, class names, method names, field names etc.
see
The Java Virtual Machine Specification

Fields Summary
private Vector
entries
The entries in the constant pool.
private Hashtable
utf8Indexes
A Hashtable of UTF8 entries - used to get constant pool indexes of the UTF8 values quickly
Constructors Summary
public ConstantPool()
Initialise the constant pool.

        entries = new Vector();

        // The zero index is never present in the constant pool itself so
        // we add a null entry for it
        entries.addElement(null);

        utf8Indexes = new Hashtable();
    
Methods Summary
public intaddEntry(ConstantPoolEntry entry)
Add an entry to the constant pool.

param
entry the new entry to be added to the constant pool.
return
the index into the constant pool at which the entry is stored.

        int index = entries.size();

        entries.addElement(entry);

        int numSlots = entry.getNumEntries();

        // add null entries for any additional slots required.
        for (int j = 0; j < numSlots - 1; ++j) {
            entries.addElement(null);
        }

        if (entry instanceof Utf8CPInfo) {
            Utf8CPInfo utf8Info = (Utf8CPInfo) entry;

            utf8Indexes.put(utf8Info.getValue(), new Integer(index));
        }

        return index;
    
public intgetClassEntry(java.lang.String className)
Get the index of a given CONSTANT_CLASS entry in the constant pool.

param
className the name of the class for which the class entry index is required.
return
the index at which the given class entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof ClassCPInfo) {
                ClassCPInfo classinfo = (ClassCPInfo) element;

                if (classinfo.getClassName().equals(className)) {
                    index = i;
                }
            }
        }

        return index;
    
public intgetConstantEntry(java.lang.Object constantValue)
Get the index of a given constant value entry in the constant pool.

param
constantValue the constant value for which the index is required.
return
the index at which the given value entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof ConstantCPInfo) {
                ConstantCPInfo constantEntry = (ConstantCPInfo) element;

                if (constantEntry.getValue().equals(constantValue)) {
                    index = i;
                }
            }
        }

        return index;
    
public ConstantPoolEntrygetEntry(int index)
Get an constant pool entry at a particular index.

param
index the index into the constant pool.
return
the constant pool entry at that index.

        return (ConstantPoolEntry) entries.elementAt(index);
    
public intgetFieldRefEntry(java.lang.String fieldClassName, java.lang.String fieldName, java.lang.String fieldType)
Get the index of a given CONSTANT_FIELDREF entry in the constant pool.

param
fieldClassName the name of the class which contains the field being referenced.
param
fieldName the name of the field being referenced.
param
fieldType the type descriptor of the field being referenced.
return
the index at which the given field ref entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof FieldRefCPInfo) {
                FieldRefCPInfo fieldRefEntry = (FieldRefCPInfo) element;

                if (fieldRefEntry.getFieldClassName().equals(fieldClassName)
                     && fieldRefEntry.getFieldName().equals(fieldName)
                     && fieldRefEntry.getFieldType().equals(fieldType)) {
                    index = i;
                }
            }
        }

        return index;
    
public intgetInterfaceMethodRefEntry(java.lang.String interfaceMethodClassName, java.lang.String interfaceMethodName, java.lang.String interfaceMethodType)
Get the index of a given CONSTANT_INTERFACEMETHODREF entry in the constant pool.

param
interfaceMethodClassName the name of the interface which contains the method being referenced.
param
interfaceMethodName the name of the method being referenced.
param
interfaceMethodType the type descriptor of the method being referenced.
return
the index at which the given method ref entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof InterfaceMethodRefCPInfo) {
                InterfaceMethodRefCPInfo interfaceMethodRefEntry
                     = (InterfaceMethodRefCPInfo) element;

                if (interfaceMethodRefEntry.getInterfaceMethodClassName().equals(
                        interfaceMethodClassName)
                     && interfaceMethodRefEntry.getInterfaceMethodName().equals(
                         interfaceMethodName)
                     && interfaceMethodRefEntry.getInterfaceMethodType().equals(
                         interfaceMethodType)) {
                    index = i;
                }
            }
        }

        return index;
    
public intgetMethodRefEntry(java.lang.String methodClassName, java.lang.String methodName, java.lang.String methodType)
Get the index of a given CONSTANT_METHODREF entry in the constant pool.

param
methodClassName the name of the class which contains the method being referenced.
param
methodName the name of the method being referenced.
param
methodType the type descriptor of the method being referenced.
return
the index at which the given method ref entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof MethodRefCPInfo) {
                MethodRefCPInfo methodRefEntry = (MethodRefCPInfo) element;

                if (methodRefEntry.getMethodClassName().equals(methodClassName)
                     && methodRefEntry.getMethodName().equals(methodName)
                     && methodRefEntry.getMethodType().equals(methodType)) {
                    index = i;
                }
            }
        }

        return index;
    
public intgetNameAndTypeEntry(java.lang.String name, java.lang.String type)
Get the index of a given CONSTANT_NAMEANDTYPE entry in the constant pool.

param
name the name
param
type the type
return
the index at which the given NameAndType entry occurs in the constant pool or -1 if the value does not occur.

        int index = -1;

        for (int i = 0; i < entries.size() && index == -1; ++i) {
            Object element = entries.elementAt(i);

            if (element instanceof NameAndTypeCPInfo) {
                NameAndTypeCPInfo nameAndTypeEntry
                    = (NameAndTypeCPInfo) element;

                if (nameAndTypeEntry.getName().equals(name)
                     && nameAndTypeEntry.getType().equals(type)) {
                    index = i;
                }
            }
        }

        return index;
    
public intgetUTF8Entry(java.lang.String value)
Get the index of a given UTF8 constant pool entry.

param
value the string value of the UTF8 entry.
return
the index at which the given string occurs in the constant pool or -1 if the value does not occur.

        int index = -1;
        Integer indexInteger = (Integer) utf8Indexes.get(value);

        if (indexInteger != null) {
            index = indexInteger.intValue();
        }

        return index;
    
public voidread(java.io.DataInputStream classStream)
Read the constant pool from a class input stream.

param
classStream the DataInputStream of a class file.
exception
IOException if there is a problem reading the constant pool from the stream

        int numEntries = classStream.readUnsignedShort();

        for (int i = 1; i < numEntries;) {
            ConstantPoolEntry nextEntry
                 = ConstantPoolEntry.readEntry(classStream);

            i += nextEntry.getNumEntries();

            addEntry(nextEntry);
        }
    
public voidresolve()
Resolve the entries in the constant pool. Resolution of the constant pool involves transforming indexes to other constant pool entries into the actual data for that entry.

        for (Enumeration i = entries.elements(); i.hasMoreElements();) {
            ConstantPoolEntry poolInfo = (ConstantPoolEntry) i.nextElement();

            if (poolInfo != null && !poolInfo.isResolved()) {
                poolInfo.resolve(this);
            }
        }
    
public intsize()
Get the size of the constant pool.

return
the size of the constant pool

        return entries.size();
    
public java.lang.StringtoString()
Dump the constant pool to a string.

return
the constant pool entries as strings

        StringBuffer sb = new StringBuffer("\n");
        int size = entries.size();

        for (int i = 0; i < size; ++i) {
            sb.append("[" + i + "] = " + getEntry(i) + "\n");
        }

        return sb.toString();