1 /* Various JNI helper functions */
3 /* Copyright (c) 2007-2017. 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 "xbt/sysdep.h"
9 #include "jxbt_utilities.h"
11 #include <cstdlib> /* abort */
15 jclass jxbt_get_class(JNIEnv * env, const char *name)
17 jclass cls = env->FindClass(name);
20 jxbt_throw_jni(env, std::string("Class ") + name + " not found");
27 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
33 id = env->GetMethodID(cls, name, signature);
37 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
38 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
39 const char *classname = env->GetStringUTFChars(jclassname, 0);
41 env->ReleaseStringUTFChars(jclassname, classname);
43 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
50 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
56 id = env->GetStaticMethodID(cls, name, signature);
59 jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
60 jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
61 const char *classname = env->GetStringUTFChars(jclassname, 0);
63 env->ReleaseStringUTFChars(jclassname, classname);
65 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
72 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
76 cls = jxbt_get_class(env, classname);
81 id = env->GetStaticMethodID(cls, name, signature);
84 jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
90 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
94 cls = jxbt_get_class(env, classname);
99 id = env->GetMethodID(cls, name, signature);
102 jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
108 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
115 id = env->GetFieldID(cls, name, signature);
118 jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
119 jstring jclassname = (jstring) env->CallObjectMethod(cls, getname_id, nullptr);
120 const char *classname = env->GetStringUTFChars(jclassname, 0);
122 env->ReleaseStringUTFChars(jclassname, classname);
124 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
132 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
134 jclass cls = jxbt_get_class(env, classname);
140 id = env->GetFieldID(cls, name, signature);
143 jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
150 void jxbt_throw_by_name(JNIEnv* env, const char* name, std::string msg)
152 jclass cls = env->FindClass(name);
154 xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
156 env->ThrowNew(cls, msg.c_str());
159 void jxbt_throw_jni(JNIEnv* env, std::string msg)
161 jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
164 void jxbt_throw_notbound(JNIEnv* env, std::string kind, void* pointer)
166 jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
167 "Internal error: " + kind + " " + static_cast<const char*>(pointer) + " not bound");
170 void jxbt_throw_null(JNIEnv* env, std::string msg)
172 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
175 void jxbt_throw_illegal(JNIEnv* env, std::string msg)
177 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
180 void jxbt_throw_host_not_found(JNIEnv* env, std::string invalid_name)
182 jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
185 void jxbt_throw_storage_not_found(JNIEnv* env, std::string invalid_name)
187 jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", "No such storage: " + invalid_name);
190 void jxbt_throw_process_not_found(JNIEnv* env, std::string invalid_name)
192 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
195 void jxbt_throw_transfer_failure(JNIEnv* env, std::string details)
197 jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
200 void jxbt_throw_host_failure(JNIEnv* env, std::string details)
202 jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
205 void jxbt_throw_time_out_failure(JNIEnv* env, std::string details)
207 jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
210 void jxbt_throw_task_cancelled(JNIEnv* env, std::string details)
212 jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);