Shehan Perera
Shehan Perera

Reputation: 349

How to get arrgument of a method using bytebuddy

I am trying to use metrics with a java application to find out performance. I used java agent with bytebuddy of get metrics.In my testing program the method that i want to check is running several time. only i need to get metrics when it passing a parameter contain name 'connector'. So i want to get this using bytebuddy and i used @AllArguments Object[] args for this . But i try to used this my TimerAdvice class not running. This is my code

class Agent {

public static void premain(String arguments, Instrumentation instrumentation) {
    System.out.println("Premain");
    new AgentBuilder.Default()
            .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
            .type((ElementMatchers.nameContains("ConnectorCallback")))
            .transform(
                    new AgentBuilder.Transformer.ForAdvice()
                            .include(MethodListner.class.getClassLoader())
                            .advice(ElementMatchers.any(), MethodListner.class.getName())
            ).installOn(instrumentation);}}

This my TimerAdvice class

public class TimerAdvice {


@Advice.OnMethodEnter
static void enter(@Advice.Origin String method , @AllArguments Object[] args)throws Exception  {

    if (changeMethodName(method).equals("BalConnectorCallback")) {
        //Metrics works
    }

}

@Advice.OnMethodExit
static void exit(@Advice.Origin String method, @AllArguments Object[] args) throws Exception {

    if (changeMethodName(method).equals("done")) {
       //Metrics works
        }
    }

 public static String changeMethodName(String method) {
    String newMethod = method.substring(0, method.lastIndexOf('('));
    newMethod = newMethod.substring(newMethod.lastIndexOf('.') + 1);
    //newMethod = newMethod.replace(".", " ");
    return newMethod;

}}

When i am using @AllArguments Object[] args this only TimerAdvice not working without it its work perfectly.Is this problem in my code ? Any Help..

Upvotes: 1

Views: 633

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

You are probably importing the wrong annotation. The annotation that you are looking for is @Advice.AllArguments.

This naming collission is unfortunate but it is too late to change that. All advice-comatible annotations are prefixed. Tge others are meant to be used with method delegation.

Upvotes: 1

Related Questions