[Java] Android 从Java向JNI传递Class数组的方法 →→→→→进入此内容的聊天室

来自 , 2021-02-23, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/b3ba8f1b
  1. #ifdef __cplusplus
  2. /*
  3.  * Reference types, in C++
  4.  */
  5. class _jobject {};
  6. class _jclass : public _jobject {};
  7. class _jstring : public _jobject {};
  8. class _jarray : public _jobject {};
  9. class _jobjectArray : public _jarray {};
  10. class _jbooleanArray : public _jarray {};
  11. class _jbyteArray : public _jarray {};
  12. class _jcharArray : public _jarray {};
  13. class _jshortArray : public _jarray {};
  14. class _jintArray : public _jarray {};
  15. class _jlongArray : public _jarray {};
  16. class _jfloatArray : public _jarray {};
  17. class _jdoubleArray : public _jarray {};
  18. class _jthrowable : public _jobject {};
  19.  
  20. typedef _jobject*       jobject;
  21. typedef _jclass*        jclass;
  22. typedef _jstring*       jstring;
  23. typedef _jarray*        jarray;
  24. typedef _jobjectArray*  jobjectArray;
  25. typedef _jbooleanArray* jbooleanArray;
  26. typedef _jbyteArray*    jbyteArray;
  27. typedef _jcharArray*    jcharArray;
  28. typedef _jshortArray*   jshortArray;
  29. typedef _jintArray*     jintArray;
  30. typedef _jlongArray*    jlongArray;
  31. typedef _jfloatArray*   jfloatArray;
  32. typedef _jdoubleArray*  jdoubleArray;
  33. typedef _jthrowable*    jthrowable;
  34. typedef _jobject*       jweak;
  35.  
  36.  
  37. #else /* not __cplusplus */
  38.  
  39. /*
  40.  * Reference types, in C.
  41.  */
  42. typedef void*           jobject;
  43. typedef jobject         jclass;
  44. typedef jobject         jstring;
  45. typedef jobject         jarray;
  46. typedef jarray          jobjectArray;
  47. typedef jarray          jbooleanArray;
  48. typedef jarray          jbyteArray;
  49. typedef jarray          jcharArray;
  50. typedef jarray          jshortArray;
  51. typedef jarray          jintArray;
  52. typedef jarray          jlongArray;
  53. typedef jarray          jfloatArray;
  54. typedef jarray          jdoubleArray;
  55. typedef jobject         jthrowable;
  56. typedef jobject         jweak;
  57.  
  58. #endif /* not __cplusplus *///源代码片段来自云代码http://yuncode.net
  59.                        
  60.  
  61. static void _classArray(JNIEnv* env, jobject thiz, jobjectArray classArr) {
  62.         jsize as = env->GetArrayLength(classArr);
  63.         LOG_N("array size=%d", as);
  64.         for (int i=0; i<as; i++) {
  65.                 jobject obj = env->GetObjectArrayElement(classArr, i);
  66.                 jclass c = env->GetObjectClass(obj);
  67.                 jmethodID m = env->GetMethodID(c, "getName", "()Ljava/lang/String;");
  68.                 jstring name = (jstring)env->CallObjectMethod(obj, m);
  69.                 const char* n = env->GetStringUTFChars(name, NULL);
  70.                 LOG_N("class name=%s", n);
  71.                 env->ReleaseStringUTFChars(name, n);
  72.         }
  73. }//源代码片段来自云代码http://yuncode.net
  74.                        

回复 "Android 从Java向JNI传递Class数组的方法"

这儿你可以回复上面这条便签

captcha