Maven:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.19</version>
</dependency>
Gradle:
compile 'net.bytebuddy:byte-buddy:1.10.19'
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
public class ByteBuddyExample {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(ElementMatchers.named("toString"))
.intercept(FixedValue.value("Hello Byte Buddy!"))
.make()
.load(ByteBuddyExample.class.getClassLoader())
.getLoaded();
Object instance = dynamicType.newInstance();
System.out.println(instance.toString());
}
}