Byte Buddy
  • is a Java - Bytecode Manipulation Library
  • is a rather new library but provides any functionality that CGLIB or Javassist provides and much more
  • comes with an expressive domain-specific language that allows for very readable code

Code Example

Class<?> dynamicType = new ByteBuddy()
  .subclass(Object.class)
  .method(ElementMatchers.named("toString"))
  .intercept(FixedValue.value("Hello World!"))
  .make()
  .load(getClass().getClassLoader())
  .getLoaded();
 
assertThat(dynamicType.newInstance().toString(), is("Hello World!"));

TODO

https://www.baeldung.com/byte-buddy

Resources