1 /* Various JNI helper functions */
3 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "jxbt_utilities.hpp"
9 #include "xbt/string.hpp"
10 #include "xbt/sysdep.h"
12 jclass jxbt_get_class(JNIEnv * env, const char *name)
14 jclass cls = env->FindClass(name);
17 jxbt_throw_jni(env, std::string("Class ") + name + " not found");
24 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
30 id = env->GetMethodID(cls, name, signature);
33 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
34 auto jclassname = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
35 const char* classname = env->GetStringUTFChars(jclassname, nullptr);
37 env->ReleaseStringUTFChars(jclassname, classname);
39 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
46 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
52 id = env->GetStaticMethodID(cls, name, signature);
55 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
56 auto jclassname = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
57 const char* classname = env->GetStringUTFChars(jclassname, nullptr);
59 env->ReleaseStringUTFChars(jclassname, classname);
61 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
68 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
72 cls = jxbt_get_class(env, classname);
77 id = env->GetStaticMethodID(cls, name, signature);
80 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
86 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
90 cls = jxbt_get_class(env, classname);
95 id = env->GetMethodID(cls, name, signature);
98 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
104 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
111 id = env->GetFieldID(cls, name, signature);
114 jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
115 auto jclassname = (jstring)env->CallObjectMethod(cls, getname_id, nullptr);
116 const char* classname = env->GetStringUTFChars(jclassname, nullptr);
118 env->ReleaseStringUTFChars(jclassname, classname);
120 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
128 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
130 jclass cls = jxbt_get_class(env, classname);
136 id = env->GetFieldID(cls, name, signature);
139 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
146 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg)
148 jclass cls = env->FindClass(name);
150 xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
152 env->ThrowNew(cls, msg.c_str());
155 void jxbt_throw_jni(JNIEnv* env, const std::string& msg)
157 jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
160 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer)
162 jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
163 simgrid::xbt::string_printf("Internal error: %s %p not bound", kind.c_str(), pointer));
166 void jxbt_throw_null(JNIEnv* env, const std::string& msg)
168 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
171 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg)
173 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
176 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
178 jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
181 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
183 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
186 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
188 jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
191 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
193 jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
196 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)
198 jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
201 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details)
203 jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);