Class ConstantPool

java.lang.Object
org.apache.bcel.classfile.ConstantPool
All Implemented Interfaces:
Cloneable, Iterable<Constant>, Node

public class ConstantPool extends Object implements Cloneable, Node, Iterable<Constant>
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. Those interested in generating constant pools programmatically should see ConstantPoolGen.
See Also:
  • Constructor Details

    • ConstantPool

      public ConstantPool(Constant[] constantPool)
      Parameters:
      constantPool - Array of constants
    • ConstantPool

      public ConstantPool(DataInput input) throws IOException
      Reads constants from given input stream.
      Parameters:
      input - Input stream
      Throws:
      IOException - if problem in readUnsignedShort or readConstant
  • Method Details

    • accept

      public void accept(Visitor v)
      Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
      Specified by:
      accept in interface Node
      Parameters:
      v - Visitor object
    • constantToString

      Resolves constant to a string representation.
      Parameters:
      c - Constant to be printed
      Returns:
      String representation
      Throws:
      IllegalArgumentException - if c is unknown constant type
    • constantToString

      public String constantToString(int index, byte tag)
      Retrieves constant at 'index' from constant pool and resolve it to a string representation.
      Parameters:
      index - of constant in constant pool
      tag - expected type
      Returns:
      String representation
    • copy

      public ConstantPool copy()
      Returns:
      deep copy of this constant pool
    • dump

      public void dump(DataOutputStream file) throws IOException
      Dump constant pool to file stream in binary format.
      Parameters:
      file - Output file stream
      Throws:
      IOException - if problem in writeShort or dump
    • getConstant

      public <T extends Constant> T getConstant(int index) throws ClassFormatException
      Gets constant from constant pool.
      Parameters:
      index - Index in constant pool
      Returns:
      Constant value
      Throws:
      ClassFormatException - if index is invalid
      See Also:
    • getConstant

      public <T extends Constant> T getConstant(int index, byte tag) throws ClassFormatException
      Gets constant from constant pool and check whether it has the expected type.
      Parameters:
      index - Index in constant pool
      tag - Tag of expected constant, i.e., its type
      Returns:
      Constant value
      Throws:
      ClassFormatException - if constant type does not match tag
      See Also:
    • getConstant

      public <T extends Constant> T getConstant(int index, byte tag, Class<T> castTo) throws ClassFormatException
      Gets constant from constant pool and check whether it has the expected type.
      Parameters:
      index - Index in constant pool
      tag - Tag of expected constant, i.e., its type
      Returns:
      Constant value
      Throws:
      ClassFormatException - if constant type does not match tag
      Since:
      6.6.0
      See Also:
    • getConstant

      public <T extends Constant> T getConstant(int index, Class<T> castTo) throws ClassFormatException
      Gets constant from constant pool.
      Type Parameters:
      T - A Constant subclass
      Parameters:
      index - Index in constant pool
      castTo - The Constant subclass to cast to.
      Returns:
      Constant value
      Throws:
      ClassFormatException - if index is invalid
      Since:
      6.6.0
      See Also:
    • getConstantInteger

      public ConstantInteger getConstantInteger(int index)
      Gets constant from constant pool and check whether it has the expected type.
      Parameters:
      index - Index in constant pool
      Returns:
      ConstantInteger value
      Throws:
      ClassFormatException - if constant type does not match tag
      See Also:
    • getConstantPool

      Returns:
      Array of constants.
      See Also:
    • getConstantString

      public String getConstantString(int index, byte tag) throws IllegalArgumentException
      Gets string from constant pool and bypass the indirection of 'ConstantClass' and 'ConstantString' objects. I.e. these classes have an index field that points to another entry of the constant pool of type 'ConstantUtf8' which contains the real data.
      Parameters:
      index - Index in constant pool
      tag - Tag of expected constant, either ConstantClass or ConstantString
      Returns:
      Contents of string reference
      Throws:
      IllegalArgumentException - if tag is invalid
      See Also:
    • getConstantUtf8

      Gets constant from constant pool and check whether it has the expected type.
      Parameters:
      index - Index in constant pool
      Returns:
      ConstantUtf8 value
      Throws:
      ClassFormatException - if constant type does not match tag
      See Also:
    • getLength

      public int getLength()
      Returns:
      Length of constant pool.
    • iterator

      Specified by:
      iterator in interface Iterable<Constant>
    • setConstant

      public void setConstant(int index, Constant constant)
      Parameters:
      constant - Constant to set
    • setConstantPool

      public void setConstantPool(Constant[] constantPool)
      Parameters:
      constantPool -
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      String representation.