英文:
Getting the local variables of the calling method
问题
我在Java中使用一个库,遇到了以下问题,假设我有:
public void methodA(ObjectA o1, ObjectB o2)
{
//代码
Object o3 = new ObjectA();
methodB(o3);
}
public void methodB(ObjectA o3)
{
//代码
}
我不能修改方法A,也不能修改方法B的参数,但我可以更改B的主体,以及对象A和B的类,我需要一种方法从MethodB中获取o1的值。
据我所了解,JVM将局部变量存储在调用堆栈上的堆栈帧中(需要确认),所以我想也许有一种方法可以使用Java或C++(或如果确实必要,可以使用其他语言)来获取这些值,所有这些都是假设性的,我实际上不知道是否可能。
我也对不涉及此类事物的新想法和可能性持开放态度。
英文:
I'm working with a library in Java and I have encountered the following problem, let's say I have:
public void methodA(ObjectA o1, ObjectB o2)
{
//Code
Object o3 = new ObjectA();
methodB(o3);
}
public void methodB(ObjectA o3)
{
//Code
}
I cannot modify method A nor the parameters of method B, but I can change the body of B as well the classes of the objects A and B, I need a way to get the value of o1 from MethodB.
As far as I understand the JVM stores local variables in stack frames on the call stack (needs confirmation), so I thought that maybe there is a way to use Java or C++ (or another language if really necessary) to get the values, all of this is hypotetical I don't actually now if it's possible.
I'm also open to new ideas and possibilities that don't involve such things.
答案1
得分: 1
我认为没有可能这样做,因为所有o1的信息将包含在它自己的对象内部。你可能需要改变对这个问题的思考方式。唯一的方法是拥有一个静态的普通变量,在所有类之间共享,但你仍然需要改变methodA或ObjectA的构造函数,这样它才会引入你需要的任何内容到一个公共变量中。
英文:
I think there no possibility to do that, as all o1 information will be inside its own object. You will probably have to change your thinking in this subject. The only way is having a static general variable shared in between all classes, but you still would need to change methodA or ObjectA constructor so it will introduce anything you need in a public variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论