Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix compilation problem with tracing
[simgrid.git] / src / jxbt_utilities.c
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <stdlib.h>             /* abort */
10 #include "xbt/misc.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/str.h"
13 #include "jxbt_utilities.h"
14
15 /* *********** */
16 /* JNI GETTERS */
17 /* *********** */
18
19 jclass jxbt_get_class(JNIEnv * env, const char *name)
20 {
21   jclass cls = (*env)->FindClass(env, name);
22
23   if (!cls) {
24     char *m = bprintf("Class %s not found", name);
25     jxbt_throw_jni(env, m);
26     free(m);
27     return NULL;
28   }
29
30   return cls;
31 }
32
33 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls,
34                            const char *name, const char *signature)
35 {
36   jmethodID id;
37
38   if (!cls)
39     return 0;
40   id = (*env)->GetMethodID(env, cls, name, signature);
41
42   if (!id) {
43
44     jmethodID tostr_id =
45         (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
46     jstring jclassname =
47         (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
48     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
49
50     char *m = bprintf("Cannot find method %s(%s) in %s", name, signature,
51                       classname);
52
53     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
54
55     jxbt_throw_jni(env, m);
56
57     free(m);
58     return 0;
59   }
60
61   return id;
62 }
63
64 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls,
65                                   const char *name, const char *signature)
66 {
67   jmethodID id;
68
69   if (!cls)
70     return 0;
71   id = (*env)->GetStaticMethodID(env, cls, name, signature);
72
73   if (!id) {
74
75     jmethodID tostr_id =
76         (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
77     jstring jclassname =
78         (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
79     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
80
81     char *m =
82         bprintf("Cannot find static method %s(%s) in %s", name, signature,
83                 classname);
84
85     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
86
87     jxbt_throw_jni(env, m);
88
89     free(m);
90     return 0;
91   }
92
93   return id;
94 }
95
96 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname,
97                                   const char *name, const char *signature)
98 {
99
100   jclass cls;
101   jmethodID id;
102   cls = jxbt_get_class(env, classname);
103
104   if (!cls)
105     return 0;
106
107   id = (*env)->GetStaticMethodID(env, cls, name, signature);
108
109   if (!id) {
110     char *m =
111         bprintf("Cannot find static method %s(%s) in %s", name, signature,
112                 classname);
113
114     jxbt_throw_jni(env, m);
115
116     free(m);
117     return 0;
118   }
119   return id;
120 }
121
122 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname,
123                            const char *name, const char *signature)
124 {
125
126   jclass cls;
127   jmethodID id;
128   cls = jxbt_get_class(env, classname);
129
130   if (!cls)
131     return 0;
132
133   id = (*env)->GetMethodID(env, cls, name, signature);
134
135   if (!id) {
136     char *m = bprintf("Cannot find method %s(%s) in %s", name, signature,
137                       classname);
138
139     jxbt_throw_jni(env, m);
140
141     free(m);
142     return 0;
143   }
144   return id;
145 }
146
147 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
148                          const char *name, const char *signature)
149 {
150   jfieldID id;
151
152   if (!cls)
153     return 0;
154
155   id = (*env)->GetFieldID(env, cls, name, signature);
156
157   if (!id) {
158     jmethodID getname_id =
159         (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
160     jstring jclassname =
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,
164                       classname);
165
166     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
167
168     jxbt_throw_jni(env, m);
169
170     free(m);
171     return 0;
172   }
173
174   return id;
175 }
176
177 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname,
178                          const char *name, const char *signature)
179 {
180   jclass cls = jxbt_get_class(env, classname);
181   jfieldID id;
182
183   if (!cls)
184     return 0;
185
186   id = (*env)->GetFieldID(env, cls, name, signature);
187
188   if (!id) {
189     char *m = bprintf("Cannot find field %s %s in %s", signature, name,
190                       classname);
191
192     jxbt_throw_jni(env, m);
193
194     free(m);
195     return 0;
196   }
197
198   return id;
199 }
200
201 /* ***************** */
202 /* EXCEPTION RAISING */
203 /* ***************** */
204 void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
205 {
206   jclass cls = (*env)->FindClass(env, name);
207
208   xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg,
209               name);
210
211   (*env)->ThrowNew(env, cls, msg);
212
213   free(msg);
214 }
215
216
217 /* Errors in MSG */
218 void jxbt_throw_jni(JNIEnv * env, const char *msg)
219 {
220   jxbt_throw_by_name(env,
221                      "org/simgrid/msg/JniException",
222                      bprintf("Internal or JNI error: %s", msg));
223 }
224
225 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
226 {
227   jxbt_throw_by_name(env,
228                      "org/simgrid/msg/JniException",
229                      bprintf("Internal error: %s %p not bound", kind,
230                              pointer));
231 }
232
233 void jxbt_throw_native(JNIEnv * env, char *msg)
234 {
235   jxbt_throw_by_name(env, "org/simgrid/msg/NativeException", msg);
236 }
237
238 /* *** */
239 void jxbt_throw_null(JNIEnv * env, char *msg)
240 {
241   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
242 }
243
244 /* Errors on user side */
245 void jxbt_throw_illegal(JNIEnv * env, char *msg)
246 {
247   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
248 }
249
250 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
251 {
252   jxbt_throw_by_name(env,
253                      "org/simgrid/msg/HostNotFoundException",
254                      bprintf("No such host: %s", invalid_name));
255 }
256
257 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
258 {
259   jxbt_throw_by_name(env,
260                      "org/simgrid/msg/ProcessNotFoundException",
261                      bprintf("No such process: %s", invalid_name));
262 }
263
264 // tranfert failure
265 void jxbt_throw_transfer_failure(JNIEnv * env, char *details)
266 {
267
268   jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
269
270 }
271
272 // host failure Exception
273 void jxbt_throw_host_failure(JNIEnv * env, char *details)
274 {
275
276   jxbt_throw_by_name(env,
277                      "org/simgrid/msg/HostFailureException",
278                      bprintf("Host Failure %s", details));
279
280 }
281
282 // time out failure Exception
283 void jxbt_throw_time_out_failure(JNIEnv * env, char *details)
284 {
285
286   jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
287
288 }
289
290 // task Cancelled exception
291 void jxbt_throw_task_cancelled(JNIEnv * env, char *details)
292 {
293
294   jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);
295
296 }