Class ByteCodeWriter

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

public final class ByteCodeWriter extends Object
Writes Sourcegen definitions using the JDK 25 classfile toolchain.

The JDK backend lowers supported definitions directly with the JDK 25 ClassFile API, including stack maps and invokedynamic call sites. Definitions that require a source-level compatibility lowering use an in-memory Java 17 compiler fallback. This is deliberately kept in this JDK-only artifact and introduces no ASM into its runtime graph.

The resulting class is normalized through the JDK ClassFile API, pinned to Java 17, and verified before it is returned.

Since:
2.2
Author:
Graeme Rocher
  • Constructor Details

    • ByteCodeWriter

      public ByteCodeWriter()
      Creates a writer using the current runtime class path.
    • ByteCodeWriter

      @Internal public ByteCodeWriter(List<Path> sourcePath, List<Path> classPath)
      Creates a writer with the paths visible to an annotation-processing invocation.
      Parameters:
      sourcePath - Source roots containing types still being compiled
      classPath - Classpath entries containing already compiled types
    • ByteCodeWriter

      @Internal public ByteCodeWriter(List<Path> sourcePath, List<Path> classPath, boolean verify)
      Creates a writer with explicit paths and verification policy.
      Parameters:
      sourcePath - Source roots containing types still being compiled
      classPath - Classpath entries containing already compiled types
      verify - Whether to run ClassFile verification
    • ByteCodeWriter

      @Internal public ByteCodeWriter(List<Path> sourcePath, List<Path> classPath, boolean verify, @Nullable CompilationTypes compilationTypes)
      Creates a writer that can resolve types of the compilation it is running in.

      A generated class often references types that are still being compiled and so have no class file to read. Supplying the current compilation's type lookup, such as visitorContext::getClassElement, lets the writer resolve their hierarchy when it computes stack maps.

      Parameters:
      sourcePath - Source roots containing types still being compiled
      classPath - Classpath entries containing already compiled types
      verify - Whether to run ClassFile verification
      compilationTypes - Looks up a type of the current compilation by binary name
  • Method Details

    • write

      public byte[] write(ObjectDef objectDef)
      Writes a top-level definition.
      Parameters:
      objectDef - The object definition
      Returns:
      Java 17 classfile bytes
    • write

      public byte[] write(ObjectDef objectDef, @Nullable ClassTypeDef outerType)
      Writes a definition which is a member of the supplied outer type.
      Parameters:
      objectDef - The object definition
      outerType - The outer type, or null for a top-level definition
      Returns:
      Java 17 classfile bytes
    • writeAll

      @Internal public Map<String,byte[]> writeAll(ObjectDef objectDef)
      Writes a definition and all member classes produced for it.
      Parameters:
      objectDef - The object definition
      Returns:
      classfile bytes keyed by binary class name
      Since:
      2.2
    • writeAll

      @Internal public Map<String,byte[]> writeAll(ObjectDef objectDef, @Nullable ClassTypeDef outerType)
      Writes a definition and all member classes produced for it.
      Parameters:
      objectDef - The object definition
      outerType - The outer type, or null for a top-level definition
      Returns:
      classfile bytes keyed by binary class name
      Since:
      2.2