I am new to bytebuddy. I tried to follow the link here to instrument a java system class, java.lang.String without success. Below is the code,
AgentBuilder agentBuilder = new AgentBuilder.Default()
.ignore(ElementMatchers.nameStartsWith("net.bytebuddy."))
.enableBootstrapInjection(instrumentation, temp);
agentBuilder
.type(ElementMatchers.nameEndsWith(".String"))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
return builder.method(ElementMatchers.nameContains("toString")).intercept(MethodDelegation.to(MyInterceptor.class));
}
}).installOn(instrumentation);
public static class MyInterceptor {
public static String intercept(@SuperCall Callable<String> zuper) throws Exception {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!! Hacking!!!!!!!!!!!!!!!!!!!!!!!!!");
return zuper.call();
}
}
from the ByteBuddy log, it seems the class has been transformed.
[Byte Buddy] DISCOVERY java.lang.String [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.String [null, null, loaded=true]
[Byte Buddy] COMPLETE java.lang.String [null, null, loaded=true]
After the instrumentation, when I invoke toString, for example,
system.out.println ("testString".toString());
I expect to see,
"!!!!!!!!!!!!!!!!!!!!!!!!!! Hacking!!!!!!!!!!!!!!!!!!!!!!!!!"
"testString"
However, I only saw
"testString"
Please let me know what the issue is. Thanks ahead.