类 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.

    • 嵌套类概要

      嵌套类 
      修饰符和类型 说明
      static interface  ClassBuilder.CodeBuilder
      This functional interface provides a code attribute composer to its implementation.
    • 构造器概要

      构造器 
      构造器 说明
      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.
    • 方法概要

      所有方法 静态方法 实例方法 具体方法 
      修饰符和类型 方法 说明
      ProgramField addAndReturnField​(int u2accessFlags, java.lang.String fieldName, java.lang.String fieldDescriptor)
      Adds a new field to the edited class, and returns it.
      ProgramField addAndReturnField​(int u2accessFlags, java.lang.String fieldName, java.lang.String fieldDescriptor, Clazz referencedClass)
      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.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • 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.
        参数:
        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.
        另请参阅:
        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.
        参数:
        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.
        另请参阅:
        VersionConstants, AccessConstants
      • ClassBuilder

        public ClassBuilder​(ProgramClass programClass)
        Creates a new ClassBuilder for the given class.
        参数:
        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.
        参数:
        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.
    • 方法详细资料

      • 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.
        参数:
        interfaceClass - the interface class.
        返回:
        this instance of ClassBuilder.
      • addInterface

        public ClassBuilder addInterface​(java.lang.String interfaceName)
        Adds a new interface to the edited class.
        参数:
        interfaceName - the name of the interface.
        返回:
        this instance of ClassBuilder.
      • addInterface

        public ClassBuilder addInterface​(java.lang.String interfaceName,
                                         Clazz referencedInterface)
        Adds a new interface to the edited class.
        参数:
        interfaceName - the name of the interface.
        referencedInterface - the referenced interface.
        返回:
        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.
        参数:
        u2accessFlags - access flags for the new field.
        fieldName - name of the new field.
        fieldDescriptor - descriptor of the new field.
        返回:
        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.
        参数:
        u2accessFlags - access flags for the new field.
        fieldName - name of the new field.
        fieldDescriptor - descriptor of the new field.
        返回:
        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.
        参数:
        u2accessFlags - access flags for the new field.
        fieldName - name of the new field.
        fieldDescriptor - descriptor of the new field.
        返回:
        the newly created field.
      • addAndReturnField

        public ProgramField addAndReturnField​(int u2accessFlags,
                                              java.lang.String fieldName,
                                              java.lang.String fieldDescriptor,
                                              Clazz referencedClass)
        Adds a new field to the edited class, and returns it.
        参数:
        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.
        返回:
        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.
        参数:
        u2accessFlags - the access flags of the new method.
        methodName - the name of the new method.
        methodDescriptor - the descriptor of the new method.
        返回:
        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.
        参数:
        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.
        返回:
        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.
        参数:
        u2accessFlags - the access flags of the new method.
        methodName - the name of the new method.
        methodDescriptor - the descriptor of the new method.
        返回:
        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.
        参数:
        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.
        返回:
        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.
        参数:
        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.
        返回:
        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.
        参数:
        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.
        返回:
        the newly created method.
      • main

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