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.

[Question] How can I intercept same target method multi times in single transformer ?

See original GitHub issue

Hi! I’m developing a APM system based on byte-buddy. sometimes I may have some different plugin to intercept same target method, and I find out in this situation only one plugin can work.

here is a demo

package com.example.demo;

import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.*;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

import static net.bytebuddy.matcher.ElementMatchers.named;

public class ByteBuddyTest {

    public static void main(String[] args) {

        Instrumentation instrumentation = ByteBuddyAgent.install();
        new AgentBuilder.Default()
                .with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
                .type(named("com.example.demo.ByteBuddyTest$TargetClass"))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
                        return builder
                                .method(named("targetMethod"))
                                .intercept(MethodDelegation.to(new InvokerA()))
                                .method(named("targetMethod"))
                                .intercept(MethodDelegation.to(new InvokerB()));
                    }
                })
                .installOn(instrumentation);


        TargetClass targetClass = new TargetClass();
        targetClass.targetMethod();

    }


    public static class TargetClass {

        public void targetMethod() {
            System.out.println("targetMethod invoked");
        }

    }

    public static class InvokerA {
        @RuntimeType
        public Object intercept(@This Object obj,
                                @AllArguments Object[] allArguments,
                                @SuperCall Callable<?> origin,
                                @Origin Method method) throws Throwable {

            System.out.println(this.getClass().getSimpleName());

            return origin.call();
        }
    }

    public static class InvokerB {
        @RuntimeType
        public Object intercept(@This Object obj,
                                @AllArguments Object[] allArguments,
                                @SuperCall Callable<?> origin,
                                @Origin Method method) throws Throwable {

            System.out.println(this.getClass().getSimpleName());

            return origin.call();
        }
    }

}

The output I expected :

InvokerB
InvokerA
targetMethod invoked

The actual output :

InvokerB
targetMethod invoked

What should I do to get InvokerA and InvokerB both to work ?

PS: Thanks for the awesome work! byte-buddy has helped me a lot.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
anylaincommented, Nov 6, 2019

I followed your advice and re-implemented my interceptor using Advice + Field. Now the system works very well, thank you very much for your help!

1reaction
raphwcommented, Nov 5, 2019

You can use Advice.Local for context passing. If you want to combine several “plugins”, I would suggest you to go for decoration rather than building such a matrix yourself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

2D Target Intercept Algorithm - Stack Overflow
This method is supposed to return the theta (angle) for the boss to throw his hook in order to intercept the player according...
Read more >
A Practical Approach to Linear Regression in Machine Learning
Regression models are supervised learning models that are generally used when the value to be predicted is of discrete or quantitative nature. One...
Read more >
sklearn.preprocessing.PolynomialFeatures
Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree....
Read more >
Remote Sensing | Free Full-Text | Towards Single-Component and ...
After radar signals preprocessing, a designed model which combines a convolutional neural network and transformer is used to accomplish multi-label ...
Read more >
Capturing the continuity and periodicity of time series for traffic ...
Traffic forecasting is a challenging problem due to the com- plexity of jointly modeling spatio-temporal dependencies at different scales. Recently, several ...
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