Class ConstantPool

java.lang.Object
serp.bytecode.lowlevel.ConstantPool
All Implemented Interfaces:
VisitAcceptor

public class ConstantPool extends Object implements VisitAcceptor
A bytecode constant pool, containing entries for all strings, constants, classes, etc referenced in the class structure and method opcodes. In keeping with the low-level bytecode representation, all pool indexes are 1-based and LongEntrys and DoubleEntrys each occupy two indexes in the pool.
  • Field Details

    • _entries

      private List _entries
    • _lookup

      private Map _lookup
  • Constructor Details

    • ConstantPool

      public ConstantPool()
      Default constructor.
  • Method Details

    • getEntries

      public Entry[] getEntries()
      Return all the entries in the pool.
    • getEntry

      public Entry getEntry(int index)
      Retrieve the entry at the specified 1-based index.
      Throws:
      IndexOutOfBoundsException - if index is invalid, including the case that it points to the second slot of a long or double entry
    • indexOf

      public int indexOf(Entry entry)
      Return the index of the given entry, or 0 if it is not in the pool.
    • addEntry

      public int addEntry(Entry entry)
      Add an entry to the pool.
      Returns:
      the index at which the entry was added
    • addEntry

      private int addEntry(Object key, Entry entry)
      Add an entry to the pool using the given key.
    • removeEntry

      public boolean removeEntry(Entry entry)
      Remove the given entry from the pool.
      Returns:
      false if the entry is not in the pool, true otherwise
    • clear

      public void clear()
      Clear all entries from the pool.
    • size

      public int size()
      Return the number of places occupied in the pool, including the fact that long and double entries occupy two places.
    • findUTF8Entry

      public int findUTF8Entry(String value, boolean add)
      Return the index of the UTF8Entry with the given value, or 0 if it does not exist.
      Parameters:
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findDoubleEntry

      public int findDoubleEntry(double value, boolean add)
      Return the constant pool index of the DoubleEntry for the given value, or 0 if it does not exist.
      Parameters:
      value - the value to find
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findFloatEntry

      public int findFloatEntry(float value, boolean add)
      Return the constant pool index of the FloatEntry for the given value, or 0 if it does not exist.
      Parameters:
      value - the value to find
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findIntEntry

      public int findIntEntry(int value, boolean add)
      Return the constant pool index of the IntEntry for the given value, or 0 if it does not exist.
      Parameters:
      value - the value to find
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findLongEntry

      public int findLongEntry(long value, boolean add)
      Return the constant pool index of the LongEntry for the given value, or 0 if it does not exist.
      Parameters:
      value - the value to find
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findStringEntry

      public int findStringEntry(String value, boolean add)
      Return the constant pool index of the StringEntry for the given string value, or 0 if it does not exist.
      Parameters:
      value - the value to find
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findClassEntry

      public int findClassEntry(String name, boolean add)
      Return the constant pool index of the ClassEntry for the given class name, or 0 if it does not exist.
      Parameters:
      name - the class name in internal form
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findNameAndTypeEntry

      public int findNameAndTypeEntry(String name, String desc, boolean add)
      Return the constant pool index of the NameAndTypeEntry for the given name and descriptor, or 0 if it does not exist.
      Parameters:
      name - the name of the entity
      desc - the descriptor of the entity in internal form
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findFieldEntry

      public int findFieldEntry(String owner, String name, String desc, boolean add)
      Return the constant pool index of the FieldEntry for the given name, descriptor, and owner class name.
      Parameters:
      owner - the name of the field's owning class in internal form
      name - the name of the field
      desc - the descriptor of the field in internal form
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findMethodEntry

      public int findMethodEntry(String owner, String name, String desc, boolean add)
      Return the constant pool index of the MethodEntry for the given name, descriptor, and owner class name.
      Parameters:
      owner - the name of the method's owning class in internal form
      name - the name of the method
      desc - the descriptor of the method in internal form
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findInterfaceMethodEntry

      public int findInterfaceMethodEntry(String owner, String name, String desc, boolean add)
      Return the constant pool index of the InterfaceMethodEntry for the given name, descriptor, and owner class name.
      Parameters:
      owner - the name of the method's owning class in internal form
      name - the name of the method
      desc - the descriptor of the method in internal form
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • findInvokeDynamicEntry

      public int findInvokeDynamicEntry(int bootstrapMethodIndex, String name, String desc, boolean add)
    • findComplexEntry

      private int findComplexEntry(String owner, String name, String desc, int type, boolean add)
      Return the constant pool index of the ComplexEntry for the given name, descriptor, and owner class name.
      Parameters:
      owner - the name of the owning class in internal form
      name - the name of the entity
      desc - the descriptor of the entity in internal form
      type - the type of entry: field, method, interface method
      add - if true, the entry will be added if it does not already exist, and the new entry's index returned
    • acceptVisit

      public void acceptVisit(BCVisitor visit)
      Description copied from interface: VisitAcceptor
      Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this one.
      Specified by:
      acceptVisit in interface VisitAcceptor
    • read

      public void read(DataInput in) throws IOException
      Fill the constant pool from the given bytecode stream.
      Throws:
      IOException
    • write

      public void write(DataOutput out) throws IOException
      Write the constant pool to the given bytecode stream.
      Throws:
      IOException
    • modifyEntry

      void modifyEntry(Object origKey, Entry entry)
      Called by constant pool entries when they are mutated.
    • find

      private int find(Object key)
      Returns the constant pool index of the entry with the given key.
    • getKey

      static Object getKey(Entry entry)
      Return the hash key used for the specified entry.