Java:如何获取传递给调用此方法的方法的参数? [英] Java: how to get arguments passed to method that called this method?

查看:24
本文介绍了Java:如何获取传递给调用此方法的方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中,可以获取调用当前方法的类和方法(获取StackTrace的方法).

In java, it is possible to get the class and method that called the current method (the method in which you get the StackTrace).

我的问题是,我可以获取传递给调用此方法的方法的参数吗?

My question is, can I get the arguments that were passed to the method that called this method?

我需要这个用于调试目的.

I need this for debugging purposes.

例如:

baseClass {
   initialFunc(input) {
       var modifiedInput = input + " I modified you";
       otherClass.doSomething(modifiedInput);
   }
}

otherClass {
    doSomething(input)  {
         //GET THE ARGUMENTS PASSED TO THE METHOD OF THE CLASS THAT CALLED THIS METHOD
    }
}

可以从堆栈跟踪中获取此信息,还是有其他方法?

Can one get this information from the stacktrace, or are there other means?

(请注意,我需要能够在运行时执行此操作,并且不能实际更改 baseClass 的源,这将是我的调试类的一个功能,但事先不知道源)

谢谢.

推荐答案

我认为使用标准 Java API 不可能做到这一点.

I don't believe this is possible using the standard Java API.

您可以做的是使用 AspectJ,在调用方法上放置切点,保存参数,在被调用方法上放置切点并传递参数.

What you could do is to use AspectJ, place a point-cut at the calling method, save the arguments, place a point-cut at the called method and pass on the arguments.

另一个选项(稍微高级一点)是使用自定义的、字节码重写的类加载器,它保存原始参数,并将它们作为额外参数传递给下一个方法.这可能需要一两天的时间来实施.合适的框架是 BCELASM.

Another option (slightly more advanced) is to use a custom, bytecode-rewriting, class loader that saves the original arguments, and passes them on as extra arguments to the next method. This would probably take a day or two to implement. Suitable frameworks are BCEL or ASM.

这篇关于Java:如何获取传递给调用此方法的方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆