1 /* Various JNI helper functions */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <stdlib.h> /* abort */
11 #include "xbt/sysdep.h"
13 #include "jxbt_utilities.h"
19 jclass jxbt_get_class(JNIEnv * env, const char *name)
21 jclass cls = (*env)->FindClass(env, name);
24 char *m = bprintf("Class %s not found", name);
25 jxbt_throw_jni(env, m);
33 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls,
34 const char *name, const char *signature)
40 id = (*env)->GetMethodID(env, cls, name, signature);
45 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
47 (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
48 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
50 char *m = bprintf("Cannot find method %s(%s) in %s", name, signature,
53 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
55 jxbt_throw_jni(env, m);
64 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls,
65 const char *name, const char *signature)
71 id = (*env)->GetStaticMethodID(env, cls, name, signature);
76 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
78 (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
79 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
82 bprintf("Cannot find static method %s(%s) in %s", name, signature,
85 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
87 jxbt_throw_jni(env, m);
96 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname,
97 const char *name, const char *signature)
102 cls = jxbt_get_class(env, classname);
107 id = (*env)->GetStaticMethodID(env, cls, name, signature);
111 bprintf("Cannot find static method %s(%s) in %s", name, signature,
114 jxbt_throw_jni(env, m);
122 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname,
123 const char *name, const char *signature)
128 cls = jxbt_get_class(env, classname);
133 id = (*env)->GetMethodID(env, cls, name, signature);
136 char *m = bprintf("Cannot find method %s(%s) in %s", name, signature,
139 jxbt_throw_jni(env, m);
147 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
148 const char *name, const char *signature)
155 id = (*env)->GetFieldID(env, cls, name, signature);
158 jmethodID getname_id =
159 (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
161 (jstring) (*env)->CallObjectMethod(env, cls, getname_id, NULL);
162 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
163 char *m = bprintf("Cannot find field %s %s in %s", signature, name,
166 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
168 jxbt_throw_jni(env, m);
177 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname,
178 const char *name, const char *signature)
180 jclass cls = jxbt_get_class(env, classname);
186 id = (*env)->GetFieldID(env, cls, name, signature);
189 char *m = bprintf("Cannot find field %s %s in %s", signature, name,
192 jxbt_throw_jni(env, m);
201 /* ***************** */
202 /* EXCEPTION RAISING */
203 /* ***************** */
204 static void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
206 jclass cls = (*env)->FindClass(env, name);
208 xbt_assert2(cls, "%s (Plus severe error: class %s not found)\n", msg,
211 (*env)->ThrowNew(env, cls, msg);
218 void jxbt_throw_jni(JNIEnv * env, const char *msg)
220 jxbt_throw_by_name(env,
221 "simgrid/msg/JniException",
222 bprintf("Internal or JNI error: %s", msg));
225 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
227 jxbt_throw_by_name(env,
228 "simgrid/msg/JniException",
229 bprintf("Internal error: %s %p not bound", kind,
233 void jxbt_throw_native(JNIEnv * env, char *msg)
235 jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
239 void jxbt_throw_null(JNIEnv * env, char *msg)
241 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
244 /* Errors on user side */
245 void jxbt_throw_illegal(JNIEnv * env, char *msg)
247 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
250 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
252 jxbt_throw_by_name(env,
253 "simgrid/msg/HostNotFoundException",
254 bprintf("No such host: %s", invalid_name));
257 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
259 jxbt_throw_by_name(env,
260 "simgrid/msg/ProcessNotFoundException",
261 bprintf("No such process: %s", invalid_name));
265 void jxbt_throw_transfer_failure(JNIEnv * env, char *details)
268 jxbt_throw_by_name(env, "simgrid/msg/TransferFailureException", details);
272 // host failure Exception
273 void jxbt_throw_host_failure(JNIEnv * env, char *details)
276 jxbt_throw_by_name(env,
277 "simgrid/msg/HostFailureException",
278 bprintf("Host Failure %s", details));
282 // time out failure Exception
283 void jxbt_throw_time_out_failure(JNIEnv * env, char *details)
286 jxbt_throw_by_name(env, "simgrid/msg/TimeoutException", details);
290 // task Cancelled exception
291 void jxbt_throw_task_cancelled(JNIEnv * env, char *details)
294 jxbt_throw_by_name(env, "simgrid/msg/TaskCancelledException", details);