#include"stdafx.h" #include "pch.h" #include #include"convertutils.h" using namespace std; // java��ά��������תpython��ά�������� // ConvertJdarrToPydarr PyObject* ConvertJdarrToPydarr(JNIEnv* env, jobjectArray data) { //Py_Initialize(); //��ȡjava�ж�ά��������� jsize rows = env->GetArrayLength((jobjectArray)data); /*if (rows < 0) { PyErr_SetString(PyExc_RuntimeError, "Invalid Java array passed to native method."); return 0; }*/ PyObject* pyList = PyList_New(rows); /*if (pyList == NULL) { PyErr_Print(); Py_Finalize(); return 0; }*/ for (jsize i = 0; i < rows; i++) { //��ȡ��ά����ÿһ������ jdoubleArray rowArray = (jdoubleArray)env->GetObjectArrayElement(data, i); /*if (rowArray == NULL) { Py_DECREF(pyList); PyErr_SetString(PyExc_RuntimeError, "Unable to get row array from Java object."); return 0; }*/ //��ȡJava����������� jsize cols = env->GetArrayLength(rowArray); PyObject* pyRow = PyList_New(cols); /*if (pyRow == NULL) { env->DeleteLocalRef(rowArray); Py_DECREF(pyList); PyErr_Print(); Py_Finalize(); return 0; }*/ //��ȡJava�������ԭʼ���� jdouble* rowElements = env->GetDoubleArrayElements(rowArray, NULL); //��Java�����Ԫ�ظ��Ƶ�Python�б� for (jsize j = 0; j < cols; j++) { PyObject* pyElement = PyFloat_FromDouble(rowElements[j]); /*if (pyElement == NULL) { env->ReleaseDoubleArrayElements(rowArray, rowElements, 0); env->DeleteLocalRef(rowArray); Py_DECREF(pyList); Py_DECREF(pyRow); PyErr_Print(); Py_Finalize(); return 0; }*/ PyList_SetItem(pyRow, j, pyElement); } env->ReleaseDoubleArrayElements(rowArray, rowElements, 0); env->DeleteLocalRef(rowArray); //����ǰ����ӵ���ά�����б� PyList_SetItem(pyList, i, pyRow); } return pyList; } // java�Ĺ�ϣ����תpython���ֵ����� // ConvertJhmapToPydict PyObject* ConvertJhmapToPydict(JNIEnv* env, jobject data_in) { Py_Initialize(); PyObject* pyDict = PyDict_New(); // ��ȡHashMap��entrySet()����ID jclass hashmapClass = env->FindClass("java/util/HashMap"); jmethodID entrySetMID = env->GetMethodID(hashmapClass, "entrySet", "()Ljava/util/Set;"); // ����entrySet()������ȡSet���� jobject setObj = env->CallObjectMethod(data_in, entrySetMID); // ��ȡSet����iterator()����ID jclass setClass = env->FindClass("java/util/Set"); jmethodID iteratorMID = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;"); // ����iterator()������ȡIterator���� jobject iteratorObj = env->CallObjectMethod(setObj, iteratorMID); // ��ȡIterator����hasNext()����ID // ����whileѭ���ж�HashMap���Ƿ������� jclass iteratorClass = env->FindClass("java/util/Iterator"); jmethodID hasNextMID = env->GetMethodID(iteratorClass, "hasNext", "()Z"); // ��ȡIterator����next()����ID // ���ڶ�ȡHashMap�е�ÿһ������ jmethodID nextMID = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;"); // ��ȡMap.Entry����getKey()��getValue()�ķ���ID // ���ڶ�ȡ���γ�-��������ֵ�ԣ�ע�⣺�ڲ���ʹ��$���ű�ʾ jclass entryClass = env->FindClass("java/util/Map$Entry"); jmethodID getKeyMID = env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;"); jmethodID getValueMID = env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;"); // ѭ�����HashMap���Ƿ������� while (env->CallBooleanMethod(iteratorObj, hasNextMID)) { // ��ȡһ������ jobject entryObj = env->CallObjectMethod(iteratorObj, nextMID); // ��ȡ������keyֵ��String���� jstring parameterJS = (jstring)env->CallObjectMethod(entryObj, getKeyMID); if (parameterJS == NULL) // HashMap����null���� continue; // jstringתC����ַ��� const char* parameterStr = env->GetStringUTFChars(parameterJS, NULL); // ��ȡ������valueֵ��Obj���� jobject javaValueObj = env->CallObjectMethod(entryObj, getValueMID); //��ȡvalue������ //��ȡ��javaValueObj��jclass����������������javaValueObj���ࡣ jclass valueClass = env->GetObjectClass(javaValueObj); //�ҵ�"getClass"���������Java�����еķ���ID��"getClass"��������Java���ж���ı�׼���������ڷ��ظö������ jmethodID mid_getClass = env->GetMethodID(valueClass, "getClass", "()Ljava/lang/Class;"); //��ȡjavaValueObj�������jclass���� jobject valueClassObj = env->CallObjectMethod(javaValueObj, mid_getClass); //��ȡ��Java�е����"getName"�����������������һ���ַ�������ʾ�����ȫ�޶����� jmethodID mid_getName = env->GetMethodID(env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;"); //��javavalueobj�������ȫ�޶����洢��jstrClassName�� jstring jstrClassName = (jstring)env->CallObjectMethod(valueClassObj, mid_getName); //��jstring����jstrClassNameת��ΪC�����ַ���strClassName const char* strClassName = env->GetStringUTFChars(jstrClassName, NULL); PyObject* pyNewValue = NULL; if (strcmp(strClassName, "java.lang.Integer") == 0) { jint intValue = env->CallIntMethod(javaValueObj, env->GetMethodID(valueClass, "intValue", "()I")); pyNewValue = PyLong_FromLong((long)intValue); //printf("intValue is %d\n", intValue); } else if (strcmp(strClassName, "java.lang.Double") == 0) { jdouble doubleValue = env->CallDoubleMethod(javaValueObj, env->GetMethodID(valueClass, "doubleValue", "()D")); pyNewValue = PyFloat_FromDouble(doubleValue); //printf("doubleValue is %f\n", doubleValue); } else if (strcmp(strClassName, "java.lang.String") == 0) { const char* strValue = env->GetStringUTFChars((jstring)javaValueObj, NULL); pyNewValue = PyUnicode_FromString(strValue); //printf("strValue is %s\n", strValue); } else if (strcmp(strClassName, "java.lang.Boolean") == 0) { jmethodID mid = env->GetMethodID(valueClass, "booleanValue", "()Z"); jboolean booleanValue = env->CallBooleanMethod(javaValueObj, mid); pyNewValue = PyBool_FromLong((long)booleanValue); //printf("booleanValue is %d\n", booleanValue); } else if (strcmp(strClassName, "java.lang.Character") == 0) { jmethodID mid = env->GetMethodID(valueClass, "charValue", "()C"); jchar charValue = env->CallCharMethod(javaValueObj, mid); pyNewValue = PyLong_FromLong((long)charValue); //printf("charValue is %c\n", charValue); } else if (strcmp(strClassName, "[D") == 0 || strcmp(strClassName, "[I") == 0) { jsize len = 0; if (strcmp(strClassName, "[D") == 0) { len = env->GetArrayLength((jdoubleArray)javaValueObj); } else { len = env->GetArrayLength((jintArray)javaValueObj); } PyObject* pyList = PyList_New(len); //printf("һά����\n"); for (int i = 0; i < len; ++i) { if (strcmp(strClassName, "[D") == 0) { jdouble* javaArray = env->GetDoubleArrayElements((jdoubleArray)javaValueObj, NULL); PyList_SetItem(pyList, i, PyFloat_FromDouble(javaArray[i])); //printf("%f ", javaArray[i]); env->ReleaseDoubleArrayElements((jdoubleArray)javaValueObj, javaArray, 0); } else { jint* javaArray = env->GetIntArrayElements((jintArray)javaValueObj, NULL); PyList_SetItem(pyList, i, PyLong_FromLong(javaArray[i])); //printf("%d ", javaArray[i]); env->ReleaseIntArrayElements((jintArray)javaValueObj, javaArray, 0); } } //printf("\n"); pyNewValue = pyList; } else if (strcmp(strClassName, "[[D") == 0 || strcmp(strClassName, "[[I") == 0) { //����ά����ת��ΪPython���б�����ʹ���б���б���ģ���λ���� jsize outerLength = env->GetArrayLength((jobjectArray)javaValueObj); PyObject* pyList = PyList_New(outerLength); PyObject* pyRowList = NULL; //printf("��ά����\n"); for (int i = 0; i < outerLength; ++i) { if (strcmp(strClassName, "[[I") == 0) { jintArray innerArray = (jintArray)env->GetObjectArrayElement((jobjectArray)javaValueObj, i); jsize innerLength = env->GetArrayLength(innerArray); pyRowList = PyList_New(innerLength); jint* innerArrayValues = env->GetIntArrayElements(innerArray, NULL); //printf("Array %d: ", i); for (int j = 0; j < innerLength; ++j) { PyList_SetItem(pyRowList, j, PyLong_FromLong(innerArrayValues[j])); //printf("%d ", innerArrayValues[j]); } //printf("\n"); env->ReleaseIntArrayElements(innerArray, innerArrayValues, 0); } else { jdoubleArray innerArray = (jdoubleArray)env->GetObjectArrayElement((jobjectArray)javaValueObj, i); jsize innerLength = env->GetArrayLength(innerArray); pyRowList = PyList_New(innerLength); jdouble* innerArrayValues = env->GetDoubleArrayElements(innerArray, NULL); //printf("Array %f: ", i); for (int j = 0; j < innerLength; ++j) { PyList_SetItem(pyRowList, j, PyLong_FromLong(innerArrayValues[j])); //printf("%f ", innerArrayValues[j]); } //printf("\n"); env->ReleaseDoubleArrayElements(innerArray, innerArrayValues, 0); } PyList_SetItem(pyList, i, pyRowList); } pyNewValue = pyList; } //��ȡ����key��Python�ֵ��е�item PyObject* pyKey = PyUnicode_FromString(parameterStr); if (pyNewValue) { PyDict_SetItem(pyDict, pyKey, pyNewValue); Py_DECREF(pyNewValue); } Py_DECREF(pyKey); // �ͷ�Java�ַ��� env->ReleaseStringUTFChars(parameterJS, parameterStr); } return pyDict; } //jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj); // //// �� Python һά�б�ת��Ϊ Java һά�������� //jobjectArray convertPyListToJArray(JNIEnv* env, PyObject* pyList, const char* elementType) { // Py_ssize_t size = PyList_Size(pyList); // jobjectArray jarray = env->NewObjectArray(size, env->FindClass(elementType), NULL); // /*if (jarray == NULL) { // return NULL; // }*/ // for (Py_ssize_t i = 0; i < size; ++i) { // PyObject* pyItem = PyList_GetItem(pyList, i); // jobject jItem = convertPyObjectToJObject(env, pyItem); // if (jItem == NULL) { // env->DeleteLocalRef(jarray); // return NULL; // } // env->SetObjectArrayElement(jarray, i, jItem); // env->DeleteLocalRef(jItem); // } // return jarray; //} // //// �� Python ��ά�б�ת��Ϊ Java ��ά�������� //jobjectArray convertPy2DListToJ2DObjectArray(JNIEnv* env, PyObject* py2DList, const char* elementType) { // Py_ssize_t rows = PyList_Size(py2DList); // jobjectArray j2DArray = env->NewObjectArray(rows, env->FindClass("[Ljava/lang/Object;"), NULL); // /*if (j2DArray == NULL) { // return NULL; // }*/ // for (Py_ssize_t i = 0; i < rows; ++i) { // PyObject* pyRow = PyList_GetItem(py2DList, i); // jobjectArray jRowArray = convertPyListToJArray(env, pyRow, elementType); // if (jRowArray == NULL) { // env->DeleteLocalRef(j2DArray); // return NULL; // } // env->SetObjectArrayElement(j2DArray, i, jRowArray); // env->DeleteLocalRef(jRowArray); // } // return j2DArray; //} // //jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj) { // if (PyLong_Check(pyObj)) { // long longVal = PyLong_AsLong(pyObj); // jclass longClass = env->FindClass("java/lang/Long"); // jmethodID longInit = env->GetMethodID(longClass, "", "(J)V"); // return env->NewObject(longClass, longInit, longVal); // } // else if (PyFloat_Check(pyObj)) { // double doubleVal = PyFloat_AsDouble(pyObj); // jclass doubleClass = env->FindClass("java/lang/Double"); // jmethodID doubleInit = env->GetMethodID(doubleClass, "", "(D)V"); // return env->NewObject(doubleClass, doubleInit, doubleVal); // } // else if (PyUnicode_Check(pyObj)) { // const char* strVal = PyUnicode_AsUTF8(pyObj); // return env->NewStringUTF(strVal); // } // else if (PyBool_Check(pyObj)) { // jboolean boolVal = (jboolean)(PyLong_AsLong(pyObj) != 0); // jclass booleanClass = env->FindClass("java/lang/Boolean"); // jmethodID booleanInit = env->GetMethodID(booleanClass, "", "(Z)V"); // return env->NewObject(booleanClass, booleanInit, boolVal); // } // else if (PyList_Check(pyObj)) { // const char* elementType = "java/lang/Object"; // for (Py_ssize_t i = 0; i < PyList_Size(pyObj); i++) { // PyObject* item = PyList_GetItem(pyObj, i); // // ����Ƕ�ά���� // if (PyList_Check(item)) { // return convertPy2DListToJ2DObjectArray(env, pyObj, elementType); // } // else { // return convertPyListToJArray(env, pyObj, elementType); // } // } // } // else if (PyDict_Check(pyObj)) // { // cout << "dict" << endl; // } // return NULL; //} // // //// python���ֵ�����תjava�Ĺ�ϣ���� //// ConvertPydictToJhmap //jobject ConvertPydictToJhmap(JNIEnv* env, PyObject* pyDict) { // jclass hashMapClass = env->FindClass("java/util/HashMap"); // jmethodID hashMapInit = env->GetMethodID(hashMapClass, "", "()V"); // jobject hashMap = env->NewObject(hashMapClass, hashMapInit); // /*if (hashMap == NULL) { // return NULL; // }*/ // Py_ssize_t pos = 0; // PyObject* key; // PyObject* value; // while (PyDict_Next(pyDict, &pos, &key, &value)) { // const char* keyStr = PyUnicode_AsUTF8(key); // jstring jKey = env->NewStringUTF(keyStr); // jobject jValue = convertPyObjectToJObject(env, value); // if (jValue != NULL) { // jmethodID putMethod = env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); // env->CallObjectMethod(hashMap, putMethod, jKey, jValue); // env->DeleteLocalRef(jValue); // } // else return NULL; // env->DeleteLocalRef(jKey); // } // return hashMap; //} jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj); // �� Python һά�б�ת��Ϊ Java һά�������� jobjectArray convertPyListToJArray(JNIEnv* env, PyObject* pyList, const char* elementType) { Py_ssize_t size = PyList_Size(pyList); jobjectArray jarray = env->NewObjectArray(size, env->FindClass(elementType), NULL); /*if (jarray == NULL) { return NULL; }*/ for (Py_ssize_t i = 0; i < size; ++i) { PyObject* pyItem = PyList_GetItem(pyList, i); jobject jItem = convertPyObjectToJObject(env, pyItem); if (jItem == NULL) { env->DeleteLocalRef(jarray); return NULL; } env->SetObjectArrayElement(jarray, i, jItem); env->DeleteLocalRef(jItem); } return jarray; } // �� Python ��ά�б�ת��Ϊ Java ��ά�������� jobjectArray convertPy2DListToJ2DObjectArray(JNIEnv* env, PyObject* py2DList, const char* elementType) { Py_ssize_t rows = PyList_Size(py2DList); jobjectArray j2DArray = env->NewObjectArray(rows, env->FindClass("[Ljava/lang/Object;"), NULL); /*if (j2DArray == NULL) { return NULL; }*/ for (Py_ssize_t i = 0; i < rows; ++i) { PyObject* pyRow = PyList_GetItem(py2DList, i); jobjectArray jRowArray = convertPyListToJArray(env, pyRow, elementType); if (jRowArray == NULL) { env->DeleteLocalRef(j2DArray); return NULL; } env->SetObjectArrayElement(j2DArray, i, jRowArray); env->DeleteLocalRef(jRowArray); } return j2DArray; } jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj) { if (PyLong_Check(pyObj)) { long longVal = PyLong_AsLong(pyObj); jclass longClass = env->FindClass("java/lang/Long"); jmethodID longInit = env->GetMethodID(longClass, "", "(J)V"); return env->NewObject(longClass, longInit, longVal); } else if (PyFloat_Check(pyObj)) { double doubleVal = PyFloat_AsDouble(pyObj); jclass doubleClass = env->FindClass("java/lang/Double"); jmethodID doubleInit = env->GetMethodID(doubleClass, "", "(D)V"); return env->NewObject(doubleClass, doubleInit, doubleVal); } else if (PyUnicode_Check(pyObj)) { const char* strVal = PyUnicode_AsUTF8(pyObj); return env->NewStringUTF(strVal); } else if (PyBool_Check(pyObj)) { jboolean boolVal = (jboolean)(PyLong_AsLong(pyObj) != 0); jclass booleanClass = env->FindClass("java/lang/Boolean"); jmethodID booleanInit = env->GetMethodID(booleanClass, "", "(Z)V"); return env->NewObject(booleanClass, booleanInit, boolVal); } else if (PyList_Check(pyObj)) { const char* elementType = "java/lang/Object"; for (Py_ssize_t i = 0; i < PyList_Size(pyObj); i++) { PyObject* item = PyList_GetItem(pyObj, i); // ����Ƕ�ά���� if (PyList_Check(item)) { return convertPy2DListToJ2DObjectArray(env, pyObj, elementType); } else { return convertPyListToJArray(env, pyObj, elementType); } } } else if (PyDict_Check(pyObj)) { return ConvertPydictToJhmap(env, pyObj); } return NULL; } // python���ֵ�����תjava�Ĺ�ϣ���� jobject ConvertPydictToJhmap(JNIEnv* env, PyObject* pyDict) { jclass hashMapClass = env->FindClass("java/util/HashMap"); jmethodID hashMapInit = env->GetMethodID(hashMapClass, "", "()V"); jobject hashMap = env->NewObject(hashMapClass, hashMapInit); if (hashMap == NULL) { return NULL; } Py_ssize_t pos = 0; PyObject* key; PyObject* value; while (PyDict_Next(pyDict, &pos, &key, &value)) { const char* keyStr = PyUnicode_AsUTF8(key); jstring jKey = env->NewStringUTF(keyStr); jobject jValue = convertPyObjectToJObject(env, value); if (jValue != NULL) { jmethodID putMethod = env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); env->CallObjectMethod(hashMap, putMethod, jKey, jValue); env->DeleteLocalRef(jValue); } else return NULL; env->DeleteLocalRef(jKey); } return hashMap; }