Class BytecodeNormalizer

java.lang.Object
org.ek9lang.compiler.backend.jvm.BytecodeNormalizer

public final class BytecodeNormalizer extends Object
Normalizes JVM bytecode output from javap for stable test comparisons.

Normalization removes fragile elements like constant pool indices, line number tables, and debug information while preserving essential instruction sequences and method signatures.

This utility is specifically designed for testing bytecode generation by enabling reliable comparison of expected vs actual bytecode output.

  • Method Details

    • normalize

      public static String normalize(byte[] classBytes)
      Normalize bytecode from .class file bytes with default settings. Includes debug information (LineNumberTable and SourceDebugExtension).
      Parameters:
      classBytes - Compiled .class file bytes
      Returns:
      Normalized bytecode text suitable for test comparison
      Throws:
      RuntimeException - if normalization fails
    • normalize

      public static String normalize(byte[] classBytes, boolean includeLineNumberTable, boolean includeSourceDebugExtension)
      Normalize bytecode from .class file bytes with configuration options.
      Parameters:
      classBytes - Compiled .class file bytes
      includeLineNumberTable - Include LineNumberTable debug information
      includeSourceDebugExtension - Include SourceDebugExtension (SMAP) information
      Returns:
      Normalized bytecode text suitable for test comparison
      Throws:
      RuntimeException - if normalization fails
    • executeJavap

      static String executeJavap(Path classFile) throws Exception
      Execute javap on class file and capture output.

      Merges stderr into stdout to prevent the classic Process deadlock where reading stdout blocks while stderr pipe buffer fills up. The Process is destroyed in a finally block to release native resources (file descriptors) which otherwise leak and cause "Too many open files" on CI runners after 200+ invocations.

      Package-private for RawJavap, which runs javap without the normalization.

      Parameters:
      classFile - Path to .class file
      Returns:
      Raw javap output
      Throws:
      Exception - if javap execution fails