Class ClassBuilder

java.lang.Object
proguard.classfile.editor.ClassBuilder

public class ClassBuilder extends Object
This editor allows to build or extend classes (ProgramClass instances). It provides methods to easily add interfaces, fields, and methods, optionally with method bodies.

If you're adding many fields and methods, it is more efficient to reuse a single instance of this builder for all fields and methods that you add.

  • Constructor Details

    • ClassBuilder

      public ClassBuilder(int u4version, int u2accessFlags, String className, String superclassName)
      Creates a new ClassBuilder for the Java class with the given name and super class.
      Parameters:
      u4version - the class version.
      u2accessFlags - access flags for the new class.
      className - the fully qualified name of the new class.
      superclassName - the fully qualified name of the super class.
      See Also:
    • ClassBuilder

      public ClassBuilder(int u4version, int u2accessFlags, String className, String superclassName, String featureName, int processingFlags, Object processingInfo)
      Creates a new ClassBuilder for the Java class with the given name and super class.
      Parameters:
      u4version - the class version.
      u2accessFlags - access flags for the new class.
      className - the fully qualified name of the new class.
      superclassName - the fully qualified name of the super class.
      featureName - an optional feature name for the new class.
      processingFlags - optional processing flags for the new class.
      processingInfo - optional processing info for the new class.
      See Also:
    • ClassBuilder

      public ClassBuilder(ProgramClass programClass)
      Creates a new ClassBuilder for the given class.
      Parameters:
      programClass - the class to be edited.
    • ClassBuilder

      public ClassBuilder(ProgramClass programClass, ClassPool programClassPool, ClassPool libraryClassPool)
      Creates a new ClassBuilder for the given class, that automatically initializes class references and class member references in new constants.
      Parameters:
      programClass - the class 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 Details

    • getProgramClass

      public ProgramClass getProgramClass()
      Returns the created or edited ProgramClass instance. This is a live instance; any later calls to the builder will still affect the instance.
    • getConstantPoolEditor

      public ConstantPoolEditor getConstantPoolEditor()
      Returns a ConstantPoolEditor instance for the created or edited class instance. Reusing this instance is more efficient for newly created classes.
    • addInterface

      public ClassBuilder addInterface(Clazz interfaceClass)
      Adds a new interface to the edited class.
      Parameters:
      interfaceClass - the interface class.
      Returns:
      this instance of ClassBuilder.
    • addInterface

      public ClassBuilder addInterface(String interfaceName)
      Adds a new interface to the edited class.
      Parameters:
      interfaceName - the name of the interface.
      Returns:
      this instance of ClassBuilder.
    • addInterface

      public ClassBuilder addInterface(String interfaceName, Clazz referencedInterface)
      Adds a new interface to the edited class.
      Parameters:
      interfaceName - the name of the interface.
      referencedInterface - the referenced interface.
      Returns:
      this instance of ClassBuilder.
    • addField

      public ClassBuilder addField(int u2accessFlags, String fieldName, String fieldDescriptor)
      Adds a new field to the edited class.
      Parameters:
      u2accessFlags - access flags for the new field.
      fieldName - name of the new field.
      fieldDescriptor - descriptor of the new field.
      Returns:
      this instance of ClassBuilder.
    • addField

      public ClassBuilder addField(int u2accessFlags, String fieldName, String fieldDescriptor, MemberVisitor extraMemberVisitor)
      Adds a new field to the edited class.
      Parameters:
      u2accessFlags - access flags for the new field.
      fieldName - name of the new field.
      fieldDescriptor - descriptor of the new field.
      Returns:
      this instance of ClassBuilder.
    • addAndReturnField

      public ProgramField addAndReturnField(int u2accessFlags, String fieldName, String fieldDescriptor)
      Adds a new field to the edited class, and returns it.
      Parameters:
      u2accessFlags - access flags for the new field.
      fieldName - name of the new field.
      fieldDescriptor - descriptor of the new field.
      Returns:
      the newly created field.
    • addAndReturnField

      public ProgramField addAndReturnField(int u2accessFlags, String fieldName, String fieldDescriptor, Clazz referencedClass)
      Adds a new field to the edited class, and returns it.
      Parameters:
      u2accessFlags - access flags for the new field.
      fieldName - name of the new field.
      fieldDescriptor - descriptor of the new field.
      referencedClass - the Clazz object referenced in the descriptor string.
      Returns:
      the newly created field.
    • addMethod

      public ClassBuilder addMethod(int u2accessFlags, String methodName, String methodDescriptor)
      Adds a new method to the edited class.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      Returns:
      this instance of ClassBuilder.
    • addMethod

      public ClassBuilder addMethod(int u2accessFlags, String methodName, String methodDescriptor, MemberVisitor extraMemberVisitor)
      Adds a new method to the edited class.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      extraMemberVisitor - an optional visitor for the method after it has been created and added to the class.
      Returns:
      this instance of ClassBuilder.
    • addAndReturnMethod

      public ProgramMethod addAndReturnMethod(int u2accessFlags, String methodName, String methodDescriptor)
      Adds a new method to the edited class, and returns it.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      Returns:
      the newly created method.
    • addMethod

      public ClassBuilder addMethod(int u2accessFlags, String methodName, String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder)
      Adds a new method with a code attribute to the edited class.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      maxCodeFragmentLength - the maximum length for the code fragment.
      codeBuilder - the provider of a composer to create code attributes.
      Returns:
      this instance of ClassBuilder.
    • addMethod

      public ClassBuilder addMethod(int u2accessFlags, String methodName, String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder, MemberVisitor extraMemberVisitor)
      Adds a new method with a code attribute to the edited class.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      maxCodeFragmentLength - the maximum length for the code fragment.
      codeBuilder - the provider of a composer to create code attributes.
      extraMemberVisitor - an optional visitor for the method after it has been created and added to the class.
      Returns:
      this instance of ClassBuilder.
    • addAndReturnMethod

      public ProgramMethod addAndReturnMethod(int u2accessFlags, String methodName, String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder)
      Adds a new method with a code attribute to the edited class, and returns it.
      Parameters:
      u2accessFlags - the access flags of the new method.
      methodName - the name of the new method.
      methodDescriptor - the descriptor of the new method.
      maxCodeFragmentLength - the maximum length for the code fragment.
      codeBuilder - the provider of a composer to create code attributes.
      Returns:
      the newly created method.
    • main

      public static void main(String[] args)
      Small sample application that illustrates the use of this class.