Class ClassFactory


  • public final class ClassFactory
    extends Object
    Generator of bytecode-backed members on a structural ClassFile - constructors, initializers, accessors, field initial values, method bodies, and StackMapTable frames.
    • Method Detail

      • createClass

        public static ClassFile createClass​(String className,
                                            int accessFlags)
        Creates a detached class carrying a default constructor and class initializer.
        Parameters:
        className - binary name of the new class
        accessFlags - class access flags
        Returns:
        the new class file
      • createClass

        public static ClassFile createClass​(ClassPool pool,
                                            String className,
                                            int accessFlags)
                                     throws IOException
        Creates a class registered in a pool, adds a default constructor and class initializer, then rebuilds it.
        Parameters:
        pool - pool that owns the new class
        className - binary name of the new class
        accessFlags - class access flags
        Returns:
        the new class file
        Throws:
        IOException - if the pool cannot create the class or the rebuild fails
      • addDefaultConstructor

        public static MethodEntry addDefaultConstructor​(ClassFile cf)
        Adds a no-argument constructor whose body only invokes the superclass constructor.
        Parameters:
        cf - class to receive the constructor
        Returns:
        the added constructor
      • addDefaultClassInitializer

        public static MethodEntry addDefaultClassInitializer​(ClassFile cf)
        Adds an empty static class initializer.
        Parameters:
        cf - class to receive the initializer
        Returns:
        the added initializer
      • createMethodWithBody

        public static MethodEntry createMethodWithBody​(ClassFile cf,
                                                       int accessFlags,
                                                       String methodName,
                                                       String methodDescriptor)
        Creates a method whose generated body immediately returns a zero or null value of the declared return type.
        Parameters:
        cf - class to receive the method
        accessFlags - method access flags
        methodName - method name
        methodDescriptor - method descriptor
        Returns:
        the added method
      • generateGetter

        public static MethodEntry generateGetter​(ClassFile cf,
                                                 FieldEntry entry,
                                                 boolean isStatic)
                                          throws IOException
        Generates a getter named get<Field> for a field.
        Parameters:
        cf - class to receive the getter
        entry - field to read
        isStatic - true to emit a static getter reading a static field
        Returns:
        the generated getter
        Throws:
        IOException - if the method body cannot be finalized
      • generateGetter

        public static MethodEntry generateGetter​(ClassFile cf,
                                                 FieldEntry entry,
                                                 String name,
                                                 boolean isStatic)
                                          throws IOException
        Generates a getter under a caller-chosen name for a field.
        Parameters:
        cf - class to receive the getter
        entry - field to read
        name - name of the generated method
        isStatic - true to emit a static getter reading a static field
        Returns:
        the generated getter
        Throws:
        IOException - if the method body cannot be finalized
      • generateSetter

        public static MethodEntry generateSetter​(ClassFile cf,
                                                 FieldEntry entry,
                                                 boolean isStatic)
                                          throws IOException
        Generates a setter named set<Field> for a field.
        Parameters:
        cf - class to receive the setter
        entry - field to write
        isStatic - true to emit a static setter writing a static field
        Returns:
        the generated setter
        Throws:
        IOException - if the method body cannot be finalized
      • generateSetter

        public static MethodEntry generateSetter​(ClassFile cf,
                                                 FieldEntry entry,
                                                 String name,
                                                 boolean isStatic)
                                          throws IOException
        Generates a setter under a caller-chosen name for a field.
        Parameters:
        cf - class to receive the setter
        entry - field to write
        name - name of the generated method
        isStatic - true to emit a static setter writing a static field
        Returns:
        the generated setter
        Throws:
        IOException - if the method body cannot be finalized
      • setFieldInitialValue

        public static void setFieldInitialValue​(ClassFile cf,
                                                FieldEntry field,
                                                Object value)
                                         throws IOException
        Prepends a store of a constant to a field at the top of the class initializer for a static field, or the no-argument constructor for an instance field, creating that method if absent.
        Parameters:
        cf - class owning the field
        field - field to initialize
        value - Integer, Long, Float, Double, or String constant to store
        Throws:
        IOException - if the method body cannot be finalized
        IllegalArgumentException - if the value is not one of the supported constant types
      • computeFrames

        public static int computeFrames​(ClassFile cf)
        Computes StackMapTable frames for every method in a class that carries code.
        Parameters:
        cf - class to process
        Returns:
        the number of methods that had frames computed
      • computeFrames

        public static boolean computeFrames​(ClassFile cf,
                                            String methodName,
                                            String descriptor)
        Computes StackMapTable frames for one method looked up by name and descriptor.
        Parameters:
        cf - class to search
        methodName - method name
        descriptor - method descriptor
        Returns:
        true if the method was found and frames were computed
      • computeFrames

        public static void computeFrames​(ClassFile cf,
                                         MethodEntry method)
        Computes StackMapTable frames for one method, doing nothing if it carries no code.
        Parameters:
        cf - class supplying the constant pool
        method - method to process