Class IOJavaClassWriter

  • All Implemented Interfaces:
    JavaClassWriter

    public final class IOJavaClassWriter
    extends Object
    implements JavaClassWriter
    This implementation of the JavaClassWriter interface is based on simple StringBuilder "println" type statements.

    The order of the generated code in this implementation depends on the initialization. The default order is to accept fields and methods in any order and generate all fields together and all methods together (by member category). Specifying the ordered parameter in the constructor will lead to a slightly different order: generation of fields and methods interspersed among each other in exactly the order they were added (no member categories).

    Author:
    raccah
    • Constructor Detail

      • IOJavaClassWriter

        public IOJavaClassWriter()
        Creates a new instance of IOJavaClassWriter which maintains the order of member input but groups them by category.
        See Also:
        IOJavaClassWriter(boolean)
      • IOJavaClassWriter

        public IOJavaClassWriter​(boolean maintainCategories)
        Creates a new instance of IOJavaClassWriter in which the order of the generated code depends on the specified flag.
        Parameters:
        maintainCategories - If true, the order of members is preserved within category groups. If false, the generation of fields and methods will be interspersed among each other in exactly the order they were added with no member categories.
    • Method Detail

      • setClassDeclaration

        public void setClassDeclaration​(int modifiers,
                                        String className,
                                        String[] comments)
        Sets the information for the class declaration including modifiers, name, and comments. Note that the name must not be fully qualified.
        Specified by:
        setClassDeclaration in interface JavaClassWriter
        Parameters:
        modifiers - The modifier flags for this class.
        className - The (non-qualified) name of this class.
        comments - The comments shown just above the class declaration. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.
        See Also:
        Modifier
      • setSuperclass

        public void setSuperclass​(String name)
        Sets the superclass of this class. Note that the name format must be package style (that is - it can contain . but not / or $).
        Specified by:
        setSuperclass in interface JavaClassWriter
        Parameters:
        name - The name of the superclass.
      • addInterface

        public void addInterface​(String name)
        Adds an interface to the list of those implemented by this class.
        Specified by:
        addInterface in interface JavaClassWriter
        Parameters:
        name - The name of the interface.
      • addField

        public void addField​(String name,
                             int modifiers,
                             String type,
                             String initialValue,
                             String[] comments)
        Adds a field to the list of those declared by this class. Note that the type format must be package style (that is - it can contain . but not / or $).
        Specified by:
        addField in interface JavaClassWriter
        Parameters:
        name - The name of the field.
        modifiers - The modifier flags for this field.
        type - A string representing the type of this field.
        initialValue - A string representing the initial value of this field.
        comments - The comments shown just above the declaration of this field. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.
        See Also:
        Modifier
      • addInitializer

        public void addInitializer​(boolean isStatic,
                                   String[] body,
                                   String[] comments)
        Adds an initializer to this class.
        Specified by:
        addInitializer in interface JavaClassWriter
        Parameters:
        isStatic - True if this is a static initializer, false otherwise.
        body - The implementation block of the initializer. The body of the implementation is passed as an array so the line separators can be added by the implementation.
        comments - The comments shown just above the initializer block. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.
      • addConstructor

        public void addConstructor​(String name,
                                   int modifiers,
                                   String[] parameterNames,
                                   String[] parameterTypes,
                                   String[] exceptions,
                                   String[] body,
                                   String[] comments)
        Adds a constructor to this class. Note that the type format in the parameter type strings must be package style (that is - it can contain . but not / or $).
        Specified by:
        addConstructor in interface JavaClassWriter
        Parameters:
        name - The name of the constructor - should be the same as the name of the class.
        modifiers - The modifier flags for this constructor.
        parameterNames - A list of parameter names.
        parameterTypes - A list of parameter types.
        exceptions - A list of exceptions.
        body - The implementation block of the constructor. The body of the implementation is passed as an array so the line separators can be added by the implementation.
        comments - The comments shown just above the constructor. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.
        See Also:
        Modifier
      • addMethod

        public void addMethod​(String name,
                              int modifiers,
                              String returnType,
                              String[] parameterNames,
                              String[] parameterTypes,
                              String[] exceptions,
                              String[] body,
                              String[] comments)
        Adds a method to this class. Note that the type format in the return type and parameter type strings must be package style (that is - it can contain . but not / or $).
        Specified by:
        addMethod in interface JavaClassWriter
        Parameters:
        name - The name of the method.
        modifiers - The modifier flags for this method.
        returnType - A string representing the return type of this method.
        parameterNames - A list of parameter names.
        parameterTypes - A list of parameter types.
        exceptions - A list of exceptions.
        body - The implementation block of the method. The body of the implementation is passed as an array so the line separators can be added by the implementation.
        comments - The comments shown just above the method. The comments are passed as an array so the line separators can be added by the implementation. Note that not all implementations will choose to make use of this comment.
        See Also:
        Modifier
      • addClass

        public void addClass​(JavaClassWriter classWriter)
        Adds an inner class to this class.
        Specified by:
        addClass in interface JavaClassWriter
        Parameters:
        classWriter - The definition of the inner class.
      • toString

        public String toString()
        Returns a string representation of this object.
        Overrides:
        toString in class Object
        Returns:
        The string representation of the generated class.