我知道方法参数很容易捕获,仅使用@ Advice.AllArguments。从建议的方法中获取返回数据是否也很​​容易?我的目标是根本不修改方法,而只是“对进来的东西进行拍照”并将其写入文件。返回类型主要是javax的Response,但有时是原语。

public class MyAdviser {

  @Advice.OnMethodEnter
  public static void onEnter(@Advice.This Object obj, @Advice.Origin String method,
        @Advice.AllArguments Object[] args) throws IOException {

    FileWriter fw = new FileWriter("AgentData.txt", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw);
    out.println(method);
    out.println("Arguments : " + Arrays.deepToString(args));
    out.println("---------------------------------------------");
    out.close();

    final Logger LOG = LoggerFactory.getLogger(obj.getClass());
    LOG.info("Method name : " + method + " Object  : " + obj + "   " + obj.getClass());
    LOG.info("Arguments : " + Arrays.deepToString(args));

  }

  @Advice.OnMethodExit
  public static void onExit(@Advice.Origin String method) {


  }
}

最佳答案

就像Advice类的javadoc中提到的那样:

@Advice.OnMethodExit
public static void onExit(@Advice.Origin String method,
                          @Advice.Return Object returned,
                          @Advice.Thrown Throwable thrown) {
  // thrown will be null if no exception was thrown, returned if it was
}

09-05 10:06