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.

Parameterize Advice Methods

See original GitHub issue

In a nutshell, I want to change parameter values of several methods using the same @Advice.OnMethodEnter and @Advice.OnMethodExit advice methods. Moreover, I want the advice methods to behave differently (change different parameters), depending on which method they instrument.

        new AgentBuilder.Default()
                .type(named("com.example.MyClass"))
                .transform((builder, typeDescription, classLoader, module) ->
                        builder
                                .visit(Advice.to(MyAdvice.class).on(named("doSomething1").and(...)))
                                .visit(Advice.to(MyAdvice.class).on(named("doSomething2").and(...)))
        );

Referring to the example, I want the @Advice.OnMethodEnter and @Advice.OnMethodExit method in MyAdvice to change different parameters for doSomething1() and doSomething2(), but by using the same logic. Therefore, I would need to parameterize MyAdvice.

Since the @Advice.OnMethodEnter and @Advice.OnMethodExit methods are static, are there any ByteBuddy-way solutions for parameterizing them? Otherwise I would need to use a global state and do the same kind of matching again which I’ve already done with the ElementMatchers, which bould be cumbersome.

Unfortunately, the interception api is not an option for me, since I don’t want to change the class schema.

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
AndreasMilicommented, Oct 18, 2018

Thanks for your blazing fast answers. I’m trying to wrap my head around on how to the custom annotation has to look like.

So basically I can achieve this:

int[] paramsToChange = // read from file
int[] paramsToChange2 = // read from file
                
builder.visit(Advice.withCustomMapping().bind(MyAnnotation.class, paramsToChange)
                   .to(MyAdvice.class).on(named("doSomething1")))
      .visit(Advice.withCustomMapping().bind(MyAnnotation.class, paramsToChange2)
                   .to(MyAdvice.class).on(named("doSomething2")));
@Advice.OnMethodEnter
public static void onEnter(@Advice.This Object obj, @Advice.Origin String method,
                           @Advice.AllArguments Object[] args, @MyAnnotation int[] params) {
}

With an annotation like that?

@Documented
@Retention(RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(ElementType.PARAMETER)
public @interface MyAnnotation {
// Use ArrayFactory here?
}

If I’m on the right track, I’ll try to figure it out and post an answer as soon as I got something working 😃

0reactions
raphwcommented, Oct 30, 2018

Please reopen the ticket if you have further questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parameterize Method - Refactoring.Guru
Create a new method with a parameter and move it to the code that's the same for all classes, by applying Extract Method....
Read more >
Parameterize a Function (Parameterization) - Statistics How To
The answer to this question isn't simple, because there are an infinite number of ways to parameterize any particular function (Du 2019).
Read more >
Parametrization (geometry) - Wikipedia
In mathematics, and more specifically in geometry, parametrization is the process of finding parametric equations of a curve, a surface, or, more generally, ......
Read more >
Chapter 6. Aspect Oriented Programming with Spring
Around advice is declared using the @Around annotation. The first parameter of the advice method must be of type ProceedingJoinPoint. Within the body...
Read more >
Spring AOP Aspect advice with method parameters
getAttribute method accepts only one String parameter. what I would like to do, I want "fixUrl" to be executed only when 'name' string...
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