4 * Various JNI helper functions
6 * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package.
13 #include <stdlib.h> /* abort */
15 #include "xbt/sysdep.h"
17 #include "jxbt_utilities.h"
23 jclass jxbt_get_class(JNIEnv * env, const char *name)
25 jclass cls = (*env)->FindClass(env, name);
28 char *m = bprintf("Class %s not found", name);
29 jxbt_throw_jni(env, m);
37 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls,
38 const char *name, const char *signature)
44 id = (*env)->GetMethodID(env, cls, name, signature);
49 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
51 (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
52 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
55 bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
57 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
59 jxbt_throw_jni(env, m);
68 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls,
69 const char *name, const char *signature)
75 id = (*env)->GetStaticMethodID(env, cls, name, signature);
80 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
82 (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
83 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
86 bprintf("Cannot find static method %s(%s) in %s", name, signature,
89 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
91 jxbt_throw_jni(env, m);
100 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname,
101 const char *name, const char *signature)
106 cls = jxbt_get_class(env, classname);
111 id = (*env)->GetStaticMethodID(env, cls, name, signature);
115 bprintf("Cannot find static method %s(%s) in %s", name, signature,
118 jxbt_throw_jni(env, m);
126 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname,
127 const char *name, const char *signature)
132 cls = jxbt_get_class(env, classname);
137 id = (*env)->GetMethodID(env, cls, name, signature);
141 bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
143 jxbt_throw_jni(env, m);
151 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
152 const char *name, const char *signature)
159 id = (*env)->GetFieldID(env, cls, name, signature);
162 jmethodID getname_id =
163 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
165 (jstring) (*env)->CallObjectMethod(env, cls, getname_id, NULL);
166 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
168 bprintf("Cannot find field %s %s in %s", signature, name, classname);
170 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
172 jxbt_throw_jni(env, m);
181 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname,
182 const char *name, const char *signature)
184 jclass cls = jxbt_get_class(env, classname);
190 id = (*env)->GetFieldID(env, cls, name, signature);
194 bprintf("Cannot find field %s %s in %s", signature, name, classname);
196 jxbt_throw_jni(env, m);
205 /* ***************** */
206 /* EXCEPTION RAISING */
207 /* ***************** */
208 static void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
210 jclass cls = (*env)->FindClass(env, name);
212 xbt_assert2(cls, "%s (Plus severe error: class %s not found)\n", msg, name);
214 (*env)->ThrowNew(env, cls, msg);
221 void jxbt_throw_jni(JNIEnv * env, const char *msg)
223 jxbt_throw_by_name(env,
224 "simgrid/msg/JniException",
225 bprintf("Internal or JNI error: %s", msg));
228 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
230 jxbt_throw_by_name(env,
231 "simgrid/msg/JniException",
232 bprintf("Internal error: %s %p not bound", kind,
236 void jxbt_throw_native(JNIEnv * env, char *msg)
238 jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
242 void jxbt_throw_null(JNIEnv * env, char *msg)
244 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
247 /* Errors on user side */
248 void jxbt_throw_illegal(JNIEnv * env, char *msg)
250 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
253 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
255 jxbt_throw_by_name(env,
256 "simgrid/msg/HostNotFoundException",
257 bprintf("No such host: %s", invalid_name));
260 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
262 jxbt_throw_by_name(env,
263 "simgrid/msg/ProcessNotFoundException",
264 bprintf("No such process: %s", invalid_name));
268 void jxbt_throw_transfer_failure(JNIEnv *env,char *details) {
270 jxbt_throw_by_name(env,
271 "simgrid/msg/TransferFailureException",
276 // host failure Exception
277 void jxbt_throw_host_failure(JNIEnv *env,char *details) {
279 jxbt_throw_by_name(env,
280 "simgrid/msg/HostFailureException",
281 bprintf("Host Failure %s",details));
285 // time out failure Exception
286 void jxbt_throw_time_out_failure(JNIEnv *env,char *details) {
288 jxbt_throw_by_name(env,
289 "simgrid/msg/TimeoutException",
294 // task Cancelled exception
295 void jxbt_throw_task_cancelled(JNIEnv *env,char *details)
298 jxbt_throw_by_name(env,
299 "simgrid/msg/TaskCancelledException",