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

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

问题描述

在java中,它有可能获得调用当前方法(在其中获得栈跟踪的方法)所述的类和方法

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?

我需要这个进行调试。

例如:

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?

(请注意,我需要能够做到这一点在运行时,不能真正改变基类的源代码,这将是我的调试类的功能,不知道源事前)

感谢。

推荐答案

我不相信这是可能使用标准的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.

另一种选择(更高级一些)是使用自定义的,字节code重写,类加载器保存原始参数,并将它们作为额外的参数给下一个方法。这可能会采取一两天来实现。合适的框架是 BCEL 或的 ASM

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天全站免登陆