| 177 | } |
| 178 | |
| 179 | std::map<int, int> JavaMapIntIntToMap(JNIEnv *env, jobject hashMap) { |
| 180 | std::map<int, int> resultMap; |
| 181 | if (hashMap == nullptr) { |
| 182 | return resultMap; |
| 183 | } |
| 184 | jclass hashMapClass = env->GetObjectClass(hashMap); |
| 185 | if (hashMapClass == nullptr) { |
| 186 | return resultMap; |
| 187 | } |
| 188 | jmethodID entrySetMethodID = env->GetMethodID(hashMapClass, "entrySet", |
| 189 | "()Ljava/util/Set;"); |
| 190 | if (entrySetMethodID == nullptr) { |
| 191 | env->DeleteLocalRef(hashMapClass); |
| 192 | return resultMap; |
| 193 | } |
| 194 | jobject entrySet = env->CallObjectMethod(hashMap, entrySetMethodID); |
| 195 | if (env->ExceptionCheck()) { |
| 196 | env->ExceptionDescribe(); |
| 197 | env->ExceptionClear(); |
| 198 | env->DeleteLocalRef(hashMapClass); |
| 199 | return resultMap; |
| 200 | } |
| 201 | jclass setClass = env->FindClass("java/util/Set"); |
| 202 | jmethodID iteratorMethodID = env->GetMethodID(setClass, "iterator", |
| 203 | "()Ljava/util/Iterator;"); |
| 204 | jobject iterator = env->CallObjectMethod(entrySet, iteratorMethodID); |
| 205 | if (env->ExceptionCheck() || iterator == nullptr) { |
| 206 | env->ExceptionDescribe(); |
| 207 | env->ExceptionClear(); |
| 208 | env->DeleteLocalRef(hashMapClass); |
| 209 | env->DeleteLocalRef(entrySet); |
| 210 | env->DeleteLocalRef(setClass); |
| 211 | return resultMap; // 空map |
| 212 | } |
| 213 | jclass iteratorClass = env->FindClass("java/util/Iterator"); |
| 214 | jmethodID hasNextMethodID = env->GetMethodID(iteratorClass, "hasNext", "()Z"); |
| 215 | jmethodID nextMethodID = env->GetMethodID(iteratorClass, "next", |
| 216 | "()Ljava/lang/Object;"); |
| 217 | |
| 218 | jclass mapEntryClass = env->FindClass("java/util/Map$Entry"); |
| 219 | jmethodID getKeyMethodID = env->GetMethodID(mapEntryClass, "getKey", |
| 220 | "()Ljava/lang/Object;"); |
| 221 | jmethodID getValueMethodID = env->GetMethodID(mapEntryClass, "getValue", |
| 222 | "()Ljava/lang/Object;"); |
| 223 | |
| 224 | |
| 225 | jclass integerClass = env->FindClass("java/lang/Integer"); |
| 226 | jmethodID intValueMethodID = env->GetMethodID(integerClass, "intValue", "()I"); |
| 227 | |
| 228 | while (env->CallBooleanMethod(iterator, hasNextMethodID)) { |
| 229 | jobject entry = env->CallObjectMethod(iterator, nextMethodID); |
| 230 | if (env->ExceptionCheck() || entry == nullptr) { |
| 231 | env->ExceptionDescribe(); |
| 232 | env->ExceptionClear(); |
| 233 | break; // 跳出循环 |
| 234 | } |
| 235 | jobject keyObejct = (env->CallObjectMethod(entry, getKeyMethodID)); |
| 236 | if (!env->IsInstanceOf(keyObejct, integerClass)) { |
no outgoing calls
no test coverage detected