如何使用 bytebuddy 获取方法参数

How to get arrgument of a method using bytebuddy

我正在尝试使用 java 应用程序的指标来了解性能。我使用 java 代理和 bytebuddy 获取 metrics.In 我的测试程序,我想检查的方法是 运行 几次。只有当它传递包含名称 'connector' 的参数时,我才需要获取指标。所以我想使用 bytebuddy 来获取它,为此我使用了 @AllArguments Object[] args。但我尝试使用我的 TimerAdvice class 而不是 运行。 这是我的代码

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);}}

这是我的 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;

}}

当我使用 @AllArguments Object[] args 时,只有 TimerAdvice 没有它就不能工作 perfectly.Is 我的代码中有这个问题吗? 任何帮助..

您可能导入了错误的注释。您要查找的注释是 @Advice.AllArguments.

这种命名冲突很不幸,但现在改变它已经太迟了。所有建议兼容的注释都带有前缀。 Tge others 意味着与方法委托一起使用。