Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Trying to fix another java bug
[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
106   jmethodID id;
107   cls = jxbt_get_class(env, classname);
108
109   if (!cls)
110     return 0;
111
112   id = (*env)->GetStaticMethodID(env, cls, name, signature);
113
114   if (!id) {
115     char *m =
116       bprintf("Cannot find static method %s(%s) in %s", name, signature,
117               classname);
118
119     jxbt_throw_jni(env, m);
120
121     free(m);
122     return 0;
123   }
124   return id;
125 }
126
127 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname,
128                            const char *name, const char *signature)
129 {
130
131   jclass cls;
132
133   jmethodID id;
134   cls = jxbt_get_class(env, classname);
135
136   if (!cls)
137     return 0;
138
139   id = (*env)->GetMethodID(env, cls, name, signature);
140
141   if (!id) {
142     char *m =
143       bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
144
145     jxbt_throw_jni(env, m);
146
147     free(m);
148     return 0;
149   }
150   return id;
151 }
152
153 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls,
154                          const char *name, const char *signature)
155 {
156   jfieldID id;
157
158   if (!cls)
159     return 0;
160
161   id = (*env)->GetFieldID(env, cls, name, signature);
162
163   if (!id) {
164     jmethodID getname_id =
165       (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
166     jstring jclassname =
167       (jstring) (*env)->CallObjectMethod(env, cls, getname_id, NULL);
168     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
169     char *m =
170       bprintf("Cannot find field %s %s in %s", signature, name, classname);
171
172     (*env)->ReleaseStringUTFChars(env, jclassname, classname);
173
174     jxbt_throw_jni(env, m);
175
176     free(m);
177     return 0;
178   }
179
180   return id;
181 }
182
183 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname,
184                          const char *name, const char *signature)
185 {
186   jclass cls = jxbt_get_class(env, classname);
187   jfieldID id;
188
189   if (!cls)
190     return 0;
191
192   id = (*env)->GetFieldID(env, cls, name, signature);
193
194   if (!id) {
195     char *m =
196       bprintf("Cannot find field %s %s in %s", signature, name, classname);
197
198     jxbt_throw_jni(env, m);
199
200     free(m);
201     return 0;
202   }
203
204   return id;
205 }
206
207 /* ***************** */
208 /* EXCEPTION RAISING */
209 /* ***************** */
210 static void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
211 {
212   jclass cls = (*env)->FindClass(env, name);
213
214   xbt_assert2(cls, "%s (Plus severe error: class %s not found)\n", msg, name);
215
216   (*env)->ThrowNew(env, cls, msg);
217
218   free(msg);
219 }
220
221
222 /* Errors in MSG */
223 void jxbt_throw_jni(JNIEnv * env, const char *msg)
224 {
225   jxbt_throw_by_name(env,
226                      "simgrid/msg/JniException",
227                      bprintf("Internal or JNI error: %s", msg));
228 }
229
230 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
231 {
232   jxbt_throw_by_name(env,
233                      "simgrid/msg/JniException",
234                      bprintf("Internal error: %s %p not bound", kind,
235                              pointer));
236 }
237
238 void jxbt_throw_native(JNIEnv * env, char *msg)
239 {
240   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
241 }
242
243 /* *** */
244 void jxbt_throw_null(JNIEnv * env, char *msg)
245 {
246   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
247 }
248
249
250 /* Errors on user side */
251 void jxbt_throw_illegal(JNIEnv * env, char *msg)
252 {
253   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
254 }
255
256 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
257 {
258   jxbt_throw_by_name(env,
259                      "simgrid/msg/HostNotFoundException",
260                      bprintf("No such host: %s", invalid_name));
261 }
262
263 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
264 {
265   jxbt_throw_by_name(env,
266                      "simgrid/msg/ProcessNotFoundException",
267                      bprintf("No such process: %s", invalid_name));
268 }