我正在尝试使用 ByteBuddy 重新定义 java.lang 包上的类,例如 String.class 或 Integer.class,但没有成功。我的问题是这是否可能?
这是我在我的 java 代理中尝试的代码:
public static void premain(String agentArgs, Instrumentation inst) {
new AgentBuilder.Default()
.type(named("java.lang.String"))
.transform((builder, typeDescription, classLoader) ->
builder.method(named("toString"))
.intercept(FixedValue.value("toString() got hacked!")))
.with(AgentBuilder.Listener.StreamWriting.toSystemOut())
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.installOn(inst);
}
当我检查日志的输出时,我看到的关于 String 类的内容是:
[Byte Buddy] IGNORE [[Ljava.lang.String; [null, null]
[Byte Buddy] COMPLETE [[Ljava.lang.String; [null, null]
这是否意味着 ByteBuddy 没有重新定义 String 类?这可能吗?
非常感谢。
最佳答案
是的,Byte Buddy 可以重新定义任何类,但默认情况下,引导类被忽略。您可以通过定义自定义忽略匹配器或将其全部删除来覆盖此默认设置:
AgentBuilder agentBuilder = new AgentBuilder.Default().ignore(none());
但是,我强烈建议您不要弄乱引导类,尤其是 String
类。很多代码都对 toString
类做出了强有力的假设。
大多数 JVM 不允许您在重新定义类时更改类文件格式,这就是您应该启用 .disableClassFormatChanges()
选项的原因。这样做,您不能再添加方法或字段,此时您应该考虑使用 Advice
类而不是标准拦截器。
关于java - 使用 ByteBuddy 重新定义 java.lang 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433232/