0

Im struggling to figure out how to access a protected field using ByteBuddy and what approach to take.

Background: Id like to intercept a class from a dependency I have on my class path. Im creating a junit test report and need to intercept org.assertj.core.api.AbstractAssert in order to capture the actual field value from that class.

Iv tried...

 Class<?> type = new ByteBuddy()
      .redefine(AbstractAssert.class)
      .visit(Advice.to(MyAdvices.class).on(ElementMatchers.isMethod()))
      .make().load(AbstractAssert.class.getClassLoader()).getLoaded();

    log.info(type.getFields().toString());
Class already loaded: class org.assertj.core.api.AbstractAssert 

Im not even sure if this is the correct approach for what Im trying to achieve

Any help appreciated

rogger2016
  • 821
  • 3
  • 11
  • 28

1 Answers1

1

You cannot just change a class on the JVM. Either you would need to use a different class loader and load the class regularly, or a Java agent. For an agent, Byte Buddy supplies a RedefinitionStrategy which you can use to integrate this in your above example. Also have a look at the byte-buddy-agent project.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192