0

I've created the following agent which supposedly rebases every class and transforms every method, using the ByteBuddy example LogInterceptor (which just prints the name of the method being called):

public static void main(String[] args) {
    ByteBuddyAgent.installOnOpenJDK();

    new AgentBuilder.Default()
            .rebase(ElementMatchers.any())
            .transform((builder, typeDescription) -> builder
                            .method(ElementMatchers.any())
                            .intercept(
                                    MethodDelegation
                                            .to(LogInterceptor.class)
                                            .andThen(SuperMethodCall.INSTANCE)
                            )
            )
            .installOnByteBuddyAgent();

After doing this I proceed to call my code, which for example does:

new JFXPanel()

...to initialize JavaFX. However, I don't see my interceptor printing any methods inside JavaFX. But what is worse, I don't see ByteBuddy printing any methods on some of my classes either, although it does in some of them completely at random (or so it seems).

Isn't my agent supposed to capture every single method for every single class? This is obviously not what I want to do, but as a way of debugging why the methods I'm interested in are not being instrumented.

Edu Garcia
  • 453
  • 7
  • 21

2 Answers2

0

It seems the visibility of the LogInterceptor and its methods matter, so making that public instead of package protected as before makes things work. However, that doesn't really explain why some of my methods were being called properly, but this is the answer to my problem in any case.

Edu Garcia
  • 453
  • 7
  • 21
0

Byte Buddy makes sure that methods are only called from an intercepted class if they are visible to it. A public method that is declared by a package-private class but by no public super class is for example never visible to a class because a declaring class is part of the invocation signature. Otherwise, this would result in an IllegalAccessException at runtime.

If no interceptor methods are visible to an intercepted class, the instrumentation fails. You can register a listener to the agent to be made aware of such cases.

Note: You are right, though, that Byte Buddy currently considers a public method of a package-private type to be invisible even if it overrides a method of another, public type. That is not intuitive and I am going to change this behavior.

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