[英]JNI: global reference to a jarray in C++
I'm writing a java program which makes extensive use of native methods and JNI implemented in C++.我正在编写一个 java 程序,它广泛使用本地方法和在 C++ 中实现的 JNI。 On the C++ side, I have a local reference to a
jarray
which I would like to convert to a global reference.在 C++ 方面,我有一个对
jarray
的本地引用,我想将其转换为全局引用。 Simple, I think: call env->NewGlobalRef(array_ref)
.我认为很简单:调用
env->NewGlobalRef(array_ref)
。 And, indeed, this works --- but the result is a jobject
, not a jarray
.而且,确实,这行得通——但结果是一个
jobject
,而不是一个jarray
。 Is it safe to downcast this back to a jarray
?将其转换回
jarray
是否安全? If not, can I recover a jarray
from it?如果没有,我可以从中恢复一个
jarray
吗? Note that I'm in C++, where these are not just typedefs of each other.请注意,我在 C++ 中,其中这些不仅仅是彼此的 typedef。
Yes, that should be safe since a jarray
is a subtype of jobject
[ref] and is basically a pointer.是的,这应该是安全的,因为
jarray
是jobject
[ref]的子类型,并且基本上是一个指针。 Here's the relevant code from jni.h
under the #ifdef __cplusplus
part:以下是
#ifdef __cplusplus
部分下jni.h
的相关代码:
class _jobject {};
class _jclass : public _jobject {};
:
:
class _jarray : public _jobject {};
class _jbooleanArray : public _jarray {};
:
typedef _jobject *jobject;
:
typedef _jarray *jarray;
typedef _jbooleanArray *jbooleanArray;
:
As is evident, these are just dummy classes to implement subtyping and jarray
and jobject
are of type _jobject*
and _jarray*
and the class _jarray
just inherits from _jobject
to evince the is-a
relationship.很明显,这些只是实现子类型化的虚拟类,
jarray
和jobject
的类型为_jobject*
和_jarray*
并且 class _jarray
只是从_jobject
继承以证明is-a
关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.