import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;
public class ByteBuddyAgentExample {
public static void premain(String arguments, Instrumentation instrumentation) {
new AgentBuilder.Default()
.type(ElementMatchers.any())
.transform((builder, type, classLoader, module) ->
builder.method(ElementMatchers.named("greeting"))
.intercept(FixedValue.value("Hello, Byte Buddy Agent!")))
.installOn(instrumentation);
}
public static class GreetingClass {
public String greeting() {
return "Hello, World!";
}
}
public static void main(String[] args) {
ByteBuddyAgent.attach();
System.out.println(new GreetingClass().greeting());
}
}