Gabriele Spisani
Gabriele Spisani

Reputation: 3

Getting the local variables of the calling method

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.

Upvotes: 0

Views: 195

Answers (1)

Román
Román

Reputation: 146

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.

Upvotes: 1

Related Questions