Search code examples
byte-buddy

Is there a way to intercept java.io.File class in java agent


all

For some reason, we want to intercept java.io.File class to add some log and do some check.

I tried to create a java agent with Byte Buddy to use Advice to do such things for some other class.

But it never work for class which is already loaded before agent premain. I am on byte-byddy 1.10.5

I also did some try use redefinition, or transform, but it's not work as well, not sure if I did something wrong.

Is there a way to achieve this?


Solution

  • You need to enable a retransformation of existing classes. Also, you need to define a custom ignore matcher as boot classes are not instrumented by default.

    agentBuilder
      .with(RedefinitionStrategy.RETRANSFORMATION)
      .disableClassFormatChanges()
      .ignore(...)