question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customizable advice dispatcher

See original GitHub issue

AFAIK, there are currently two ways advices are dispatched.

By default, advice methods are inlined in to the instrumented method.

public class InlinedAdvice {
    @Advice.OnMethodEnter
    public static void onFoo(@Advice.Argument(0) boolean bar) {
        System.out.println("foo: " + bar);
    }
}

public class InstrumentedClass {
    public void foo(boolean bar) {
        System.out.println("foo: " + bar);
    }
}

If the inline property is set to false, only a static method call is inserted into the instrumented method that calls the advice method.

public class DelegatedAdvice {
    @Advice.OnMethodEnter(inline = false)
    public static void onFoo(@Advice.Argument(0) boolean bar) {
        System.out.println("foo: " + bar);
    }
}


public class InstrumentedClass {
    public void foo(boolean bar) {
        InlinedAdvice.onFoo(bar);
    }
}

I’d like to have a way to customize the dispatching.

public class CustomDelegationAdvice {
    @Advice.OnMethodEnter(inline = false)
    @Advice.Dispatcher(CustomDispatcher.class)
    public static void onFoo(@Advice.Argument(0) boolean bar) {
        System.out.println("foo: " + bar);
    }
}


public class InstrumentedClass {
    public void foo(boolean bar) {
        MethodHandleDispatcher
            .getMethodHandle(getClass().getClassLoader(), "CustomDelegationAdvice#onFoo")
            .invoke(bar);
    }
}

MethodHandleDispatcher is a registry where I would make sure to add method handles to all advice methods. Currently, I do that style of dispatching manually by creating an inlined advice that contains the MethodHandle lookup and invocation. It would be great if I could just write the advice as it was a inline = true advice and if Byte Buddy could inject the method handle lookup and invocation in a similar way that it currently does for non-inlined advices via net.bytebuddy.asm.Advice.Dispatcher.Delegating.

This is in the context of this PR: https://github.com/elastic/apm-agent-java/pull/1090

Is that something that would be possible?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
raphwcommented, Apr 11, 2020

I’ll look into the possibility of adding additional semantics to a method return value.

As for returning null: the bootstrap mechanism does not support this. You can however bind a constant call site that drops all arguments and returns null, a primitive 0 or nothing depending on the advice methods return type. Have a look at the lookup API for creating such a constant handle.

0reactions
raphwcommented, Apr 12, 2020

Any constructor of an instance of CallSite can be used as a bootstrap method. You could for example subclass constant call site for doing so.

And yes, the bootstrap method needs to be visible (and exported) . Otherwise you could breach into protected code using invokedynamic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Dispatcher - Etsy
Check out our custom dispatcher selection for the very best in unique or custom, handmade pieces from our shops.
Read more >
Customizable Training » CritiCall 911 Dispatcher ...
Self-guided, customizable, dispatcher training program focusing on speech and protocols - job domains critical to maintaining public safety.
Read more >
Service Dispatch Software with Customizable Branding - GetApp
View the best Service Dispatch software with Customizable Branding in 2022. ... GPS tracking allows you to help technicians get more done in...
Read more >
Customize the Dispatcher Console - Salesforce Help
Make the dispatcher console work for you! Adjust the time frame and contents of the appointment list and Gantt, customize each section's layout, ......
Read more >
59+ Dispatcher Gifts on Zazzle
Shop for the perfect dispatcher gift from our wide selection of designs, or create your own personalized gifts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found