Open sagarvyass opened 1 month ago
You would need to enable retransformation using RetransformationStrategy.RETRANSFORM
, and likely set disableClassFormatChanges()
. Runtime
will already be loaded when the JVM dispatches the agent.
update to this, still not working
public static void premain(String arg, Instrumentation inst) throws Exception {
File temp = Files.createTempDirectory("tmp").toFile();
ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst).inject(Collections.singletonMap(
new TypeDescription.ForLoadedType(MyInterceptor.class),
ClassFileLocator.ForClassLoader.read(MyInterceptor.class)));
new AgentBuilder.Default()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.disableClassFormatChanges()
.ignore(ElementMatchers.nameStartsWith("net.bytebuddy."))
.type(ElementMatchers.nameEndsWith("java.lang.Runtime"))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,JavaModule javaModule, ProtectionDomain protectionDomain) {
return builder.method(ElementMatchers.named("getRuntime")).intercept(MethodDelegation.to(MyInterceptor.class));
}
}).installOn(inst);
}
public static class MyInterceptor {
public static String intercept(@SuperCall Callable<String> zuper) throws Exception {
System.out.println("Intercepted!");
return zuper.call();
}
}
If you add a listener, you will see an error message. With retransofation, you cannot apply all forms of transformations. You will likely need to switch over to Advice
.
would the advice be able to intercept core java packages like Runtime.exec() ?
Certainly.
I have tried LTW using aspectJ weaver as a java agent, still couldn’t intercept. Is there some resource you could help with?
If you look up YouTube and Vimeo, I have given a row of introductions to the topic. Looking at Byte Buddy's tests is also a good option.
can you please share the links?
For example: https://www.youtube.com/watch?v=o9NVLXKRKeY
not able to intercept java.lang.Runtime