带byteBuddy的乐器Java系统类

时间:2018-11-02 20:46:07

标签: java java-bytecode-asm byte-buddy javaagents

我是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"

请让我知道问题是什么。谢谢你。

0 个答案:

没有答案