简体   繁体   English

如何在JNI中创建Class []

[英]how to create Class[] in JNI

I have to pass a Class[] from jni to java. 我必须将jni的Class []传递给java。 I create the array in my native code as below, 我在本地代码中创建数组,如下所示:

array = (*env)->NewObjectArray(env, 10, (*env)->FindClass(env, "java/lang/Class"), NULL);

I need to insert, Integer[].class and Integer.TYPE into this array and receive it in my java program. 我需要将Integer []。class和Integer.TYPE插入此数组并在我的Java程序中接收它。

public int Sample(Class[] cls){
   //some code
}

How can I insert the elements into the array in my jni layer using SetObjectArrayElement() . 如何使用SetObjectArrayElement()将元素插入到jni层的数组中。

Something like this (I am using the more compact C++ JNI calls): 这样的事情(我正在使用更紧凑的C ++ JNI调用):

jclass classClass = env->FindClass("java/lang/Class");
jclass intClass = env->FindClass("java/lang/Integer");
jclass intArrayClass = env->FindClass("[java/lang/Integer";
jfieldID fid = env->GetFieldID(intClass, "TYPE");
jclass intTypeClass = (jclass)env->GetStaticObjectField(intClass, fid);
jobjectArray array = env->NewObjectArray(2, classClass, 0);
env->SetObjectArrayElement(array, 0, intTypeClass);
env->SetObjectArrayElement(array, 1, intArrayClass);

Note that, depending on how this array is to be used, you may need to convert the array and its elements from local references to global references. 请注意,根据如何使用此数组,您可能需要将数组及其元素从本地引用转换为全局引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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