Class ConstantPoolEditor


  • public class ConstantPoolEditor
    extends java.lang.Object
    This class can add constant pool entries to a given class.

    If you're building a class from scratch, it is more efficient to reuse a single instance of this editor for all constants that you add.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int addClassConstant​(int nameIndex, Clazz referencedClass)
      Finds or creates a ClassConstant constant pool entry with the given name UTF-8 constant pool index.
      int addClassConstant​(java.lang.String name, Clazz referencedClass)
      Finds or creates a ClassConstant constant pool entry with the given name.
      int addClassConstant​(Clazz referencedClass)
      Finds or creates a ClassConstant constant pool entry for the given class.
      int addConstant​(Constant constant)
      Adds a given constant pool entry to the end of the constant pool.
      int addDoubleConstant​(double value)
      Finds or creates a DoubleConstant constant pool entry with the given value.
      int addDynamicConstant​(int bootstrapMethodIndex, int nameAndTypeIndex, Clazz[] referencedClasses)
      Finds or creates a DynamicConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
      int addFieldrefConstant​(int classIndex, int nameAndTypeIndex, Clazz referencedClass, Field referencedField)
      Finds or creates a FieldrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
      int addFieldrefConstant​(int classIndex, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Field referencedField)
      Finds or creates a FieldrefConstant constant pool entry with the given class constant pool entry index, field name, and descriptor.
      int addFieldrefConstant​(java.lang.String className, int nameAndTypeIndex, Clazz referencedClass, Field referencedField)
      Finds or creates a FieldrefConstant constant pool entry with the given class name, field name, and descriptor.
      int addFieldrefConstant​(java.lang.String className, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Field referencedField)
      Finds or creates a FieldrefConstant constant pool entry with the given class name, field name, and descriptor.
      int addFieldrefConstant​(Clazz referencedClass, Field referencedField)
      Finds or creates a FieldrefConstant constant pool entry for the given class and field.
      int addFloatConstant​(float value)
      Finds or creates a FloatConstant constant pool entry with the given value.
      int addIntegerConstant​(int value)
      Finds or creates a IntegerConstant constant pool entry with the given value.
      int addInterfaceMethodrefConstant​(int classIndex, int nameAndTypeIndex, Clazz referencedClass, Method referencedMethod)
      Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
      int addInterfaceMethodrefConstant​(int classIndex, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Method referencedMethod)
      Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class constant pool entry index, method name, and descriptor.
      int addInterfaceMethodrefConstant​(java.lang.String className, int nameAndTypeIndex, Clazz referencedClass, Method referencedMethod)
      Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class name, method name, and descriptor.
      int addInterfaceMethodrefConstant​(java.lang.String className, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Method referencedMethod)
      Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class name, method name, and descriptor.
      int addInterfaceMethodrefConstant​(Clazz referencedClass, Method referencedMethod)
      Finds or creates a InterfaceMethodrefConstant constant pool entry for the given class and method.
      int addInvokeDynamicConstant​(int bootstrapMethodIndex, int nameAndTypeIndex, Clazz[] referencedClasses)
      Finds or creates an InvokeDynamicConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
      int addInvokeDynamicConstant​(int bootstrapMethodIndex, java.lang.String name, java.lang.String descriptor, Clazz[] referencedClasses)
      Finds or creates a InvokeDynamicConstant constant pool entry with the given bootstrap method constant pool entry index, method name, and descriptor.
      int addLongConstant​(long value)
      Finds or creates a LongConstant constant pool entry with the given value.
      int addMethodHandleConstant​(int referenceKind, int referenceIndex)
      Finds or creates a MethodHandleConstant constant pool entry of the specified kind and with the given field ref, interface method ref, or method ref constant pool entry index.
      int addMethodrefConstant​(int classIndex, int nameAndTypeIndex, Clazz referencedClass, Method referencedMethod)
      Finds or creates a MethodrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
      int addMethodrefConstant​(int classIndex, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Method referencedMethod)
      Finds or creates a MethodrefConstant constant pool entry with the given class constant pool entry index, method name, and descriptor.
      int addMethodrefConstant​(java.lang.String className, int nameAndTypeIndex, Clazz referencedClass, Method referencedMethod)
      Finds or creates a MethodrefConstant constant pool entry with the given class name, method name, and descriptor.
      int addMethodrefConstant​(java.lang.String className, java.lang.String name, java.lang.String descriptor, Clazz referencedClass, Method referencedMethod)
      Finds or creates a MethodrefConstant constant pool entry with the given class name, method name, and descriptor.
      int addMethodrefConstant​(Clazz referencedClass, Method referencedMethod)
      Finds or creates a MethodrefConstant constant pool entry for the given class and method.
      int addMethodTypeConstant​(int descriptorIndex, Clazz[] referencedClasses)
      Finds or creates a MethodTypeConstant constant pool entry with the given descriptor UTF-8 constant pool index.
      int addMethodTypeConstant​(java.lang.String descriptor, Clazz[] referencedClasses)
      Finds or creates a MethodTypeConstant constant pool entry with the given descriptor.
      int addModuleConstant​(int nameIndex)
      Finds or creates a ModuleConstant constant pool entry with the given name constant pool index.
      int addModuleConstant​(java.lang.String name)
      Finds or creates a ModuleConstant constant pool entry with the given name.
      int addNameAndTypeConstant​(int nameIndex, int descriptorIndex)
      Finds or creates a NameAndTypeConstant constant pool entry with the given name and descriptor UTF-8 constant pool indices.
      int addNameAndTypeConstant​(java.lang.String name, java.lang.String descriptor)
      Finds or creates a NameAndTypeConstant constant pool entry with the given name and descriptor.
      int addPackageConstant​(int nameIndex)
      Finds or creates a PackageConstant constant pool entry with the given name constant pool index.
      int addPackageConstant​(java.lang.String name)
      Finds or creates a PackageConstant constant pool entry with the given name.
      int addPrimitiveArrayConstant​(java.lang.Object values)
      Finds or creates a PrimitiveArrayConstant constant pool entry with the given values.
      int addStringConstant​(int utf8index, Clazz referencedClass, Member referencedMember, int resourceFileId, ResourceFile resourceFile)
      Finds or creates a StringConstant constant pool entry with the given UTF-8 constant index.
      int addStringConstant​(java.lang.String string)
      Finds or creates a StringConstant constant pool entry with the given value.
      int addStringConstant​(java.lang.String string, Clazz referencedClass, Member referencedMember)
      Finds or creates a StringConstant constant pool entry with the given value, with optional referenced class/member.
      int addStringConstant​(java.lang.String string, Clazz referencedClass, Member referencedMember, int resourceFileId, ResourceFile resourceFile)
      Finds or creates a StringConstant constant pool entry with the given value.
      int addStringConstant​(java.lang.String string, ResourceFile referencedResourceFile)
      Finds or creates a StringConstant constant pool entry with the given value.
      int addUtf8Constant​(java.lang.String string)
      Finds or creates a Utf8Constant constant pool entry for the given string.
      int findOrAddConstant​(Constant constant)
      Finds or adds a given constant pool entry.
      ProgramClass getTargetClass()
      Returns the target class in which constants are edited.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConstantPoolEditor

        public ConstantPoolEditor​(ProgramClass targetClass)
        Creates a new ConstantPoolEditor.
        Parameters:
        targetClass - the target class in which constants are to be edited.
      • ConstantPoolEditor

        public ConstantPoolEditor​(ProgramClass targetClass,
                                  ClassPool programClassPool,
                                  ClassPool libraryClassPool)
        Creates a new ConstantPoolEditor that automatically initializes class references and class member references in new constants.
        Parameters:
        targetClass - the target class in which constants are to be edited.
        programClassPool - the program class pool from which new constants can be initialized.
        libraryClassPool - the library class pool from which new constants can be initialized.
    • Method Detail

      • getTargetClass

        public ProgramClass getTargetClass()
        Returns the target class in which constants are edited.
      • addIntegerConstant

        public int addIntegerConstant​(int value)
        Finds or creates a IntegerConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the Utf8Constant.
      • addLongConstant

        public int addLongConstant​(long value)
        Finds or creates a LongConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the LongConstant.
      • addFloatConstant

        public int addFloatConstant​(float value)
        Finds or creates a FloatConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the FloatConstant.
      • addDoubleConstant

        public int addDoubleConstant​(double value)
        Finds or creates a DoubleConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the DoubleConstant.
      • addPrimitiveArrayConstant

        public int addPrimitiveArrayConstant​(java.lang.Object values)
        Finds or creates a PrimitiveArrayConstant constant pool entry with the given values.
        Returns:
        the constant pool index of the PrimitiveArrayConstant.
      • addStringConstant

        public int addStringConstant​(java.lang.String string,
                                     Clazz referencedClass,
                                     Member referencedMember)
        Finds or creates a StringConstant constant pool entry with the given value, with optional referenced class/member.
        Parameters:
        referencedClass - The class that this string references.
        referencedMember - The member that this string references.
        Returns:
        the constant pool index of the StringConstant.
      • addStringConstant

        public int addStringConstant​(java.lang.String string)
        Finds or creates a StringConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the StringConstant.
      • addStringConstant

        public int addStringConstant​(java.lang.String string,
                                     ResourceFile referencedResourceFile)
        Finds or creates a StringConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the StringConstant.
      • addStringConstant

        public int addStringConstant​(java.lang.String string,
                                     Clazz referencedClass,
                                     Member referencedMember,
                                     int resourceFileId,
                                     ResourceFile resourceFile)
        Finds or creates a StringConstant constant pool entry with the given value.
        Returns:
        the constant pool index of the StringConstant.
      • addStringConstant

        public int addStringConstant​(int utf8index,
                                     Clazz referencedClass,
                                     Member referencedMember,
                                     int resourceFileId,
                                     ResourceFile resourceFile)
        Finds or creates a StringConstant constant pool entry with the given UTF-8 constant index.
        Returns:
        the constant pool index of the StringConstant.
      • addInvokeDynamicConstant

        public int addInvokeDynamicConstant​(int bootstrapMethodIndex,
                                            java.lang.String name,
                                            java.lang.String descriptor,
                                            Clazz[] referencedClasses)
        Finds or creates a InvokeDynamicConstant constant pool entry with the given bootstrap method constant pool entry index, method name, and descriptor.
        Returns:
        the constant pool index of the InvokeDynamicConstant.
      • addDynamicConstant

        public int addDynamicConstant​(int bootstrapMethodIndex,
                                      int nameAndTypeIndex,
                                      Clazz[] referencedClasses)
        Finds or creates a DynamicConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
        Returns:
        the constant pool index of the DynamicConstant.
      • addInvokeDynamicConstant

        public int addInvokeDynamicConstant​(int bootstrapMethodIndex,
                                            int nameAndTypeIndex,
                                            Clazz[] referencedClasses)
        Finds or creates an InvokeDynamicConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
        Returns:
        the constant pool index of the InvokeDynamicConstant.
      • addMethodHandleConstant

        public int addMethodHandleConstant​(int referenceKind,
                                           int referenceIndex)
        Finds or creates a MethodHandleConstant constant pool entry of the specified kind and with the given field ref, interface method ref, or method ref constant pool entry index.
        Returns:
        the constant pool index of the MethodHandleConstant.
      • addModuleConstant

        public int addModuleConstant​(java.lang.String name)
        Finds or creates a ModuleConstant constant pool entry with the given name.
        Returns:
        the constant pool index of the ModuleConstant.
      • addModuleConstant

        public int addModuleConstant​(int nameIndex)
        Finds or creates a ModuleConstant constant pool entry with the given name constant pool index.
        Returns:
        the constant pool index of the ModuleConstant.
      • addPackageConstant

        public int addPackageConstant​(java.lang.String name)
        Finds or creates a PackageConstant constant pool entry with the given name.
        Returns:
        the constant pool index of the PackageConstant.
      • addPackageConstant

        public int addPackageConstant​(int nameIndex)
        Finds or creates a PackageConstant constant pool entry with the given name constant pool index.
        Returns:
        the constant pool index of the PackageConstant.
      • addFieldrefConstant

        public int addFieldrefConstant​(Clazz referencedClass,
                                       Field referencedField)
        Finds or creates a FieldrefConstant constant pool entry for the given class and field.
        Returns:
        the constant pool index of the FieldrefConstant.
      • addFieldrefConstant

        public int addFieldrefConstant​(java.lang.String className,
                                       java.lang.String name,
                                       java.lang.String descriptor,
                                       Clazz referencedClass,
                                       Field referencedField)
        Finds or creates a FieldrefConstant constant pool entry with the given class name, field name, and descriptor.
        Returns:
        the constant pool index of the FieldrefConstant.
      • addFieldrefConstant

        public int addFieldrefConstant​(java.lang.String className,
                                       int nameAndTypeIndex,
                                       Clazz referencedClass,
                                       Field referencedField)
        Finds or creates a FieldrefConstant constant pool entry with the given class name, field name, and descriptor.
        Returns:
        the constant pool index of the FieldrefConstant.
      • addFieldrefConstant

        public int addFieldrefConstant​(int classIndex,
                                       java.lang.String name,
                                       java.lang.String descriptor,
                                       Clazz referencedClass,
                                       Field referencedField)
        Finds or creates a FieldrefConstant constant pool entry with the given class constant pool entry index, field name, and descriptor.
        Returns:
        the constant pool index of the FieldrefConstant.
      • addFieldrefConstant

        public int addFieldrefConstant​(int classIndex,
                                       int nameAndTypeIndex,
                                       Clazz referencedClass,
                                       Field referencedField)
        Finds or creates a FieldrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
        Returns:
        the constant pool index of the FieldrefConstant.
      • addInterfaceMethodrefConstant

        public int addInterfaceMethodrefConstant​(java.lang.String className,
                                                 java.lang.String name,
                                                 java.lang.String descriptor,
                                                 Clazz referencedClass,
                                                 Method referencedMethod)
        Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class name, method name, and descriptor.
        Returns:
        the constant pool index of the InterfaceMethodrefConstant.
      • addInterfaceMethodrefConstant

        public int addInterfaceMethodrefConstant​(java.lang.String className,
                                                 int nameAndTypeIndex,
                                                 Clazz referencedClass,
                                                 Method referencedMethod)
        Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class name, method name, and descriptor.
        Returns:
        the constant pool index of the InterfaceMethodrefConstant.
      • addInterfaceMethodrefConstant

        public int addInterfaceMethodrefConstant​(Clazz referencedClass,
                                                 Method referencedMethod)
        Finds or creates a InterfaceMethodrefConstant constant pool entry for the given class and method.
        Returns:
        the constant pool index of the InterfaceMethodrefConstant.
      • addInterfaceMethodrefConstant

        public int addInterfaceMethodrefConstant​(int classIndex,
                                                 java.lang.String name,
                                                 java.lang.String descriptor,
                                                 Clazz referencedClass,
                                                 Method referencedMethod)
        Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class constant pool entry index, method name, and descriptor.
        Returns:
        the constant pool index of the InterfaceMethodrefConstant.
      • addInterfaceMethodrefConstant

        public int addInterfaceMethodrefConstant​(int classIndex,
                                                 int nameAndTypeIndex,
                                                 Clazz referencedClass,
                                                 Method referencedMethod)
        Finds or creates a InterfaceMethodrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
        Returns:
        the constant pool index of the InterfaceMethodrefConstant.
      • addMethodrefConstant

        public int addMethodrefConstant​(Clazz referencedClass,
                                        Method referencedMethod)
        Finds or creates a MethodrefConstant constant pool entry for the given class and method.
        Returns:
        the constant pool index of the MethodrefConstant.
      • addMethodrefConstant

        public int addMethodrefConstant​(java.lang.String className,
                                        java.lang.String name,
                                        java.lang.String descriptor,
                                        Clazz referencedClass,
                                        Method referencedMethod)
        Finds or creates a MethodrefConstant constant pool entry with the given class name, method name, and descriptor.
        Returns:
        the constant pool index of the MethodrefConstant.
      • addMethodrefConstant

        public int addMethodrefConstant​(java.lang.String className,
                                        int nameAndTypeIndex,
                                        Clazz referencedClass,
                                        Method referencedMethod)
        Finds or creates a MethodrefConstant constant pool entry with the given class name, method name, and descriptor.
        Returns:
        the constant pool index of the MethodrefConstant.
      • addMethodrefConstant

        public int addMethodrefConstant​(int classIndex,
                                        java.lang.String name,
                                        java.lang.String descriptor,
                                        Clazz referencedClass,
                                        Method referencedMethod)
        Finds or creates a MethodrefConstant constant pool entry with the given class constant pool entry index, method name, and descriptor.
        Returns:
        the constant pool index of the MethodrefConstant.
      • addMethodrefConstant

        public int addMethodrefConstant​(int classIndex,
                                        int nameAndTypeIndex,
                                        Clazz referencedClass,
                                        Method referencedMethod)
        Finds or creates a MethodrefConstant constant pool entry with the given class constant pool entry index and name and type constant pool entry index.
        Returns:
        the constant pool index of the MethodrefConstant.
      • addClassConstant

        public int addClassConstant​(Clazz referencedClass)
        Finds or creates a ClassConstant constant pool entry for the given class.
        Returns:
        the constant pool index of the ClassConstant.
      • addClassConstant

        public int addClassConstant​(java.lang.String name,
                                    Clazz referencedClass)
        Finds or creates a ClassConstant constant pool entry with the given name.
        Returns:
        the constant pool index of the ClassConstant.
      • addClassConstant

        public int addClassConstant​(int nameIndex,
                                    Clazz referencedClass)
        Finds or creates a ClassConstant constant pool entry with the given name UTF-8 constant pool index.
        Returns:
        the constant pool index of the ClassConstant.
      • addMethodTypeConstant

        public int addMethodTypeConstant​(java.lang.String descriptor,
                                         Clazz[] referencedClasses)
        Finds or creates a MethodTypeConstant constant pool entry with the given descriptor.
        Returns:
        the constant pool index of the MethodTypeConstant.
      • addMethodTypeConstant

        public int addMethodTypeConstant​(int descriptorIndex,
                                         Clazz[] referencedClasses)
        Finds or creates a MethodTypeConstant constant pool entry with the given descriptor UTF-8 constant pool index.
        Returns:
        the constant pool index of the MethodTypeConstant.
      • addNameAndTypeConstant

        public int addNameAndTypeConstant​(java.lang.String name,
                                          java.lang.String descriptor)
        Finds or creates a NameAndTypeConstant constant pool entry with the given name and descriptor.
        Returns:
        the constant pool index of the NameAndTypeConstant.
      • addNameAndTypeConstant

        public int addNameAndTypeConstant​(int nameIndex,
                                          int descriptorIndex)
        Finds or creates a NameAndTypeConstant constant pool entry with the given name and descriptor UTF-8 constant pool indices.
        Returns:
        the constant pool index of the NameAndTypeConstant.
      • addUtf8Constant

        public int addUtf8Constant​(java.lang.String string)
        Finds or creates a Utf8Constant constant pool entry for the given string.
        Returns:
        the constant pool index of the Utf8Constant.
      • findOrAddConstant

        public int findOrAddConstant​(Constant constant)
        Finds or adds a given constant pool entry.
        Returns:
        the index of the entry in the constant pool.
      • addConstant

        public int addConstant​(Constant constant)
        Adds a given constant pool entry to the end of the constant pool.
        Returns:
        the constant pool index for the added entry.
        See Also:
        findOrAddConstant(Constant)