-
-JNIEXPORT jobject JNICALL
-Java_org_simgrid_msg_VM_get_pm(JNIEnv *env, jobject jvm) {
- jobject jhost;
- msg_vm_t vm = jvm_get_native(env,jvm);
- msg_host_t host = MSG_vm_get_pm(vm);
-
- if (!host->extension(JAVA_HOST_LEVEL)) {
- THROW_DEADCODE;
- /* the native host not yet associated with the java host instance */
-
- /* instanciate a new java host instance */
- jhost = jhost_new_instance(env);
-
- if (!jhost) {
- jxbt_throw_jni(env, "java host instantiation failed");
- return NULL;
- }
-
- /* get a global reference to the newly created host */
- jhost = jhost_ref(env, jhost);
-
- if (!jhost) {
- jxbt_throw_jni(env, "global ref allocation failed");
- return NULL;
- }
- /* Sets the host name */
- const char *name = MSG_host_get_name(host);
- jobject jname = env->NewStringUTF(name);
- env->SetObjectField(jhost, jxbt_get_jfield(env,
- env->FindClass("org/simgrid/msg/Host"), "name", "Ljava/lang/String;"),
- jname);
- /* Bind & store it */
- jhost_bind(jhost, host, env);
- host->extension_set(JAVA_HOST_LEVEL, (void *) jhost);
- } else {
- jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
+ /* get the C string from the java string */
+ if (jname == nullptr) {
+ jxbt_throw_null(env, bprintf("No VM can have a null name"));
+ return nullptr;
+ }
+ const char* name = env->GetStringUTFChars(jname, 0);
+ /* get the VM by name (VMs are just special hosts, unfortunately) */
+ msg_host_t host = MSG_host_by_name(name);
+
+ if (not host) { /* invalid name */
+ jxbt_throw_host_not_found(env, name);
+ env->ReleaseStringUTFChars(jname, name);
+ return nullptr;