| 46 | } |
| 47 | |
| 48 | void Env::createEnv() |
| 49 | { |
| 50 | if (_envIfKnown == nullptr) { |
| 51 | if (_globalVm == nullptr) { |
| 52 | throw std::runtime_error("bdn::java::Env used from unattached thread, but " |
| 53 | "global VM pointer not initialized."); |
| 54 | } |
| 55 | |
| 56 | JNIEnv *env = nullptr; |
| 57 | bool mustDetachThread = false; |
| 58 | int getResult = |
| 59 | _globalVm->GetEnv((void **)&env, JNI_VERSION_1_6); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast) |
| 60 | if (getResult == JNI_EDETACHED) { |
| 61 | // thread is not yet attached to the VM. Attach it. |
| 62 | int attachResult = _globalVm->AttachCurrentThread(&env, nullptr); |
| 63 | if (attachResult != JNI_OK) { |
| 64 | throw std::runtime_error("Fatal error: unable to attach JNI to current " |
| 65 | "thread (JavaVM::AttachCurrentThread result " + |
| 66 | std::to_string(attachResult) + ")."); |
| 67 | } |
| 68 | |
| 69 | mustDetachThread = true; |
| 70 | } else if (getResult != JNI_OK) { |
| 71 | throw std::runtime_error("Fatal error: unable to get JNI env for current thread " |
| 72 | "(JNIEnv::GetEnv result " + |
| 73 | std::to_string(getResult) + "."); |
| 74 | } |
| 75 | |
| 76 | setEnv(env, mustDetachThread); |
| 77 | } |
| 78 | } |
| 79 | } |
nothing calls this directly
no test coverage detected