我是bytebuddy的新手。我试图遵循the link来检测Java系统类java.lang.String失败。下面是代码,
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();
}
}
从ByteBuddy日志中的看来,该类已被转换。
[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]
检测后,例如,当我调用toString时,
system.out.println ("testString".toString());
我希望看到
"!!!!!!!!!!!!!!!!!!!!!!!!!! Hacking!!!!!!!!!!!!!!!!!!!!!!!!!"
"testString"
但是,我只看到
"testString"
请让我知道问题是什么。谢谢你。