Class ByteCodeWriter

java.lang.Object
io.micronaut.sourcegen.bytecode.ByteCodeWriter

public final class ByteCodeWriter extends Object
Generates the classes directly by writing the bytecode.

Unlike a compiler, the writer synthesizes the bridge methods a class requires itself, derived from the declared supertypes. That derivation needs the supertype's method and generic metadata, so a supertype should be referenced through its definition — ClassTypeDef.of(ObjectDef) for another generated type, ClassTypeDef.of(Class) for a compiled class, or ClassTypeDef.of(ClassElement) for a source type. A generic supertype referenced only by its name, via ClassTypeDef.of(String), carries no such metadata: no bridges can be derived from it, and calls through its erased signatures may fail with an AbstractMethodError at runtime.

Since:
1.5
Author:
Denis Stepanov
  • Constructor Details

    • ByteCodeWriter

      public ByteCodeWriter()
    • ByteCodeWriter

      public ByteCodeWriter(boolean checkClass, boolean visitMaxs)
  • Method Details

    • writeObject

      public void writeObject(org.objectweb.asm.ClassVisitor classVisitor, ObjectDef objectDef)
      Write an object.
      Parameters:
      classVisitor - The class visitor
      objectDef - The object definition
    • writeObject

      public void writeObject(org.objectweb.asm.ClassVisitor classVisitor, ObjectDef objectDef, @Nullable ClassTypeDef outerType)
      Write an object.
      Parameters:
      classVisitor - The class visitor
      objectDef - The object definition
      outerType - The outer type
    • writeField

      public void writeField(org.objectweb.asm.ClassVisitor classVisitor, ObjectDef objectDef, FieldDef fieldDef)
      Write an enum.
      Parameters:
      classVisitor - The class visitor
      objectDef - The object definition
      fieldDef - The field definition
    • writeInterface

      public void writeInterface(org.objectweb.asm.ClassVisitor classVisitor, InterfaceDef interfaceDef, @Nullable ClassTypeDef outerType)
      Write an interface.
      Parameters:
      classVisitor - The class visitor
      interfaceDef - The interface definition
      outerType - The outer type
    • writeRecord

      public void writeRecord(org.objectweb.asm.ClassVisitor classVisitor, RecordDef recordDef)
      Write a record.
      Parameters:
      classVisitor - The class visitor
      recordDef - The record definition
    • writeRecord

      public void writeRecord(org.objectweb.asm.ClassVisitor classVisitor, RecordDef recordDef, @Nullable ClassTypeDef outerType)
      Write a record.

      The components of the record are expanded the way a compiler expands them: a record component, a private final field, the canonical constructor, an accessor per component, and equals, hashCode and toString linked through java.lang.runtime.ObjectMethods#bootstrap. A member the definition declares itself is kept, replacing the one that would have been generated.

      Parameters:
      classVisitor - The class visitor
      recordDef - The record definition
      outerType - The outer type
    • writeClass

      public void writeClass(org.objectweb.asm.ClassVisitor classVisitor, ClassDef classDef)
      Write an interface.
      Parameters:
      classVisitor - The class visitor
      classDef - The class definition
    • writeClass

      public void writeClass(org.objectweb.asm.ClassVisitor classVisitor, ClassDef classDef, @Nullable ClassTypeDef outerType)
      Write an interface.
      Parameters:
      classVisitor - The class visitor
      classDef - The class definition
      outerType - The outer type
    • write

      public byte[] write(ObjectDef objectDef)
      Writes the bytecode of generated class.
      Parameters:
      objectDef - The object definition.
      Returns:
      The bytes
    • write

      public byte[] write(ObjectDef objectDef, @Nullable ClassTypeDef outerType)
      Writes the bytecode of generated class.
      Parameters:
      objectDef - The object definition.
      outerType - The outer type.
      Returns:
      The bytes