Deepak Chaudhary
Deepak Chaudhary

Reputation: 162

byte-buddy Advice or Intercept Constructor invocation

I am trying to intercept or advice constructor invocation as following:

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))))

Interceptor:

@RuntimeType
public static void intercept(@Origin Constructor<?> constructor) {
    System.out.println("Intercepted: " + constructor.getName());
}

Advice:

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

on trying with interceptor or advice, exception throws is:

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)

Let me know for nay pointers. Thank you for your help.

Upvotes: 1

Views: 919

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44077

You are blending Advice and MethodDelegation here which are not related. They have some common concepts, but you cannot mix the annotations.

You can however wrap advice around a delegation:

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

Upvotes: 3

Related Questions