获取调用方法的局部变量

huangapple go评论69阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月16日 00:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定