Search code examples
javabytecodebyte-buddy

Is it possible to intercept methods of a Executor Service that is initialised in a specific java class using bytebuddy


Using byte buddy, I am trying to intercept the method of executor service that is initialised in a known class. The following is a sample code.


public class TestClient {

    ExecutorService executorService = Executors.newFixedThreadPool(1);

    public Response get() {
        Future<Response> future = executorService.submit(this::call2);
        return future.get();
    }
}

The following intercepts all usage of ExecutoreService, including those in dependencies as well, which I want to avoid.

return ElementMatchers.isSubTypeOf(ExecutorService.class);

Is there a way to intercept the method of executor service initialised in any class of a specific package or class?


Solution

  • No, Byte Buddy can only alter byte code. If you could not implement it in Java, Byte Buddy is unlikely able to help.

    What you can do is to alter all constructors creating a different target object. In this object, override the relevant methods.

    To change constructors, have a look at MemberSubstitution.