[英]Bytebuddy: How to inject class that can be read by java.net.http.HttpClient#sendAsync
我正在尝试java.net.http.HttpClient#sendAsync
返回的CompletableFuture
所需的时间
// premain
public static void premain(String arguments, Instrumentation instrumentation) {
// ...inject helper class to bootloader
new AgentBuilder.Default()
.ignore(ElementMatchers.none())
.with(Listener.StreamWriting.toSystemOut().withErrorsOnly())
.type(hasSuperType(named("java.net.http.HttpClient")))
.transform((builder, typeDescription, classLoader,
module, protectionDomain) -> {
return builder
.visit(Advice.to(SendAsyncAdvice.class)
.on(hasMethodName("async")));
})
.asTerminalTransformation()
.installOn(instrumentation);
}
class SendAsyncAdvice {
@Advice.OnMethodExit()
public static void exit(
@Advice.Return(readOnly = false) CompletableFuture<HttpResponse<?>> future) {
future = future.whenComplete(new ResponseConsumer());
}
}
class ReponseConsumer implements BiConsumer<HttpResponse<?>, Throwable> {
@Override
public void accept(HttpResponse<?> arg0, Throwable arg1) {
System.out.println("HELLO");
}
}
我将ReponseConsumer
注入到引导加载程序中,我收到了这个错误
Exception in thread "main" java.lang.IllegalAccessError:
failed to access class io.hello.agent.SendAsyncAdvice
from class jdk.internal.net.http.HttpClientImpl
(io.hello.agent.SendAsyncAdvice is in unnamed module of loader
'bootstrap'; jdk.internal.net.http.HttpClientImpl
is in module java.net.http of loader 'platform')
只是想知道我是否在java.net.http
模块中提供了ReponseConsumer
class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.