Search code examples
javabyte-buddy

ByteBuddy: generate a static initializer?


ByteBuddy provides LoadedTypeInitializer but it is not persisted in generated bytecode.

Is it possible to generate a static initialisation block?

class Foo { static { /** Added code here **/ } }

Solution

  • Yes, on the builder:

    builder.invokeable(isTypeInitializer()).intercept(...)
    

    This way you can define it just as any static method.