| 256 | } |
| 257 | |
| 258 | std::map<std::string, int> JavaMapStringIntToMap(JNIEnv *env, jobject hashMap) { |
| 259 | std::map<std::string, int> resultMap; |
| 260 | if (hashMap == nullptr) { |
| 261 | return resultMap; |
| 262 | } |
| 263 | jclass hashMapClass = env->GetObjectClass(hashMap); |
| 264 | if (hashMapClass == nullptr) { |
| 265 | return resultMap; |
| 266 | } |
| 267 | jmethodID entrySetMethodID = env->GetMethodID(hashMapClass, "entrySet", |
| 268 | "()Ljava/util/Set;"); |
| 269 | if (entrySetMethodID == nullptr) { |
| 270 | env->DeleteLocalRef(hashMapClass); |
| 271 | return resultMap; |
| 272 | } |
| 273 | jobject entrySet = env->CallObjectMethod(hashMap, entrySetMethodID); |
| 274 | if (env->ExceptionCheck()) { |
| 275 | env->ExceptionDescribe(); |
| 276 | env->ExceptionClear(); |
| 277 | env->DeleteLocalRef(hashMapClass); |
| 278 | return resultMap; |
| 279 | } |
| 280 | jclass setClass = env->FindClass("java/util/Set"); |
| 281 | jmethodID iteratorMethodID = env->GetMethodID(setClass, "iterator", |
| 282 | "()Ljava/util/Iterator;"); |
| 283 | jobject iterator = env->CallObjectMethod(entrySet, iteratorMethodID); |
| 284 | if (env->ExceptionCheck() || iterator == nullptr) { |
| 285 | env->ExceptionDescribe(); |
| 286 | env->ExceptionClear(); |
| 287 | env->DeleteLocalRef(hashMapClass); |
| 288 | env->DeleteLocalRef(entrySet); |
| 289 | env->DeleteLocalRef(setClass); |
| 290 | return resultMap; // 空map |
| 291 | } |
| 292 | jclass iteratorClass = env->FindClass("java/util/Iterator"); |
| 293 | jmethodID hasNextMethodID = env->GetMethodID(iteratorClass, "hasNext", "()Z"); |
| 294 | jmethodID nextMethodID = env->GetMethodID(iteratorClass, "next", |
| 295 | "()Ljava/lang/Object;"); |
| 296 | |
| 297 | jclass mapEntryClass = env->FindClass("java/util/Map$Entry"); |
| 298 | jmethodID getKeyMethodID = env->GetMethodID(mapEntryClass, "getKey", |
| 299 | "()Ljava/lang/Object;"); |
| 300 | jmethodID getValueMethodID = env->GetMethodID(mapEntryClass, "getValue", |
| 301 | "()Ljava/lang/Object;"); |
| 302 | |
| 303 | |
| 304 | jclass stringClass = env->FindClass("java/lang/String"); |
| 305 | jclass integerClass = env->FindClass("java/lang/Integer"); |
| 306 | jmethodID intValueMethodID = env->GetMethodID(integerClass, "intValue", "()I"); |
| 307 | |
| 308 | jmethodID toStringMethodID = env->GetMethodID(stringClass, "toString", |
| 309 | "()Ljava/lang/String;"); |
| 310 | while (env->CallBooleanMethod(iterator, hasNextMethodID)) { |
| 311 | jobject entry = env->CallObjectMethod(iterator, nextMethodID); |
| 312 | if (env->ExceptionCheck() || entry == nullptr) { |
| 313 | env->ExceptionDescribe(); |
| 314 | env->ExceptionClear(); |
| 315 | break; // 跳出循环 |
nothing calls this directly
no test coverage detected