Record Class ConstructorBody

java.lang.Object
java.lang.Record
io.micronaut.sourcegen.bytecode.core.ConstructorBody
Record Components:
implicitSuper - Whether the writer writes the call of the superclass's no-argument constructor first
hoistedCall - The constructor call moved to the front of the statements, if any
initializedFields - The instance fields whose initializers run after the constructor call, in declaration order
statements - The statements, without a hoisted call

@Internal public record ConstructorBody(boolean implicitSuper, @Nullable StatementDef hoistedCall, List<FieldDef> initializedFields, List<StatementDef> statements) extends Record
The body of a constructor as both bytecode writers write it: the constructor call first, then the initializers of the instance fields, then the rest of the statements.
  • A constructor calling no other constructor calls the superclass's no-argument one, which the writer writes itself: implicitSuper().
  • Field initializers run straight after the constructor call, so the call is moved to the front - hoistedCall() - only when there are any; otherwise the statements stay as they are, because the call may use locals defined before it.
  • A record has no fields of its own, only the ones backing its components, which its canonical constructor assigns.

Both writers run the field initializers once on a constructor delegating to this(...), as javac does - ConstructorBody.InitializersAfterThis.SKIP; ConstructorBody.InitializersAfterThis.RERUN is kept for a writer running them again.

Since:
2.3
  • Constructor Details

    • ConstructorBody

      public ConstructorBody(boolean implicitSuper, @Nullable StatementDef hoistedCall, List<FieldDef> initializedFields, List<StatementDef> statements)
      Creates an instance of a ConstructorBody record class.
      Parameters:
      implicitSuper - the value for the implicitSuper record component
      hoistedCall - the value for the hoistedCall record component
      initializedFields - the value for the initializedFields record component
      statements - the value for the statements record component
  • Method Details

    • of

      public static ConstructorBody of(@Nullable ObjectDef objectDef, List<StatementDef> statements, ConstructorBody.InitializersAfterThis mode)
      Normalizes the statements of a constructor.
      Parameters:
      objectDef - The definition declaring the constructor
      statements - The statements of the constructor
      mode - Whether the field initializers run after a delegation to this(...)
      Returns:
      The body
    • initializedFields

      public static List<FieldDef> initializedFields(ObjectDef objectDef)
      The instance fields of a class that have an initializer, in declaration order; a record has none of its own.
      Parameters:
      objectDef - The definition
      Returns:
      The fields
    • isConstructorInvocation

      public static boolean isConstructorInvocation(StatementDef statement)
      Whether a statement calls another constructor: super(...), or the deprecated form of an instance invocation of a constructor, on this or on super.
      Parameters:
      statement - The statement
      Returns:
      true for a constructor call
    • asStatements

      public List<StatementDef> asStatements()
      The whole body as statements: the call of the superclass's no-argument constructor where it is implicit, the hoisted call, the assignments of the field initializers and the statements.
      Returns:
      The statements
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • implicitSuper

      public boolean implicitSuper()
      Returns the value of the implicitSuper record component.
      Returns:
      the value of the implicitSuper record component
    • hoistedCall

      public @Nullable StatementDef hoistedCall()
      Returns the value of the hoistedCall record component.
      Returns:
      the value of the hoistedCall record component
    • initializedFields

      public List<FieldDef> initializedFields()
      Returns the value of the initializedFields record component.
      Returns:
      the value of the initializedFields record component
    • statements

      public List<StatementDef> statements()
      Returns the value of the statements record component.
      Returns:
      the value of the statements record component