I am trying to replace Java bootstrap class method System.currentTimeMillis with ByteBuddy. However after spent more than one day I still don't get the expected result. Following is my code:-
The inteceptor class in bootstrap.jar used for appendToBootstrapClassLoaderSearch:
public class SystemTimeInterceptor {
public static long currentTimeMillis() {
return 1L;
}
The agent class:
instrumention.appendToBootstrapClassLoaderSearch(new JarFile("path/to/bootstrap.jar"));
final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
new AgentBuilder.Default()
.enableNativeMethodPrefix("foo")
.with(byteBuddy)
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.ignore(none())
.type(ElementMatchers.named("java.lang.System"))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,
JavaModule module) {
return builder.method(ElementMatchers.named("currentTimeMillis")).intercept(
MethodDelegation.to(SystemTimeInterceptor.class));
}
}).installOn(inst);
And after I start my test main method with specifying the javaagent argument, I still see the current time in milliseconds in the output. With above way, I tried to redefine System.setSecurityManager and it works. I also tried to remove the strategy settings in order to use the default. But it doesn't work.