Class ClassBuilder


  • public class ClassBuilder
    extends java.lang.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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ClassBuilder.CodeBuilder
      This functional interface provides a code attribute composer to its implementation.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassBuilder​(int u4version, int u2accessFlags, java.lang.String className, java.lang.String superclassName)
      Creates a new ClassBuilder for the Java class with the given name and super class.
      ClassBuilder​(int u4version, int u2accessFlags, java.lang.String className, java.lang.String superclassName, java.lang.String featureName, int processingFlags, java.lang.Object processingInfo)
      Creates a new ClassBuilder for the Java class with the given name and super class.
      ClassBuilder​(ProgramClass programClass)
      Creates a new ClassBuilder for the given class.
      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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ProgramField addAndReturnField​(int u2accessFlags, java.lang.String fieldName, java.lang.String fieldDescriptor)
      Adds a new field to the edited class, and returns it.
      ProgramMethod addAndReturnMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor)
      Adds a new method to the edited class, and returns it.
      ProgramMethod addAndReturnMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder)
      Adds a new method with a code attribute to the edited class, and returns it.
      ClassBuilder addField​(int u2accessFlags, java.lang.String fieldName, java.lang.String fieldDescriptor)
      Adds a new field to the edited class.
      ClassBuilder addField​(int u2accessFlags, java.lang.String fieldName, java.lang.String fieldDescriptor, MemberVisitor extraMemberVisitor)
      Adds a new field to the edited class.
      ClassBuilder addInterface​(java.lang.String interfaceName)
      Adds a new interface to the edited class.
      ClassBuilder addInterface​(java.lang.String interfaceName, Clazz referencedInterface)
      Adds a new interface to the edited class.
      ClassBuilder addInterface​(Clazz interfaceClass)
      Adds a new interface to the edited class.
      ClassBuilder addMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor)
      Adds a new method to the edited class.
      ClassBuilder addMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder)
      Adds a new method with a code attribute to the edited class.
      ClassBuilder addMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor, int maxCodeFragmentLength, ClassBuilder.CodeBuilder codeBuilder, MemberVisitor extraMemberVisitor)
      Adds a new method with a code attribute to the edited class.
      ClassBuilder addMethod​(int u2accessFlags, java.lang.String methodName, java.lang.String methodDescriptor, MemberVisitor extraMemberVisitor)
      Adds a new method to the edited class.
      ConstantPoolEditor getConstantPoolEditor()
      Returns a ConstantPoolEditor instance for the created or edited class instance.
      ProgramClass getProgramClass()
      Returns the created or edited ProgramClass instance.
      static void main​(java.lang.String[] args)
      Small sample application that illustrates the use of this class.
      • Methods inherited from class java.lang.Object

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

      • ClassBuilder

        public ClassBuilder​(int u4version,
                            int u2accessFlags,
                            java.lang.String className,
                            java.lang.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:
        VersionConstants, AccessConstants
      • ClassBuilder

        public ClassBuilder​(int u4version,
                            int u2accessFlags,
                            java.lang.String className,
                            java.lang.String superclassName,
                            java.lang.String featureName,
                            int processingFlags,
                            java.lang.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:
        VersionConstants, AccessConstants
      • 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 Detail

      • 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​(java.lang.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​(java.lang.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,
                                     java.lang.String fieldName,
                                     java.lang.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,
                                     java.lang.String fieldName,
                                     java.lang.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,
                                              java.lang.String fieldName,
                                              java.lang.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.
      • addMethod

        public ClassBuilder addMethod​(int u2accessFlags,
                                      java.lang.String methodName,
                                      java.lang.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,
                                      java.lang.String methodName,
                                      java.lang.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,
                                                java.lang.String methodName,
                                                java.lang.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,
                                      java.lang.String methodName,
                                      java.lang.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,
                                      java.lang.String methodName,
                                      java.lang.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,
                                                java.lang.String methodName,
                                                java.lang.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​(java.lang.String[] args)
        Small sample application that illustrates the use of this class.