Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
java: setup a RAII wrapper to properly deal with GetStringUTFChars/ReleaseStringUTFChar
[simgrid.git] / src / bindings / java / jxbt_utilities.cpp
index 9907ef0..79142ae 100644 (file)
@@ -1,6 +1,6 @@
 /* Various JNI helper functions                                             */
 
-/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -9,8 +9,6 @@
 #include "xbt/string.hpp"
 #include "xbt/sysdep.h"
 
-#include <cstdlib> /* abort */
-
 jclass jxbt_get_class(JNIEnv * env, const char *name)
 {
   jclass cls = env->FindClass(name);
@@ -33,12 +31,11 @@ jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const cha
 
   if (not id) {
     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
-    auto jclassname       = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
-    const char* classname = env->GetStringUTFChars(jclassname, nullptr);
+    auto jclassname    = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
+    jstring_wrapper classname(env, jclassname);
+    auto msg = std::string("Cannot find method") + name + "(" + signature + ") in " + classname.value;
 
-    env->ReleaseStringUTFChars(jclassname, classname);
-
-    jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
+    jxbt_throw_jni(env, msg);
     return nullptr;
   }
 
@@ -56,11 +53,10 @@ jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, co
   if (not id) {
     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
     auto jclassname       = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
-    const char* classname = env->GetStringUTFChars(jclassname, nullptr);
+    jstring_wrapper classname(env, jclassname);
+    auto msg = std::string("Cannot find static method") + name + "(" + signature + ") in " + classname.value;
 
-    env->ReleaseStringUTFChars(jclassname, classname);
-
-    jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
+    jxbt_throw_jni(env, msg);
     return nullptr;
   }
 
@@ -180,11 +176,6 @@ void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
   jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
 }
 
-void jxbt_throw_storage_not_found(JNIEnv* env, const std::string& invalid_name)
-{
-  jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", "No such storage: " + invalid_name);
-}
-
 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
 {
   jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
@@ -197,7 +188,7 @@ void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
 
 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
 {
-  jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
+  jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure" + details);
 }
 
 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)