Interface ClassCreator

All Superinterfaces:
AnnotatableCreator, GenericTyped, ModifiableCreator, SimpleTyped, TypeCreator, Typed, TypeParameterizedCreator
All Known Subinterfaces:
AnonymousClassCreator

public sealed interface ClassCreator extends TypeCreator, SimpleTyped, TypeParameterizedCreator permits AnonymousClassCreator (not exhaustive)
A creator for a class type.
  • Method Details

    • superClass

      ClassDesc superClass()
      Returns the superclass.
      Returns:
      the superclass
      See Also:
    • extends_

      void extends_(GenericType.OfClass genericType)
      Extend the given generic class.
      Parameters:
      genericType - the generic class (must not be null)
    • extends_

      void extends_(ClassDesc desc)
      Extend the given class.
      Parameters:
      desc - the class (must not be null)
    • implements_

      void implements_(GenericType.OfClass genericType)
      Implement a generic interface.
      Parameters:
      genericType - the generic interface type (must not be null)
    • implements_

      void implements_(ClassDesc interface_)
      Implement an interface.
      Parameters:
      interface_ - the descriptor of the interface (must not be null)
    • implements_

      default void implements_(Class<?> interface_)
      Implement an interface.
      Parameters:
      interface_ - the interface (must not be null)
    • extends_

      default void extends_(Class<?> clazz)
      Extend the given class.
      Parameters:
      clazz - the class (must not be null)
    • field

      Add an instance field to this class.
      Parameters:
      name - the field name (must not be null)
      builder - the builder (must not be null)
      Returns:
      the field variable (not null)
    • field

      default FieldDesc field(String name, Const initial)
      Add an instance field to this class.
      Parameters:
      name - the field name (must not be null)
      initial - the field's initial value (must not be null)
      Returns:
      the field variable (not null)
    • method

      Add an instance method to the class.
      Parameters:
      name - the method name (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • method

      default MethodDesc method(String name, MethodTypeDesc type, Consumer<InstanceMethodCreator> builder)
      Add an instance method to the class having the given predefined type.
      Parameters:
      name - the method name (must not be null)
      type - the method type (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • method

      default MethodDesc method(MethodDesc desc, Consumer<InstanceMethodCreator> builder)
      Add an instance method to the class having the same name and type as the given method.
      Parameters:
      desc - the original method descriptor (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • abstractMethod

      MethodDesc abstractMethod(String name, Consumer<AbstractMethodCreator> builder)
      Add an abstract instance method to the class.
      Parameters:
      name - the method name (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • abstractMethod

      default MethodDesc abstractMethod(String name, MethodTypeDesc type, Consumer<AbstractMethodCreator> builder)
      Add an abstract instance method to the class having the given predefined type.
      Parameters:
      name - the method name (must not be null)
      type - the method type (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • abstractMethod

      default MethodDesc abstractMethod(MethodDesc desc, Consumer<AbstractMethodCreator> builder)
      Add an abstract instance method to the class having the same name and type as the given method.
      Parameters:
      desc - the original method descriptor (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • nativeMethod

      MethodDesc nativeMethod(String name, Consumer<AbstractMethodCreator> builder)
      Add a native instance method to the class.
      Parameters:
      name - the method name (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • nativeMethod

      default MethodDesc nativeMethod(String name, MethodTypeDesc type, Consumer<AbstractMethodCreator> builder)
      Add a native instance method to the class having the given predefined type.
      Parameters:
      name - the method name (must not be null)
      type - the method type (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • nativeMethod

      default MethodDesc nativeMethod(MethodDesc desc, Consumer<AbstractMethodCreator> builder)
      Add a native instance method to the class having the same name and type as the given method.
      Parameters:
      desc - the original method descriptor (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • staticNativeMethod

      MethodDesc staticNativeMethod(String name, Consumer<AbstractMethodCreator> builder)
      Add a native static method to the class.
      Parameters:
      name - the method name (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • staticNativeMethod

      default MethodDesc staticNativeMethod(String name, MethodTypeDesc type, Consumer<AbstractMethodCreator> builder)
      Add a native static method to the class having the given predefined type.
      Parameters:
      name - the method name (must not be null)
      type - the method type (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • staticNativeMethod

      default MethodDesc staticNativeMethod(MethodDesc desc, Consumer<AbstractMethodCreator> builder)
      Add a native static method to the class having the same name and type as the given method.
      Parameters:
      desc - the original method descriptor (must not be null)
      builder - the method builder (must not be null)
      Returns:
      the built method's selector for invocation (not null)
    • constructor

      Add a constructor to the class.
      Parameters:
      builder - the constructor builder (must not be null)
      Returns:
      the built constructor's selector for invocation (must not be null)
    • constructor

      default ConstructorDesc constructor(MethodTypeDesc type, Consumer<ConstructorCreator> builder)
      Add a constructor to the class having the given predefined type. The type must have a void return type.
      Parameters:
      type - the method type (must not be null)
      builder - the constructor builder (must not be null)
      Returns:
      the built constructor's selector for invocation (must not be null)
    • constructor

      default ConstructorDesc constructor(ConstructorDesc desc, Consumer<ConstructorCreator> builder)
      Add a constructor to the class having the same type as the given constructor.
      Parameters:
      desc - the original constructor descriptor (must not be null)
      builder - the constructor builder (must not be null)
      Returns:
      the built constructor's selector for invocation (must not be null)
    • defaultConstructor

      default ConstructorDesc defaultConstructor()
      Add a default constructor to this class.
      Returns:
      the built constructor's selector for invocation (must not be null)
    • instanceInitializer

      void instanceInitializer(Consumer<BlockCreator> builder)
      Add a general instance initializer block to the type. A type may have many instance initializers; they will be concatenated in the order that they are added.
      Parameters:
      builder - the builder (must not be null)
    • abstract_

      default void abstract_()
      Add the abstract modifier flag to this creator.
      Throws:
      IllegalArgumentException - if this creator does not support the abstract modifier flag
    • generateEquals

      default void generateEquals(List<FieldDesc> fields)
      Generates a structural equals method in this class that compares given fields. The generated code is similar to what IDEs would typically generate from a template:
      1. Reference equality is tested. If this is identical to the other object, true is returned.
      2. Type of the other object is tested using instanceof. If the other object is not an instance of this class, false is returned.
      3. All fields are compared. Primitive types are compared using ==, object types are compared using Objects.equals, single-dimension arrays are compared using Arrays.equals, and multi-dimensional arrays are compared using Arrays.deepEquals. If one of the comparisons fails, false is returned.
      4. Otherwise, true is returned.

      If one of the fields doesn't belong to this class, an exception is thrown.

      Parameters:
      fields - fields to consider in the equals method (must not be null)
    • generateEqualsAndHashCode

      default void generateEqualsAndHashCode(List<FieldDesc> fields)
      Generates structural equals and hashCode methods in this class, based on given fields. The generated code is similar to what IDEs would typically generate from a template. See generateEquals(List) for description of the generated equals method. The hashCode method is generated like so:
      1. If no field is given, 0 is returned.
      2. Otherwise, a result variable is allocated with initial value of 1.
      3. For each field, a hash code is computed. Hash code for primitive types is computed using Integer.hashCode and equivalent methods, for object types using Objects.hashCode, for single-dimension arrays using Arrays.hashCode, and for multi-dimensional arrays using Arrays.deepHashCode. Then, the result is updated like so: result = 31 * result + fieldHashCode.
      4. At the end, the result is returned.

      If one of the fields doesn't belong to this class, an exception is thrown.

      Parameters:
      fields - fields to consider in the equals and hashCode methods (must not be null)
    • generateToString

      default void generateToString(List<FieldDesc> fields)
      Generates a toString methods in this class, based on given fields. The generated code is similar to what IDEs would typically generate from a template:
      1. An empty StringBuilder is allocated.
      2. Simple name of the class is appended.
      3. An opening parenthesis '(' is appended.
      4. For each field, its name is appended, followed by the equals sign '=', followed by the field value. Primitive types and object types are appended to the StringBuilder directly, Arrays.toString is used for single-dimension arrays, and Arrays.deepToString for multi-dimensional arrays. A comma followed by a space ", " are appended between fields.
      5. A closing parenthesis ')' is appended.
      6. The StringBuilder.toString() outcome is returned.

      If one of the fields doesn't belong to this class, an exception is thrown.

      Parameters:
      fields - fields to consider in the toString methods (must not be null)