Logo AND Algorithmique Numérique Distribuée

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