我在写一个应用程序在那里我有很多事情要做 JNI
电话,每一次我必须执行吸气剂()
调用访问变量值。取而代之的是它可以访问对象的对象引用 JNI
Java的层,这样可以得到更新变量值只是变量名称(如 obj.name
,而不是 obj.getName()
)。
我请与这和的这个,但没有得到的方式如何隐蔽地址,对象是java层。
修改 我想从JNI访问的OBJ在的Java层这种方式。
私人本地CustomObj getCPPCustomObjectPointer();
在这里任何建议。
解决方案是否有可能访问JNI对象的对象引用上的Java层?
是的,你可以。但是你不能用它来访问它的属性。你只能够保持其地址为一个长
值。
![计算机等级考试二级Java辅导 Java语言诞生 目标 特点和实现机制](https://p.xsw88.cn/allimgs/daicuo/20230906/2197.png)
如果你想这样做的话,你应该创建你的C ++堆内存中的对象,并返回其地址为长
的数字。
MyClass的* OBJ =新MyClass的();
返回(长)目标文件;
在Java端,你可以在地址另存为,无论你想一个长
号。由于对象已在堆内存中创建,他们将继续有效JNI调用之间。
此外,你必须通过他们以后JNI为长
号码的电话,然后你应该将它们转换为 MyClass的*
在C ++的一面。
MyClass的* OBJ =(MyClass的*)thatLongNumber;
obj-> someProperty; //通过访问其属性和方法 - >操作者
I'm writing an application where I have lot to do with JNI
call and every-time I have to perform getter()
call to access variable values. Instead is it possible to access Object reference of JNI
object on Java Layer so can get updated variable value just by variable name (like obj.name
instead obj.getName()
).
I have check with this and this, but not getting way how to covert address to Object at java layer.
EDIT I wanted to access Obj this way at Java layer from JNI.
private native CustomObj getCPPCustomObjectPointer();
Any suggestion here.
解决方案Is it possible to access Object reference of JNI object on Java Layer?
Yes, you can. However you cannot use it for accessing its properties. You are only able to hold its address as a long
value.
If you would like to do so, you should create your C++ objects in heap memory and return their addresses as long
numbers.
MyClass *obj = new MyClass();
return (long) obj;
In Java side you can save that address as a long
number wherever you want. Since objects have been created in heap memory, they will remain valid between JNI calls.
Also, you have to pass them to later JNI calls as a long
number and then you should cast them to MyClass *
in your C++ side.
MyClass *obj = (MyClass *)thatLongNumber;
obj->someProperty; // Access its properties and methods via -> operator
相关推荐
最新文章