首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字节伙伴建议或截取构造函数调用

字节伙伴建议或截取构造函数调用
EN

Stack Overflow用户
提问于 2021-07-03 16:04:50
回答 1查看 69关注 0票数 1

我尝试截取或通知构造函数调用,如下所示:

代码语言:javascript
复制
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))))

拦截器:

代码语言:javascript
复制
@RuntimeType
public static void intercept(@Origin Constructor<?> constructor) {
    System.out.println("Intercepted: " + constructor.getName());
}

建议:

代码语言:javascript
复制
@Advice.OnMethodExit
public static void after(//@Advice.This Object thisObject,
                         @Advice.Origin String method,
                         @Advice.AllArguments Object[] args
) {
    logger.info("<----- instantiated firefoxwebdriver: {}", method);
}

在尝试拦截器或通知时,异常抛出是:

代码语言:javascript
复制
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)

请让我知道反对的意见。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-07-13 04:16:35

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

但是,您可以围绕委托提供建议:

代码语言:javascript
复制
Advice.to(...).wrap(MethodDelegation.to(...))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68234353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档