Class ConstantPool

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable<Constant>, Node

    public class ConstantPool
    extends java.lang.Object
    implements java.lang.Cloneable, Node, java.lang.Iterable<Constant>
    This class represents the constant pool, that is, 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:
    Constant, ConstantPoolGen
    • Constructor Summary

      Constructors 
      Constructor Description
      ConstantPool​(java.io.DataInput input)
      Reads constants from given input stream.
      ConstantPool​(Constant... constantPool)
      Constructs a ConstantPool.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void accept​(Visitor v)
      Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
      java.lang.String constantToString​(int index, byte tag)
      Retrieves constant at 'index' from constant pool and resolve it to a string representation.
      java.lang.String constantToString​(Constant c)
      Resolves constant to a string representation.
      ConstantPool copy()
      Creates a deep copy of this constant pool.
      void dump​(java.io.DataOutputStream file)
      Dumps constant pool to file stream in binary format.
      private static java.lang.String escape​(java.lang.String str)  
      <T extends Constant>
      T
      getConstant​(int index)
      Gets constant from constant pool.
      <T extends Constant>
      T
      getConstant​(int index, byte tag)
      Gets constant from constant pool and check whether it has the expected type.
      <T extends Constant>
      T
      getConstant​(int index, byte tag, java.lang.Class<T> castTo)
      Gets constant from constant pool and check whether it has the expected type.
      <T extends Constant>
      T
      getConstant​(int index, java.lang.Class<T> castTo)
      Gets constant from constant pool.
      ConstantInteger getConstantInteger​(int index)
      Gets constant from constant pool and check whether it has the expected type.
      Constant[] getConstantPool()
      Gets the array of constants.
      java.lang.String getConstantString​(int index, byte tag)
      Gets string from constant pool and bypass the indirection of 'ConstantClass' and 'ConstantString' objects.
      ConstantUtf8 getConstantUtf8​(int index)
      Gets constant from constant pool and check whether it has the expected type.
      int getLength()
      Gets the length of constant pool.
      java.util.Iterator<Constant> iterator()  
      void setConstant​(int index, Constant constant)
      Sets a constant at the specified index.
      void setConstantPool​(Constant[] constantPool)
      Sets the constant pool.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • constantPool

        private Constant[] constantPool
    • Constructor Detail

      • ConstantPool

        public ConstantPool​(Constant... constantPool)
        Constructs a ConstantPool.
        Parameters:
        constantPool - Array of constants.
      • ConstantPool

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

      • escape

        private static java.lang.String escape​(java.lang.String str)
      • 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

        public java.lang.String constantToString​(Constant c)
                                          throws java.lang.IllegalArgumentException
        Resolves constant to a string representation.
        Parameters:
        c - Constant to be printed.
        Returns:
        String representation.
        Throws:
        java.lang.IllegalArgumentException - if c is unknown constant type.
      • constantToString

        public java.lang.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()
        Creates a deep copy of this constant pool.
        Returns:
        deep copy of this constant pool.
      • dump

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

        public <T extends Constant> T getConstant​(int index)
                                           throws ClassFormatException
        Gets constant from constant pool.
        Type Parameters:
        T - the type of the constant.
        Parameters:
        index - Index in constant pool.
        Returns:
        Constant value.
        Throws:
        ClassFormatException - if index is invalid.
        See Also:
        Constant
      • 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.
        Type Parameters:
        T - the type of the constant.
        Parameters:
        index - Index in constant pool.
        tag - Tag of expected constant, that is, its type.
        Returns:
        Constant value.
        Throws:
        ClassFormatException - if constant type does not match tag.
        See Also:
        Constant
      • getConstant

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

        public <T extends Constant> T getConstant​(int index,
                                                  java.lang.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:
        Constant
      • 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:
        ConstantInteger
      • getConstantPool

        public Constant[] getConstantPool()
        Gets the array of constants.
        Returns:
        Array of constants.
        See Also:
        Constant
      • getConstantString

        public java.lang.String getConstantString​(int index,
                                                  byte tag)
                                           throws java.lang.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:
        java.lang.IllegalArgumentException - if tag is invalid.
        See Also:
        ConstantClass, ConstantString
      • getConstantUtf8

        public ConstantUtf8 getConstantUtf8​(int index)
                                     throws ClassFormatException
        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:
        ConstantUtf8
      • getLength

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

        public java.util.Iterator<Constant> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<Constant>
      • setConstant

        public void setConstant​(int index,
                                Constant constant)
        Sets a constant at the specified index.
        Parameters:
        index - the index in the constant pool.
        constant - Constant to set.
      • setConstantPool

        public void setConstantPool​(Constant[] constantPool)
        Sets the constant pool.
        Parameters:
        constantPool - the constant pool array.
      • toString

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