简体   繁体   中英

Is it possible to access package members using JNI?

I'm having an issue getting objects from my imports using JNI. Is it possible? For example, say I have this in my java file:

import foo.Bar;

public class MyClass {
    public MyClass(){
        Bar.runMethod();
    }
}

Can I reference "Bar" using JNI without changing the Java code? I've only been able to create a new "Bar" object, which isn't what I want to do.

edit: to expand on this a bit, the runMethod() does something like this:

public class Bar{
    public static void runMethod(){
        Bar inst = new Bar();
        ...more code...
    }
}

So now there's an instance of Bar floating around that I'm not sure how to access using JNI. If bar has things like togglebuttons, radio buttons, etc, I can't use JNI to manipulate them since I can't get a jobject that references it. Is there anything I can do, or do I have to resort to editing the Java file? (I don't maintain the code, so it would be a last resort)

您的问题或标题尚不清楚,但是您可以将Bar实例传递给本机方法或Bar.class,也可以从中查找Bar.class。

JNI can access any method of any object, including private methods of Java classes. This question however doesn't provide enough concrete detail for us to understand the context well enough to help you.

The Oracle reference is a little obtuse, but if you provide a code sample from the JNI side, we can likely demystify it a bit for you: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#wp15982

public class Bar{
public static void runMethod(){
    Bar inst = new Bar();
    ...more code...
}

The instance of Bar named inst within the method "runMethod" no longer exists when the function completes, unless it is stored under a static class variable later in your "more code" section, so there wouldn't be an instance for you to reference.

JNI is only going to give you access to class members and methods, most certainly not local method variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM