Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new line at the end of the file, to avoid a gcc warning (turned into error by...
[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   jclass cls = (*env)->FindClass(env, name);
25         
26   if(!cls) {
27     char *m=bprintf("Class %s not found",name);
28     jxbt_throw_jni(env, m);
29     free(m);
30     return NULL;
31   }
32
33   return cls;
34 }
35
36 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls, 
37                            const char *name,const char *signature) {
38   jmethodID id;
39
40   if (!cls)
41     return 0;
42   id = (*env)->GetMethodID(env, cls, name,signature);
43         
44   if(!id) {
45
46     jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
47     jstring jclassname = (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 ,classname);
51
52     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
53
54     jxbt_throw_jni(env,m);
55
56     free(m);
57     return 0;
58   }
59
60   return id;
61 }
62
63 jmethodID jxbt_get_static_jmethod(JNIEnv* env, jclass cls, 
64                            const char *name,const char *signature) {
65   jmethodID id;
66
67   if (!cls)
68     return 0;
69   id = (*env)->GetStaticMethodID(env, cls, name,signature);
70         
71   if(!id) {
72
73     jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
74     jstring jclassname = (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
75     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
76
77     char *m=bprintf("Cannot find static method %s(%s) in %s", name, signature ,classname);
78
79     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
80
81     jxbt_throw_jni(env,m);
82
83     free(m);
84     return 0;
85   }
86
87   return id;
88 }
89
90 jmethodID jxbt_get_static_smethod(JNIEnv* env, const char *classname, 
91                           const char *name,const char *signature) { 
92   
93         jclass cls;
94
95         jmethodID id;
96         cls = jxbt_get_class(env,classname);
97
98   if (!cls)
99     return 0;
100
101   id = (*env)->GetStaticMethodID(env, cls, name,signature);
102         
103   if(!id) {
104     char *m=bprintf("Cannot find static method %s(%s) in %s", name, signature,classname);
105
106     jxbt_throw_jni(env,m);
107
108     free(m);
109     return 0;
110   }
111   return id;
112 }
113
114 jmethodID jxbt_get_smethod(JNIEnv* env, const char *classname, 
115                           const char *name,const char *signature) { 
116   
117         jclass cls;
118
119         jmethodID id;
120         cls = jxbt_get_class(env,classname);
121
122   if (!cls)
123     return 0;
124
125   id = (*env)->GetMethodID(env, cls, name,signature);
126         
127   if(!id) {
128     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature,classname);
129
130     jxbt_throw_jni(env,m);
131
132     free(m);
133     return 0;
134   }
135   return id;
136 }
137
138 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls, 
139                          const char *name, const char *signature) {
140   jfieldID id;
141
142   if (!cls)
143     return 0;
144
145   id = (*env)->GetFieldID(env, cls, name, signature);
146         
147   if(!id) {
148     jmethodID getname_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
149     jstring jclassname = (jstring) (*env)->CallObjectMethod(env,cls, getname_id, NULL);
150     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
151     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
152
153     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
154
155     jxbt_throw_jni(env,m);
156
157     free(m);
158     return 0;
159   }
160
161   return id;
162 }
163
164 jfieldID jxbt_get_sfield(JNIEnv* env, const char *classname, 
165                         const char *name, const char *signature) {
166   jclass cls = jxbt_get_class(env,classname);
167   jfieldID id;
168
169   if (!cls)
170     return 0;
171
172   id = (*env)->GetFieldID(env, cls, name, signature);
173         
174   if(!id) {
175     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
176
177     jxbt_throw_jni(env,m);
178
179     free(m);
180     return 0;
181   }
182
183   return id;
184 }
185
186 /* ***************** */
187 /* EXCEPTION RAISING */
188 /* ***************** */
189 static void jxbt_throw_by_name(JNIEnv* env,const char* name, char *msg) {
190    jclass cls = (*env)->FindClass(env, name);
191
192    xbt_assert2(cls,"%s (Plus severe error: class %s not found)\n",
193                msg,name);
194
195    (*env)->ThrowNew(env,cls,msg);
196
197    free(msg);
198 }
199
200
201 /* Errors in MSG */
202 void jxbt_throw_jni(JNIEnv* env,const char* msg) {
203   jxbt_throw_by_name(env,
204                      "simgrid/msg/JniException",
205                      bprintf("Internal or JNI error: %s",msg));
206 }
207 void jxbt_throw_notbound(JNIEnv* env,const char* kind, void *pointer) {
208   jxbt_throw_by_name(env,
209                      "simgrid/msg/JniException",
210                      bprintf("Internal error: %s %p not bound",kind, pointer));
211 }
212
213 void jxbt_throw_native(JNIEnv* env,char* msg) {
214   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
215 }
216
217 /* *** */
218 void jxbt_throw_null(JNIEnv* env, char* msg) {
219   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
220 }
221
222
223 /* Errors on user side */
224 void jxbt_throw_illegal(JNIEnv* env, char* msg) {
225   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
226 }
227 void jxbt_throw_host_not_found(JNIEnv* env, const char *invalid_name) {
228   jxbt_throw_by_name(env,
229                      "simgrid/msg/HostNotFoundException",
230                      bprintf("No such host: %s",invalid_name));
231 }
232 void jxbt_throw_process_not_found(JNIEnv* env, const char *invalid_name) {
233   jxbt_throw_by_name(env,
234                      "simgrid/msg/ProcessNotFoundException",
235                      bprintf("No such process: %s",invalid_name));
236 }