[Question] How can I intercept same target method multi times in single transformer ?
See original GitHub issueHi! 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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!You can use
Advice.Localfor context passing. If you want to combine several “plugins”, I would suggest you to go for decoration rather than building such a matrix yourself.