import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
public class ByteBuddyAgentExample {
public static void main(String[] args) {
try {
Class<?> dynamicClass = new ByteBuddy()
.subclass(Object.class)
.method(ElementMatchers.named("toString"))
.intercept(FixedValue.value("Enhanced toString method"))
.make()
.load(ByteBuddyAgentExample.class.getClassLoader())
.getLoaded();
System.out.println(dynamicClass.newInstance().toString());
e.printStackTrace();
}
}
}
-javaagent:path/to/byte-buddy-agent.jar