Код: Выделить всё
public class HookAgent {
public static void premain(String arg, Instrumentation instrumentation) throws Exception {
AgentBuilder agentBuilder = new AgentBuilder.Default().ignore(none());
agentBuilder
.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(ElementMatchers.named("java.net.URL"))
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> {
System.out.println("Transforming class..." + typeDescription.getName());
return builder
.method(ElementMatchers.named("openConnection"))
.intercept(MethodDelegation.to(InterceptorClass.class));
})
//.with(new AgentBuilder.Listener.StreamWriting(System.err))
.installOn(instrumentation);
}
public static class InterceptorClass {
public static URLConnection openConnection() throws Exception {
System.out.println("openConnection() Intercepted!");
return null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... byte-buddy