提交 | 用户 | 时间
|
f7932f
|
1 |
#include"stdafx.h" |
D |
2 |
#include "pch.h" |
|
3 |
#include <string> |
|
4 |
#include"convertutils.h" |
|
5 |
|
|
6 |
using namespace std; |
|
7 |
|
|
8 |
// java��ά��������תpython��ά�������� |
|
9 |
// ConvertJdarrToPydarr |
|
10 |
PyObject* ConvertJdarrToPydarr(JNIEnv* env, jobjectArray data) |
|
11 |
{ |
|
12 |
|
|
13 |
//Py_Initialize(); |
|
14 |
//��ȡjava�ж�ά��������� |
|
15 |
jsize rows = env->GetArrayLength((jobjectArray)data); |
|
16 |
/*if (rows < 0) { |
|
17 |
PyErr_SetString(PyExc_RuntimeError, "Invalid Java array passed to native method."); |
|
18 |
return 0; |
|
19 |
}*/ |
|
20 |
PyObject* pyList = PyList_New(rows); |
|
21 |
/*if (pyList == NULL) { |
|
22 |
PyErr_Print(); |
|
23 |
Py_Finalize(); |
|
24 |
return 0; |
|
25 |
}*/ |
|
26 |
for (jsize i = 0; i < rows; i++) { |
|
27 |
//��ȡ��ά����ÿһ������ |
|
28 |
jdoubleArray rowArray = (jdoubleArray)env->GetObjectArrayElement(data, i); |
|
29 |
/*if (rowArray == NULL) { |
|
30 |
Py_DECREF(pyList); |
|
31 |
PyErr_SetString(PyExc_RuntimeError, "Unable to get row array from Java object."); |
|
32 |
return 0; |
|
33 |
}*/ |
|
34 |
//��ȡJava����������� |
|
35 |
jsize cols = env->GetArrayLength(rowArray); |
|
36 |
PyObject* pyRow = PyList_New(cols); |
|
37 |
/*if (pyRow == NULL) { |
|
38 |
env->DeleteLocalRef(rowArray); |
|
39 |
Py_DECREF(pyList); |
|
40 |
PyErr_Print(); |
|
41 |
Py_Finalize(); |
|
42 |
return 0; |
|
43 |
}*/ |
|
44 |
//��ȡJava�������ԭʼ���� |
|
45 |
jdouble* rowElements = env->GetDoubleArrayElements(rowArray, NULL); |
|
46 |
//��Java�����Ԫ�ظ��Ƶ�Python�б� |
|
47 |
for (jsize j = 0; j < cols; j++) { |
|
48 |
PyObject* pyElement = PyFloat_FromDouble(rowElements[j]); |
|
49 |
/*if (pyElement == NULL) { |
|
50 |
env->ReleaseDoubleArrayElements(rowArray, rowElements, 0); |
|
51 |
env->DeleteLocalRef(rowArray); |
|
52 |
Py_DECREF(pyList); |
|
53 |
Py_DECREF(pyRow); |
|
54 |
PyErr_Print(); |
|
55 |
Py_Finalize(); |
|
56 |
return 0; |
|
57 |
}*/ |
|
58 |
PyList_SetItem(pyRow, j, pyElement); |
|
59 |
} |
|
60 |
env->ReleaseDoubleArrayElements(rowArray, rowElements, 0); |
|
61 |
env->DeleteLocalRef(rowArray); |
|
62 |
//����ǰ����ӵ���ά�����б� |
|
63 |
PyList_SetItem(pyList, i, pyRow); |
|
64 |
} |
|
65 |
return pyList; |
|
66 |
} |
|
67 |
|
|
68 |
// java�Ĺ�ϣ����תpython���ֵ����� |
|
69 |
// ConvertJhmapToPydict |
|
70 |
PyObject* ConvertJhmapToPydict(JNIEnv* env, jobject data_in) |
|
71 |
{ |
|
72 |
Py_Initialize(); |
|
73 |
PyObject* pyDict = PyDict_New(); |
|
74 |
|
|
75 |
// ��ȡHashMap��entrySet()����ID |
|
76 |
jclass hashmapClass = env->FindClass("java/util/HashMap"); |
|
77 |
jmethodID entrySetMID = env->GetMethodID(hashmapClass, "entrySet", "()Ljava/util/Set;"); |
|
78 |
// ����entrySet()������ȡSet���� |
|
79 |
jobject setObj = env->CallObjectMethod(data_in, entrySetMID); |
|
80 |
// ��ȡSet����iterator()����ID |
|
81 |
jclass setClass = env->FindClass("java/util/Set"); |
|
82 |
jmethodID iteratorMID = env->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;"); |
|
83 |
// ����iterator()������ȡIterator���� |
|
84 |
jobject iteratorObj = env->CallObjectMethod(setObj, iteratorMID); |
|
85 |
// ��ȡIterator����hasNext()����ID |
|
86 |
// ����whileѭ���ж�HashMap���Ƿ������� |
|
87 |
jclass iteratorClass = env->FindClass("java/util/Iterator"); |
|
88 |
jmethodID hasNextMID = env->GetMethodID(iteratorClass, "hasNext", "()Z"); |
|
89 |
// ��ȡIterator����next()����ID |
|
90 |
// ���ڶ�ȡHashMap�е�ÿһ������ |
|
91 |
jmethodID nextMID = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;"); |
|
92 |
// ��ȡMap.Entry����getKey()��getValue()�ķ���ID |
|
93 |
// ���ڶ�ȡ���γ�-��������ֵ�ԣ�ע�⣺�ڲ���ʹ��$���ű�ʾ |
|
94 |
jclass entryClass = env->FindClass("java/util/Map$Entry"); |
|
95 |
jmethodID getKeyMID = env->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;"); |
|
96 |
jmethodID getValueMID = env->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;"); |
|
97 |
// ѭ�����HashMap���Ƿ������� |
|
98 |
while (env->CallBooleanMethod(iteratorObj, hasNextMID)) { |
|
99 |
// ��ȡһ������ |
|
100 |
jobject entryObj = env->CallObjectMethod(iteratorObj, nextMID); |
|
101 |
// ��ȡ������keyֵ��String���� |
|
102 |
jstring parameterJS = (jstring)env->CallObjectMethod(entryObj, getKeyMID); |
|
103 |
if (parameterJS == NULL) // HashMap����null���� |
|
104 |
continue; |
|
105 |
// jstringתC����ַ��� |
|
106 |
const char* parameterStr = env->GetStringUTFChars(parameterJS, NULL); |
|
107 |
|
|
108 |
// ��ȡ������valueֵ��Obj���� |
|
109 |
jobject javaValueObj = env->CallObjectMethod(entryObj, getValueMID); |
|
110 |
|
|
111 |
//��ȡvalue������ |
|
112 |
//��ȡ��javaValueObj��jclass����������������javaValueObj���ࡣ |
|
113 |
jclass valueClass = env->GetObjectClass(javaValueObj); |
|
114 |
//�ҵ�"getClass"���������Java�����еķ���ID��"getClass"��������Java���ж���ı����������ڷ��ظö������ |
|
115 |
jmethodID mid_getClass = env->GetMethodID(valueClass, "getClass", "()Ljava/lang/Class;"); |
|
116 |
//��ȡjavaValueObj�������jclass���� |
|
117 |
jobject valueClassObj = env->CallObjectMethod(javaValueObj, mid_getClass); |
|
118 |
//��ȡ��Java�е����"getName"�����������������һ���ַ�������ʾ�����ȫ������ |
|
119 |
jmethodID mid_getName = env->GetMethodID(env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;"); |
|
120 |
//��javavalueobj�������ȫ�����洢��jstrClassName�� |
|
121 |
jstring jstrClassName = (jstring)env->CallObjectMethod(valueClassObj, mid_getName); |
|
122 |
//��jstring����jstrClassNameת��ΪC�����ַ���strClassName |
|
123 |
const char* strClassName = env->GetStringUTFChars(jstrClassName, NULL); |
|
124 |
|
|
125 |
PyObject* pyNewValue = NULL; |
|
126 |
|
|
127 |
if (strcmp(strClassName, "java.lang.Integer") == 0) { |
|
128 |
jint intValue = env->CallIntMethod(javaValueObj, env->GetMethodID(valueClass, "intValue", "()I")); |
|
129 |
pyNewValue = PyLong_FromLong((long)intValue); |
|
130 |
//printf("intValue is %d\n", intValue); |
|
131 |
} |
|
132 |
else if (strcmp(strClassName, "java.lang.Double") == 0) { |
|
133 |
jdouble doubleValue = env->CallDoubleMethod(javaValueObj, env->GetMethodID(valueClass, "doubleValue", "()D")); |
|
134 |
pyNewValue = PyFloat_FromDouble(doubleValue); |
|
135 |
//printf("doubleValue is %f\n", doubleValue); |
|
136 |
} |
|
137 |
else if (strcmp(strClassName, "java.lang.String") == 0) { |
|
138 |
const char* strValue = env->GetStringUTFChars((jstring)javaValueObj, NULL); |
|
139 |
pyNewValue = PyUnicode_FromString(strValue); |
|
140 |
//printf("strValue is %s\n", strValue); |
|
141 |
} |
|
142 |
else if (strcmp(strClassName, "java.lang.Boolean") == 0) { |
|
143 |
jmethodID mid = env->GetMethodID(valueClass, "booleanValue", "()Z"); |
|
144 |
jboolean booleanValue = env->CallBooleanMethod(javaValueObj, mid); |
|
145 |
pyNewValue = PyBool_FromLong((long)booleanValue); |
|
146 |
//printf("booleanValue is %d\n", booleanValue); |
|
147 |
} |
|
148 |
else if (strcmp(strClassName, "java.lang.Character") == 0) { |
|
149 |
jmethodID mid = env->GetMethodID(valueClass, "charValue", "()C"); |
|
150 |
jchar charValue = env->CallCharMethod(javaValueObj, mid); |
|
151 |
pyNewValue = PyLong_FromLong((long)charValue); |
|
152 |
//printf("charValue is %c\n", charValue); |
|
153 |
} |
|
154 |
else if (strcmp(strClassName, "[D") == 0 || strcmp(strClassName, "[I") == 0) { |
|
155 |
jsize len = 0; |
|
156 |
if (strcmp(strClassName, "[D") == 0) { |
|
157 |
len = env->GetArrayLength((jdoubleArray)javaValueObj); |
|
158 |
} |
|
159 |
else { |
|
160 |
len = env->GetArrayLength((jintArray)javaValueObj); |
|
161 |
} |
|
162 |
PyObject* pyList = PyList_New(len); |
|
163 |
//printf("һά����\n"); |
|
164 |
for (int i = 0; i < len; ++i) { |
|
165 |
if (strcmp(strClassName, "[D") == 0) { |
|
166 |
jdouble* javaArray = env->GetDoubleArrayElements((jdoubleArray)javaValueObj, NULL); |
|
167 |
PyList_SetItem(pyList, i, PyFloat_FromDouble(javaArray[i])); |
|
168 |
//printf("%f ", javaArray[i]); |
|
169 |
env->ReleaseDoubleArrayElements((jdoubleArray)javaValueObj, javaArray, 0); |
|
170 |
} |
|
171 |
else { |
|
172 |
jint* javaArray = env->GetIntArrayElements((jintArray)javaValueObj, NULL); |
|
173 |
PyList_SetItem(pyList, i, PyLong_FromLong(javaArray[i])); |
|
174 |
//printf("%d ", javaArray[i]); |
|
175 |
env->ReleaseIntArrayElements((jintArray)javaValueObj, javaArray, 0); |
|
176 |
} |
|
177 |
} |
|
178 |
//printf("\n"); |
|
179 |
pyNewValue = pyList; |
|
180 |
} |
|
181 |
else if (strcmp(strClassName, "[[D") == 0 || strcmp(strClassName, "[[I") == 0) { |
|
182 |
//����ά����ת��ΪPython���б�����ʹ���б���б���ģ���λ���� |
|
183 |
jsize outerLength = env->GetArrayLength((jobjectArray)javaValueObj); |
|
184 |
PyObject* pyList = PyList_New(outerLength); |
|
185 |
PyObject* pyRowList = NULL; |
|
186 |
//printf("������\n"); |
|
187 |
for (int i = 0; i < outerLength; ++i) { |
|
188 |
if (strcmp(strClassName, "[[I") == 0) { |
|
189 |
jintArray innerArray = (jintArray)env->GetObjectArrayElement((jobjectArray)javaValueObj, i); |
|
190 |
jsize innerLength = env->GetArrayLength(innerArray); |
|
191 |
pyRowList = PyList_New(innerLength); |
|
192 |
jint* innerArrayValues = env->GetIntArrayElements(innerArray, NULL); |
|
193 |
//printf("Array %d: ", i); |
|
194 |
for (int j = 0; j < innerLength; ++j) { |
|
195 |
PyList_SetItem(pyRowList, j, PyLong_FromLong(innerArrayValues[j])); |
|
196 |
//printf("%d ", innerArrayValues[j]); |
|
197 |
} |
|
198 |
//printf("\n"); |
|
199 |
env->ReleaseIntArrayElements(innerArray, innerArrayValues, 0); |
|
200 |
} |
|
201 |
else { |
|
202 |
jdoubleArray innerArray = (jdoubleArray)env->GetObjectArrayElement((jobjectArray)javaValueObj, i); |
|
203 |
jsize innerLength = env->GetArrayLength(innerArray); |
|
204 |
pyRowList = PyList_New(innerLength); |
|
205 |
jdouble* innerArrayValues = env->GetDoubleArrayElements(innerArray, NULL); |
|
206 |
//printf("Array %f: ", i); |
|
207 |
for (int j = 0; j < innerLength; ++j) { |
|
208 |
PyList_SetItem(pyRowList, j, PyLong_FromLong(innerArrayValues[j])); |
|
209 |
//printf("%f ", innerArrayValues[j]); |
|
210 |
} |
|
211 |
//printf("\n"); |
|
212 |
env->ReleaseDoubleArrayElements(innerArray, innerArrayValues, 0); |
|
213 |
} |
|
214 |
PyList_SetItem(pyList, i, pyRowList); |
|
215 |
} |
|
216 |
pyNewValue = pyList; |
|
217 |
} |
|
218 |
|
|
219 |
|
|
220 |
//��ȡ����key��Python�ֵ��е�item |
|
221 |
PyObject* pyKey = PyUnicode_FromString(parameterStr); |
|
222 |
if (pyNewValue) { |
|
223 |
PyDict_SetItem(pyDict, pyKey, pyNewValue); |
|
224 |
Py_DECREF(pyNewValue); |
|
225 |
} |
|
226 |
Py_DECREF(pyKey); |
|
227 |
// �ͷ�Java�ַ��� |
|
228 |
env->ReleaseStringUTFChars(parameterJS, parameterStr); |
|
229 |
|
|
230 |
} |
|
231 |
return pyDict; |
|
232 |
} |
|
233 |
|
|
234 |
//jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj); |
|
235 |
// |
|
236 |
//// �� Python һά�б�ת��Ϊ Java һά�������� |
|
237 |
//jobjectArray convertPyListToJArray(JNIEnv* env, PyObject* pyList, const char* elementType) { |
|
238 |
// Py_ssize_t size = PyList_Size(pyList); |
|
239 |
// jobjectArray jarray = env->NewObjectArray(size, env->FindClass(elementType), NULL); |
|
240 |
// /*if (jarray == NULL) { |
|
241 |
// return NULL; |
|
242 |
// }*/ |
|
243 |
// for (Py_ssize_t i = 0; i < size; ++i) { |
|
244 |
// PyObject* pyItem = PyList_GetItem(pyList, i); |
|
245 |
// jobject jItem = convertPyObjectToJObject(env, pyItem); |
|
246 |
// if (jItem == NULL) { |
|
247 |
// env->DeleteLocalRef(jarray); |
|
248 |
// return NULL; |
|
249 |
// } |
|
250 |
// env->SetObjectArrayElement(jarray, i, jItem); |
|
251 |
// env->DeleteLocalRef(jItem); |
|
252 |
// } |
|
253 |
// return jarray; |
|
254 |
//} |
|
255 |
// |
|
256 |
//// �� Python ��ά�б�ת��Ϊ Java ��ά�������� |
|
257 |
//jobjectArray convertPy2DListToJ2DObjectArray(JNIEnv* env, PyObject* py2DList, const char* elementType) { |
|
258 |
// Py_ssize_t rows = PyList_Size(py2DList); |
|
259 |
// jobjectArray j2DArray = env->NewObjectArray(rows, env->FindClass("[Ljava/lang/Object;"), NULL); |
|
260 |
// /*if (j2DArray == NULL) { |
|
261 |
// return NULL; |
|
262 |
// }*/ |
|
263 |
// for (Py_ssize_t i = 0; i < rows; ++i) { |
|
264 |
// PyObject* pyRow = PyList_GetItem(py2DList, i); |
|
265 |
// jobjectArray jRowArray = convertPyListToJArray(env, pyRow, elementType); |
|
266 |
// if (jRowArray == NULL) { |
|
267 |
// env->DeleteLocalRef(j2DArray); |
|
268 |
// return NULL; |
|
269 |
// } |
|
270 |
// env->SetObjectArrayElement(j2DArray, i, jRowArray); |
|
271 |
// env->DeleteLocalRef(jRowArray); |
|
272 |
// } |
|
273 |
// return j2DArray; |
|
274 |
//} |
|
275 |
// |
|
276 |
//jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj) { |
|
277 |
// if (PyLong_Check(pyObj)) { |
|
278 |
// long longVal = PyLong_AsLong(pyObj); |
|
279 |
// jclass longClass = env->FindClass("java/lang/Long"); |
|
280 |
// jmethodID longInit = env->GetMethodID(longClass, "<init>", "(J)V"); |
|
281 |
// return env->NewObject(longClass, longInit, longVal); |
|
282 |
// } |
|
283 |
// else if (PyFloat_Check(pyObj)) { |
|
284 |
// double doubleVal = PyFloat_AsDouble(pyObj); |
|
285 |
// jclass doubleClass = env->FindClass("java/lang/Double"); |
|
286 |
// jmethodID doubleInit = env->GetMethodID(doubleClass, "<init>", "(D)V"); |
|
287 |
// return env->NewObject(doubleClass, doubleInit, doubleVal); |
|
288 |
// } |
|
289 |
// else if (PyUnicode_Check(pyObj)) { |
|
290 |
// const char* strVal = PyUnicode_AsUTF8(pyObj); |
|
291 |
// return env->NewStringUTF(strVal); |
|
292 |
// } |
|
293 |
// else if (PyBool_Check(pyObj)) { |
|
294 |
// jboolean boolVal = (jboolean)(PyLong_AsLong(pyObj) != 0); |
|
295 |
// jclass booleanClass = env->FindClass("java/lang/Boolean"); |
|
296 |
// jmethodID booleanInit = env->GetMethodID(booleanClass, "<init>", "(Z)V"); |
|
297 |
// return env->NewObject(booleanClass, booleanInit, boolVal); |
|
298 |
// } |
|
299 |
// else if (PyList_Check(pyObj)) { |
|
300 |
// const char* elementType = "java/lang/Object"; |
|
301 |
// for (Py_ssize_t i = 0; i < PyList_Size(pyObj); i++) { |
|
302 |
// PyObject* item = PyList_GetItem(pyObj, i); |
|
303 |
// // ����Ƕ�ά���� |
|
304 |
// if (PyList_Check(item)) { |
|
305 |
// return convertPy2DListToJ2DObjectArray(env, pyObj, elementType); |
|
306 |
// } |
|
307 |
// else { |
|
308 |
// return convertPyListToJArray(env, pyObj, elementType); |
|
309 |
// } |
|
310 |
// } |
|
311 |
// } |
|
312 |
// else if (PyDict_Check(pyObj)) |
|
313 |
// { |
|
314 |
// cout << "dict" << endl; |
|
315 |
// } |
|
316 |
// return NULL; |
|
317 |
//} |
|
318 |
// |
|
319 |
// |
|
320 |
//// python���ֵ�����תjava�Ĺ�ϣ���� |
|
321 |
//// ConvertPydictToJhmap |
|
322 |
//jobject ConvertPydictToJhmap(JNIEnv* env, PyObject* pyDict) { |
|
323 |
// jclass hashMapClass = env->FindClass("java/util/HashMap"); |
|
324 |
// jmethodID hashMapInit = env->GetMethodID(hashMapClass, "<init>", "()V"); |
|
325 |
// jobject hashMap = env->NewObject(hashMapClass, hashMapInit); |
|
326 |
// /*if (hashMap == NULL) { |
|
327 |
// return NULL; |
|
328 |
// }*/ |
|
329 |
// Py_ssize_t pos = 0; |
|
330 |
// PyObject* key; |
|
331 |
// PyObject* value; |
|
332 |
// while (PyDict_Next(pyDict, &pos, &key, &value)) { |
|
333 |
// const char* keyStr = PyUnicode_AsUTF8(key); |
|
334 |
// jstring jKey = env->NewStringUTF(keyStr); |
|
335 |
// jobject jValue = convertPyObjectToJObject(env, value); |
|
336 |
// if (jValue != NULL) { |
|
337 |
// jmethodID putMethod = env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); |
|
338 |
// env->CallObjectMethod(hashMap, putMethod, jKey, jValue); |
|
339 |
// env->DeleteLocalRef(jValue); |
|
340 |
// } |
|
341 |
// else return NULL; |
|
342 |
// env->DeleteLocalRef(jKey); |
|
343 |
// } |
|
344 |
// return hashMap; |
|
345 |
//} |
|
346 |
|
|
347 |
jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj); |
|
348 |
// �� Python һά�б�ת��Ϊ Java һά�������� |
|
349 |
jobjectArray convertPyListToJArray(JNIEnv* env, PyObject* pyList, const char* elementType) { |
|
350 |
Py_ssize_t size = PyList_Size(pyList); |
|
351 |
jobjectArray jarray = env->NewObjectArray(size, env->FindClass(elementType), NULL); |
|
352 |
/*if (jarray == NULL) { |
|
353 |
return NULL; |
|
354 |
}*/ |
|
355 |
for (Py_ssize_t i = 0; i < size; ++i) { |
|
356 |
PyObject* pyItem = PyList_GetItem(pyList, i); |
|
357 |
jobject jItem = convertPyObjectToJObject(env, pyItem); |
|
358 |
if (jItem == NULL) { |
|
359 |
env->DeleteLocalRef(jarray); |
|
360 |
return NULL; |
|
361 |
} |
|
362 |
env->SetObjectArrayElement(jarray, i, jItem); |
|
363 |
env->DeleteLocalRef(jItem); |
|
364 |
} |
|
365 |
return jarray; |
|
366 |
} |
|
367 |
|
|
368 |
|
|
369 |
// �� Python ��ά�б�ת��Ϊ Java ��ά�������� |
|
370 |
jobjectArray convertPy2DListToJ2DObjectArray(JNIEnv* env, PyObject* py2DList, const char* elementType) { |
|
371 |
Py_ssize_t rows = PyList_Size(py2DList); |
|
372 |
jobjectArray j2DArray = env->NewObjectArray(rows, env->FindClass("[Ljava/lang/Object;"), NULL); |
|
373 |
/*if (j2DArray == NULL) { |
|
374 |
return NULL; |
|
375 |
}*/ |
|
376 |
for (Py_ssize_t i = 0; i < rows; ++i) { |
|
377 |
PyObject* pyRow = PyList_GetItem(py2DList, i); |
|
378 |
jobjectArray jRowArray = convertPyListToJArray(env, pyRow, elementType); |
|
379 |
if (jRowArray == NULL) { |
|
380 |
env->DeleteLocalRef(j2DArray); |
|
381 |
return NULL; |
|
382 |
} |
|
383 |
env->SetObjectArrayElement(j2DArray, i, jRowArray); |
|
384 |
env->DeleteLocalRef(jRowArray); |
|
385 |
} |
|
386 |
return j2DArray; |
|
387 |
} |
|
388 |
|
|
389 |
jobject convertPyObjectToJObject(JNIEnv* env, PyObject* pyObj) { |
|
390 |
if (PyLong_Check(pyObj)) { |
|
391 |
long longVal = PyLong_AsLong(pyObj); |
|
392 |
jclass longClass = env->FindClass("java/lang/Long"); |
|
393 |
jmethodID longInit = env->GetMethodID(longClass, "<init>", "(J)V"); |
|
394 |
return env->NewObject(longClass, longInit, longVal); |
|
395 |
} |
|
396 |
else if (PyFloat_Check(pyObj)) { |
|
397 |
double doubleVal = PyFloat_AsDouble(pyObj); |
|
398 |
jclass doubleClass = env->FindClass("java/lang/Double"); |
|
399 |
jmethodID doubleInit = env->GetMethodID(doubleClass, "<init>", "(D)V"); |
|
400 |
return env->NewObject(doubleClass, doubleInit, doubleVal); |
|
401 |
} |
|
402 |
else if (PyUnicode_Check(pyObj)) { |
|
403 |
const char* strVal = PyUnicode_AsUTF8(pyObj); |
|
404 |
return env->NewStringUTF(strVal); |
|
405 |
} |
|
406 |
else if (PyBool_Check(pyObj)) { |
|
407 |
jboolean boolVal = (jboolean)(PyLong_AsLong(pyObj) != 0); |
|
408 |
jclass booleanClass = env->FindClass("java/lang/Boolean"); |
|
409 |
jmethodID booleanInit = env->GetMethodID(booleanClass, "<init>", "(Z)V"); |
|
410 |
return env->NewObject(booleanClass, booleanInit, boolVal); |
|
411 |
} |
|
412 |
else if (PyList_Check(pyObj)) { |
|
413 |
const char* elementType = "java/lang/Object"; |
|
414 |
for (Py_ssize_t i = 0; i < PyList_Size(pyObj); i++) { |
|
415 |
PyObject* item = PyList_GetItem(pyObj, i); |
|
416 |
// ����Ƕ�ά���� |
|
417 |
if (PyList_Check(item)) { |
|
418 |
return convertPy2DListToJ2DObjectArray(env, pyObj, elementType); |
|
419 |
} |
|
420 |
else { |
|
421 |
return convertPyListToJArray(env, pyObj, elementType); |
|
422 |
} |
|
423 |
} |
|
424 |
} |
|
425 |
else if (PyDict_Check(pyObj)) { |
|
426 |
return ConvertPydictToJhmap(env, pyObj); |
|
427 |
} |
|
428 |
return NULL; |
|
429 |
} |
|
430 |
|
|
431 |
|
|
432 |
// python���ֵ�����תjava�Ĺ�ϣ���� |
|
433 |
jobject ConvertPydictToJhmap(JNIEnv* env, PyObject* pyDict) { |
|
434 |
jclass hashMapClass = env->FindClass("java/util/HashMap"); |
|
435 |
jmethodID hashMapInit = env->GetMethodID(hashMapClass, "<init>", "()V"); |
|
436 |
jobject hashMap = env->NewObject(hashMapClass, hashMapInit); |
|
437 |
if (hashMap == NULL) { |
|
438 |
return NULL; |
|
439 |
} |
|
440 |
Py_ssize_t pos = 0; |
|
441 |
PyObject* key; |
|
442 |
PyObject* value; |
|
443 |
while (PyDict_Next(pyDict, &pos, &key, &value)) { |
|
444 |
const char* keyStr = PyUnicode_AsUTF8(key); |
|
445 |
jstring jKey = env->NewStringUTF(keyStr); |
|
446 |
jobject jValue = convertPyObjectToJObject(env, value); |
|
447 |
if (jValue != NULL) { |
|
448 |
jmethodID putMethod = env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); |
|
449 |
env->CallObjectMethod(hashMap, putMethod, jKey, jValue); |
|
450 |
env->DeleteLocalRef(jValue); |
|
451 |
} |
|
452 |
else return NULL; |
|
453 |
env->DeleteLocalRef(jKey); |
|
454 |
} |
|
455 |
return hashMap; |
|
456 |
} |