Sanjid Haque
Sanjid Haque

Reputation: 13

Is there any way to access a Java method/function local variables and local object instances through JNI

public class some_class
{
    public static void som_func() {
        var some_object = new SomeObject();
        var ret = 0;
        do
        {
            ret = some_object.DoSomething();
        } while(ret != 1);
        System.out.printf("Return value: %d", ret);
    }
}

Now in C++ if I call like this way:

auto some_cls = g_env->FindClass("some_class");
auto some_func = g_env->GetStaticMethodID(some_cls, "som_func", "()V");
g_env->CallStaticVoidMethod(some_cls, some_func);

Is there any way to access that some_object variable var some_object = new SomeObject(); or that ret variable var ret = 0; through JNI or any JVM memory accessing trick?

I want to do something like:

auto some_object_class = g_env->FindClass("SomeObject");
/* 
and then some way this 'some_object_class' will be a reference to that local variable 'some_object'
*/

I suppose I am trying to know how to access the JVM's heap memory? I heard that JVM allocates an object on the heap and a reference to that allocated object is stored on the stack, if that's the case can I access that stack memory and retrieve the reference? I am confused, I guess I don't know what I am talking about in the end. Please guide me to the right path.

Upvotes: 0

Views: 215

Answers (1)

boneill
boneill

Reputation: 1526

Can't be done. Note that you can't access these local variables from a regular Java method either. Part of the problem is that the method has no idea where these local variables are. They might be allocated in a stack frame, or they might be in registers. Even if you implemented some assembly code to access the stack and registers directly, you'll have no idea where to look, and because of the dynamic nature of JVM code compilation, the locations can change.

Upvotes: 1

Related Questions