【发布时间】:2021-07-03 08:04:49
【问题描述】:
我正在尝试拦截或建议构造函数调用如下:
new AgentBuilder.Default()
//.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.type(ElementMatchers.nameMatches("org.openqa.selenium.firefox.FirefoxDriver"))
.transform((builder, typeDescription, classLoader, module) -> builder
.constructor(ElementMatchers.any())
.intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(FirefoxDriverInterceptor.class))))
//.intercept(SuperMethodCall.INSTANCE.andThen(Advice.to(FirefoxDriverConstructorAdvice.class))))
拦截器:
@RuntimeType
public static void intercept(@Origin Constructor<?> constructor) {
System.out.println("Intercepted: " + constructor.getName());
}
建议:
@Advice.OnMethodExit
public static void after(//@Advice.This Object thisObject,
@Advice.Origin String method,
@Advice.AllArguments Object[] args
) {
logger.info("<----- instantiated firefoxwebdriver: {}", method);
}
在尝试使用拦截器或建议时,异常抛出是:
java.lang.IllegalStateException: Cannot call super (or default) method for public org.openqa.selenium.firefox.FirefoxDriver(org.openqa.selenium.firefox.GeckoDriverService,org.openqa.selenium.Capabilities)
at net.bytebuddy.implementation.SuperMethodCall$Appender.apply(SuperMethodCall.java:102)
at net.bytebuddy.implementation.bytecode.ByteCodeAppender$Compound.apply(ByteCodeAppender.java:134)
让我知道任何指针。感谢您的帮助。
【问题讨论】:
-
我可以通过添加
.with(AgentBuilder.TypeStrategy.Default.REBASE)来解决这个问题 - 这适用于拦截构造函数.intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(FirefoxDriverInterceptor.class))。但是,在尝试以.intercept(SuperMethodCall.INSTANCE.andThen(Advice.to(FirefoxDriverConstructorAdvice.class))应用建议时,会打印以下异常跟踪:java.lang.VerifyError: Inconsistent stackmap frames at branch target 23 Exception Details: Location: org/openqa/selenium/firefox/FirefoxDriver.<init>(Lorg/openqa/selenium/firefox/XpiDr
标签: byte-buddy javaagents