Class ConstantPoolGen


  • public class ConstantPoolGen
    extends java.lang.Object
    This class is used to build up a constant pool. The user adds constants via 'addXXX' methods, 'addString', 'addClass', and so on. These methods return an index into the constant pool. Finally, 'getFinalConstantPool()' returns the constant pool built up. Intermediate versions of the constant pool can be obtained with 'getConstantPool()'. A constant pool has capacity for Constants.MAX_SHORT entries. Note that the first (0) is used by the JVM and that Double and Long constants need two slots.
    See Also:
    Constant
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​java.lang.Integer> classTable  
      protected Constant[] constants
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter
      private java.util.Map<java.lang.String,​java.lang.Integer> cpTable  
      private static int DEFAULT_BUFFER_SIZE  
      private static java.lang.String FIELDREF_DELIM  
      private static java.lang.String IMETHODREF_DELIM  
      protected int index
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getSize()
      private static java.lang.String METHODREF_DELIM  
      private static java.lang.String NAT_DELIM  
      private java.util.Map<java.lang.String,​java.lang.Integer> natTable  
      protected int size
      Deprecated.
      (since 6.0) will be made private; do not access directly, use getter/setter
      private java.util.Map<java.lang.String,​java.lang.Integer> stringTable  
      private java.util.Map<java.lang.String,​java.lang.Integer> utf8Table  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int addArrayClass​(ArrayType type)
      Add a reference to an array class (for example, String[][]) as needed by MULTIANEWARRAY instruction, for example, to the ConstantPool.
      int addClass​(java.lang.String str)
      Add a new Class reference to the ConstantPool, if it is not already in there.
      int addClass​(ObjectType type)
      Add a new Class reference to the ConstantPool for a given type.
      private int addClass_​(java.lang.String clazz)  
      int addConstant​(Constant constant, ConstantPoolGen cpGen)
      Adds a constant from another ConstantPool and returns the new index.
      int addDouble​(double n)
      Add a new double constant to the ConstantPool, if it is not already in there.
      int addFieldref​(java.lang.String className, java.lang.String fieldName, java.lang.String signature)
      Add a new Fieldref constant to the ConstantPool, if it is not already in there.
      int addFloat​(float n)
      Add a new Float constant to the ConstantPool, if it is not already in there.
      int addInteger​(int n)
      Add a new Integer constant to the ConstantPool, if it is not already in there.
      int addInterfaceMethodref​(java.lang.String className, java.lang.String methodName, java.lang.String signature)
      Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.
      int addInterfaceMethodref​(MethodGen method)
      Adds a new InterfaceMethodref constant to the ConstantPool.
      int addLong​(long n)
      Add a new long constant to the ConstantPool, if it is not already in there.
      int addMethodref​(java.lang.String className, java.lang.String methodName, java.lang.String signature)
      Add a new Methodref constant to the ConstantPool, if it is not already in there.
      int addMethodref​(MethodGen method)
      Adds a new Methodref constant to the ConstantPool.
      int addNameAndType​(java.lang.String name, java.lang.String signature)
      Add a new NameAndType constant to the ConstantPool if it is not already in there.
      int addString​(java.lang.String str)
      Add a new String constant to the ConstantPool, if it is not already in there.
      int addUtf8​(java.lang.String n)
      Add a new Utf8 constant to the ConstantPool, if it is not already in there.
      protected void adjustSize()
      Resize internal array of constants.
      private int computeIfAbsent​(java.util.Map<java.lang.String,​java.lang.Integer> map, java.lang.String key, int value)  
      Constant getConstant​(int i)
      Gets a constant pool entry at the specified index.
      ConstantPool getConstantPool()
      Gets the intermediate constant pool.
      ConstantPool getFinalConstantPool()
      Gets the constant pool with proper length.
      private int getIndex​(java.util.Map<java.lang.String,​java.lang.Integer> map, java.lang.String key)  
      int getSize()
      Gets the current size of constant pool.
      int lookupClass​(java.lang.String str)
      Look for ConstantClass in ConstantPool named 'str'.
      int lookupDouble​(double n)
      Look for ConstantDouble in ConstantPool.
      int lookupFieldref​(java.lang.String className, java.lang.String fieldName, java.lang.String signature)
      Look for ConstantFieldref in ConstantPool.
      int lookupFloat​(float n)
      Look for ConstantFloat in ConstantPool.
      int lookupInteger​(int n)
      Look for ConstantInteger in ConstantPool.
      int lookupInterfaceMethodref​(java.lang.String className, java.lang.String methodName, java.lang.String signature)
      Look for ConstantInterfaceMethodref in ConstantPool.
      int lookupInterfaceMethodref​(MethodGen method)
      Looks up an InterfaceMethodref in the ConstantPool.
      int lookupLong​(long n)
      Look for ConstantLong in ConstantPool.
      int lookupMethodref​(java.lang.String className, java.lang.String methodName, java.lang.String signature)
      Look for ConstantMethodref in ConstantPool.
      int lookupMethodref​(MethodGen method)
      Looks up a Methodref in the ConstantPool.
      int lookupNameAndType​(java.lang.String name, java.lang.String signature)
      Look for ConstantNameAndType in ConstantPool.
      int lookupString​(java.lang.String str)
      Look for ConstantString in ConstantPool containing String 'str'.
      int lookupUtf8​(java.lang.String n)
      Look for ConstantUtf8 in ConstantPool.
      void setConstant​(int i, Constant c)
      Use with care!
      private int toIndex​(java.lang.Integer index)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • IMETHODREF_DELIM

        private static final java.lang.String IMETHODREF_DELIM
        See Also:
        Constant Field Values
      • size

        @Deprecated
        protected int size
        Deprecated.
        (since 6.0) will be made private; do not access directly, use getter/setter
      • constants

        @Deprecated
        protected Constant[] constants
        Deprecated.
        (since 6.0) will be made private; do not access directly, use getter/setter
      • index

        @Deprecated
        protected int index
        Deprecated.
        (since 6.0) will be made private; do not access directly, use getSize()
      • stringTable

        private final java.util.Map<java.lang.String,​java.lang.Integer> stringTable
      • classTable

        private final java.util.Map<java.lang.String,​java.lang.Integer> classTable
      • utf8Table

        private final java.util.Map<java.lang.String,​java.lang.Integer> utf8Table
      • natTable

        private final java.util.Map<java.lang.String,​java.lang.Integer> natTable
      • cpTable

        private final java.util.Map<java.lang.String,​java.lang.Integer> cpTable
    • Constructor Detail

      • ConstantPoolGen

        public ConstantPoolGen()
        Constructs a new empty constant pool.
      • ConstantPoolGen

        public ConstantPoolGen​(Constant[] cs)
        Constructs a new instance with the given array of constants.
        Parameters:
        cs - array of given constants, new ones will be appended.
      • ConstantPoolGen

        public ConstantPoolGen​(ConstantPool cp)
        Constructs a new instance with the given constant pool.
        Parameters:
        cp - the constant pool.
    • Method Detail

      • addArrayClass

        public int addArrayClass​(ArrayType type)
        Add a reference to an array class (for example, String[][]) as needed by MULTIANEWARRAY instruction, for example, to the ConstantPool.
        Parameters:
        type - type of array class.
        Returns:
        index of entry.
      • addClass

        public int addClass​(ObjectType type)
        Add a new Class reference to the ConstantPool for a given type.
        Parameters:
        type - Class to add.
        Returns:
        index of entry.
      • addClass

        public int addClass​(java.lang.String str)
        Add a new Class reference to the ConstantPool, if it is not already in there.
        Parameters:
        str - Class to add.
        Returns:
        index of entry.
      • addClass_

        private int addClass_​(java.lang.String clazz)
      • addConstant

        public int addConstant​(Constant constant,
                               ConstantPoolGen cpGen)
        Adds a constant from another ConstantPool and returns the new index.
        Parameters:
        constant - The constant to add.
        cpGen - Source pool.
        Returns:
        index of entry.
      • addDouble

        public int addDouble​(double n)
        Add a new double constant to the ConstantPool, if it is not already in there.
        Parameters:
        n - Double number to add.
        Returns:
        index of entry.
      • addFieldref

        public int addFieldref​(java.lang.String className,
                               java.lang.String fieldName,
                               java.lang.String signature)
        Add a new Fieldref constant to the ConstantPool, if it is not already in there.
        Parameters:
        className - class name string to add.
        fieldName - field name string to add.
        signature - signature string to add.
        Returns:
        index of entry.
      • addFloat

        public int addFloat​(float n)
        Add a new Float constant to the ConstantPool, if it is not already in there.
        Parameters:
        n - Float number to add.
        Returns:
        index of entry.
      • addInteger

        public int addInteger​(int n)
        Add a new Integer constant to the ConstantPool, if it is not already in there.
        Parameters:
        n - integer number to add.
        Returns:
        index of entry.
      • addInterfaceMethodref

        public int addInterfaceMethodref​(MethodGen method)
        Adds a new InterfaceMethodref constant to the ConstantPool.
        Parameters:
        method - the method to add.
        Returns:
        index of entry.
      • addInterfaceMethodref

        public int addInterfaceMethodref​(java.lang.String className,
                                         java.lang.String methodName,
                                         java.lang.String signature)
        Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.
        Parameters:
        className - class name string to add.
        methodName - method name string to add.
        signature - signature string to add.
        Returns:
        index of entry.
      • addLong

        public int addLong​(long n)
        Add a new long constant to the ConstantPool, if it is not already in there.
        Parameters:
        n - Long number to add.
        Returns:
        index of entry.
      • addMethodref

        public int addMethodref​(MethodGen method)
        Adds a new Methodref constant to the ConstantPool.
        Parameters:
        method - the method to add.
        Returns:
        index of entry.
      • addMethodref

        public int addMethodref​(java.lang.String className,
                                java.lang.String methodName,
                                java.lang.String signature)
        Add a new Methodref constant to the ConstantPool, if it is not already in there.
        Parameters:
        className - class name string to add.
        methodName - method name string to add.
        signature - method signature string to add.
        Returns:
        index of entry.
      • addNameAndType

        public int addNameAndType​(java.lang.String name,
                                  java.lang.String signature)
        Add a new NameAndType constant to the ConstantPool if it is not already in there.
        Parameters:
        name - Name string to add.
        signature - signature string to add.
        Returns:
        index of entry.
      • addString

        public int addString​(java.lang.String str)
        Add a new String constant to the ConstantPool, if it is not already in there.
        Parameters:
        str - String to add.
        Returns:
        index of entry.
      • addUtf8

        public int addUtf8​(java.lang.String n)
        Add a new Utf8 constant to the ConstantPool, if it is not already in there.
        Parameters:
        n - Utf8 string to add.
        Returns:
        index of entry.
      • adjustSize

        protected void adjustSize()
        Resize internal array of constants.
      • computeIfAbsent

        private int computeIfAbsent​(java.util.Map<java.lang.String,​java.lang.Integer> map,
                                    java.lang.String key,
                                    int value)
      • getConstant

        public Constant getConstant​(int i)
        Gets a constant pool entry at the specified index.
        Parameters:
        i - index in constant pool.
        Returns:
        constant pool entry at index i.
      • getConstantPool

        public ConstantPool getConstantPool()
        Gets the intermediate constant pool.
        Returns:
        intermediate constant pool.
      • getFinalConstantPool

        public ConstantPool getFinalConstantPool()
        Gets the constant pool with proper length.
        Returns:
        constant pool with proper length.
      • getIndex

        private int getIndex​(java.util.Map<java.lang.String,​java.lang.Integer> map,
                             java.lang.String key)
      • getSize

        public int getSize()
        Gets the current size of constant pool.
        Returns:
        current size of constant pool.
      • lookupClass

        public int lookupClass​(java.lang.String str)
        Look for ConstantClass in ConstantPool named 'str'.
        Parameters:
        str - String to search for.
        Returns:
        index on success, -1 otherwise.
      • lookupDouble

        public int lookupDouble​(double n)
        Look for ConstantDouble in ConstantPool.
        Parameters:
        n - Double number to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupFieldref

        public int lookupFieldref​(java.lang.String className,
                                  java.lang.String fieldName,
                                  java.lang.String signature)
        Look for ConstantFieldref in ConstantPool.
        Parameters:
        className - Where to find method.
        fieldName - Guess what.
        signature - return and argument types.
        Returns:
        index on success, -1 otherwise.
      • lookupFloat

        public int lookupFloat​(float n)
        Look for ConstantFloat in ConstantPool.
        Parameters:
        n - Float number to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupInteger

        public int lookupInteger​(int n)
        Look for ConstantInteger in ConstantPool.
        Parameters:
        n - integer number to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupInterfaceMethodref

        public int lookupInterfaceMethodref​(MethodGen method)
        Looks up an InterfaceMethodref in the ConstantPool.
        Parameters:
        method - the method to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupInterfaceMethodref

        public int lookupInterfaceMethodref​(java.lang.String className,
                                            java.lang.String methodName,
                                            java.lang.String signature)
        Look for ConstantInterfaceMethodref in ConstantPool.
        Parameters:
        className - Where to find method.
        methodName - Guess what.
        signature - return and argument types.
        Returns:
        index on success, -1 otherwise.
      • lookupLong

        public int lookupLong​(long n)
        Look for ConstantLong in ConstantPool.
        Parameters:
        n - Long number to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupMethodref

        public int lookupMethodref​(MethodGen method)
        Looks up a Methodref in the ConstantPool.
        Parameters:
        method - the method to look for.
        Returns:
        index on success, -1 otherwise.
      • lookupMethodref

        public int lookupMethodref​(java.lang.String className,
                                   java.lang.String methodName,
                                   java.lang.String signature)
        Look for ConstantMethodref in ConstantPool.
        Parameters:
        className - Where to find method.
        methodName - Guess what.
        signature - return and argument types.
        Returns:
        index on success, -1 otherwise.
      • lookupNameAndType

        public int lookupNameAndType​(java.lang.String name,
                                     java.lang.String signature)
        Look for ConstantNameAndType in ConstantPool.
        Parameters:
        name - of variable/method.
        signature - of variable/method.
        Returns:
        index on success, -1 otherwise.
      • lookupString

        public int lookupString​(java.lang.String str)
        Look for ConstantString in ConstantPool containing String 'str'.
        Parameters:
        str - String to search for.
        Returns:
        index on success, -1 otherwise.
      • lookupUtf8

        public int lookupUtf8​(java.lang.String n)
        Look for ConstantUtf8 in ConstantPool.
        Parameters:
        n - Utf8 string to look for.
        Returns:
        index on success, -1 otherwise.
      • setConstant

        public void setConstant​(int i,
                                Constant c)
        Use with care!
        Parameters:
        i - index in constant pool.
        c - new constant pool entry at index i.
      • toIndex

        private int toIndex​(java.lang.Integer index)
      • toString

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