1 /* Various JNI helper functions */
3 /* Copyright (c) 2007-2019. 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);
36 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
37 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
38 const char *classname = env->GetStringUTFChars(jclassname, 0);
40 env->ReleaseStringUTFChars(jclassname, classname);
42 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
49 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
55 id = env->GetStaticMethodID(cls, name, signature);
58 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
59 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
60 const char *classname = env->GetStringUTFChars(jclassname, 0);
62 env->ReleaseStringUTFChars(jclassname, classname);
64 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
71 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
75 cls = jxbt_get_class(env, classname);
80 id = env->GetStaticMethodID(cls, name, signature);
83 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
89 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
93 cls = jxbt_get_class(env, classname);
98 id = env->GetMethodID(cls, name, signature);
101 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
107 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
114 id = env->GetFieldID(cls, name, signature);
117 jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
118 jstring jclassname = (jstring) env->CallObjectMethod(cls, getname_id, nullptr);
119 const char *classname = env->GetStringUTFChars(jclassname, 0);
121 env->ReleaseStringUTFChars(jclassname, classname);
123 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
131 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
133 jclass cls = jxbt_get_class(env, classname);
139 id = env->GetFieldID(cls, name, signature);
142 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
149 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg)
151 jclass cls = env->FindClass(name);
153 xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
155 env->ThrowNew(cls, msg.c_str());
158 void jxbt_throw_jni(JNIEnv* env, const std::string& msg)
160 jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
163 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer)
165 jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
166 simgrid::xbt::string_printf("Internal error: %s %p not bound", kind.c_str(), pointer));
169 void jxbt_throw_null(JNIEnv* env, const std::string& msg)
171 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
174 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg)
176 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
179 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
181 jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
184 void jxbt_throw_storage_not_found(JNIEnv* env, const std::string& invalid_name)
186 jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", "No such storage: " + invalid_name);
189 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
191 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
194 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
196 jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
199 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
201 jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
204 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)
206 jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
209 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details)
211 jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);