1 /* Various JNI helper functions */
3 /* Copyright (c) 2007-2020. 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 #include <cstdlib> /* abort */
14 jclass jxbt_get_class(JNIEnv * env, const char *name)
16 jclass cls = env->FindClass(name);
19 jxbt_throw_jni(env, std::string("Class ") + name + " not found");
26 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
32 id = env->GetMethodID(cls, name, signature);
35 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
36 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
37 const char *classname = env->GetStringUTFChars(jclassname, 0);
39 env->ReleaseStringUTFChars(jclassname, classname);
41 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
48 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
54 id = env->GetStaticMethodID(cls, name, signature);
57 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
58 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
59 const char *classname = env->GetStringUTFChars(jclassname, 0);
61 env->ReleaseStringUTFChars(jclassname, classname);
63 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
70 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
74 cls = jxbt_get_class(env, classname);
79 id = env->GetStaticMethodID(cls, name, signature);
82 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
88 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
92 cls = jxbt_get_class(env, classname);
97 id = env->GetMethodID(cls, name, signature);
100 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
106 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
113 id = env->GetFieldID(cls, name, signature);
116 jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
117 jstring jclassname = (jstring) env->CallObjectMethod(cls, getname_id, nullptr);
118 const char *classname = env->GetStringUTFChars(jclassname, 0);
120 env->ReleaseStringUTFChars(jclassname, classname);
122 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
130 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
132 jclass cls = jxbt_get_class(env, classname);
138 id = env->GetFieldID(cls, name, signature);
141 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
148 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg)
150 jclass cls = env->FindClass(name);
152 xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
154 env->ThrowNew(cls, msg.c_str());
157 void jxbt_throw_jni(JNIEnv* env, const std::string& msg)
159 jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
162 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer)
164 jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
165 simgrid::xbt::string_printf("Internal error: %s %p not bound", kind.c_str(), pointer));
168 void jxbt_throw_null(JNIEnv* env, const std::string& msg)
170 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
173 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg)
175 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
178 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
180 jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
183 void jxbt_throw_storage_not_found(JNIEnv* env, const std::string& invalid_name)
185 jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", "No such storage: " + invalid_name);
188 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
190 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
193 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
195 jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
198 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
200 jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
203 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)
205 jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
208 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details)
210 jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);