【问题标题】:byte-buddy Advice or Intercept Constructor invocationbyte-buddy Advice 或 Intercept 构造函数调用
【发布时间】: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.&lt;init&gt;(Lorg/openqa/selenium/firefox/XpiDr

标签: byte-buddy javaagents


【解决方案1】:

您在此处混合了不相关的 AdviceMethodDelegation。它们有一些共同的概念,但你不能混合注释。

但是,您可以将建议包装在委托周围:

Advice.to(...).wrap(MethodDelegation.to(...))

【讨论】:

    猜你喜欢
    • 2019-02-03
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多