Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c1e5b4a5ac828a2cc1d67d9ecb35ba5437a204c8
[simgrid.git] / src / bindings / java / jxbt_utilities.cpp
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007-2014. 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 SG_BEGIN_DECL()
16
17 jclass jxbt_get_class(JNIEnv * env, const char *name)
18 {
19   jclass cls = env->FindClass(name);
20
21   if (!cls) {
22     char *m = bprintf("Class %s not found", name);
23     jxbt_throw_jni(env, m);
24     free(m);
25     return nullptr;
26   }
27
28   return cls;
29 }
30
31 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
32 {
33   jmethodID id;
34
35   if (!cls)
36     return 0;
37   id = env->GetMethodID(cls, name, signature);
38
39   if (!id) {
40
41     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
42     jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
43     const char *classname = env->GetStringUTFChars(jclassname, 0);
44
45     char *m = bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
46
47     env->ReleaseStringUTFChars(jclassname, classname);
48
49     jxbt_throw_jni(env, m);
50
51     free(m);
52     return 0;
53   }
54
55   return id;
56 }
57
58 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
59 {
60   jmethodID id;
61
62   if (!cls)
63     return 0;
64   id = env->GetStaticMethodID(cls, name, signature);
65
66   if (!id) {
67     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
68     jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
69     const char *classname = env->GetStringUTFChars(jclassname, 0);
70
71     char *m = bprintf("Cannot find static method %s(%s) in %s", name, signature, classname);
72
73     env->ReleaseStringUTFChars(jclassname, classname);
74
75     jxbt_throw_jni(env, m);
76
77     free(m);
78     return 0;
79   }
80
81   return id;
82 }
83
84 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
85 {
86   jclass cls;
87   jmethodID id;
88   cls = jxbt_get_class(env, classname);
89
90   if (!cls)
91     return 0;
92
93   id = env->GetStaticMethodID(cls, name, signature);
94
95   if (!id) {
96     char *m = bprintf("Cannot find static method %s(%s) in %s", name, signature, classname);
97
98     jxbt_throw_jni(env, m);
99
100     free(m);
101     return 0;
102   }
103   return id;
104 }
105
106 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
107 {
108   jclass cls;
109   jmethodID id;
110   cls = jxbt_get_class(env, classname);
111
112   if (!cls)
113     return 0;
114
115   id = env->GetMethodID(cls, name, signature);
116
117   if (!id) {
118     char *m = bprintf("Cannot find method %s(%s) in %s", name, signature, classname);
119
120     jxbt_throw_jni(env, m);
121
122     free(m);
123     return 0;
124   }
125   return id;
126 }
127
128 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
129 {
130   jfieldID id;
131
132   if (!cls)
133     return 0;
134
135   id = env->GetFieldID(cls, name, signature);
136
137   if (!id) {
138     jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
139     jstring jclassname = (jstring) env->CallObjectMethod(cls, getname_id, nullptr);
140     const char *classname = env->GetStringUTFChars(jclassname, 0);
141     char *m = bprintf("Cannot find field %s %s in %s", signature, name, classname);
142
143     env->ReleaseStringUTFChars(jclassname, classname);
144
145     jxbt_throw_jni(env, m);
146
147     free(m);
148     return 0;
149   }
150
151   return id;
152 }
153
154 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
155 {
156   jclass cls = jxbt_get_class(env, classname);
157   jfieldID id;
158
159   if (!cls)
160     return 0;
161
162   id = env->GetFieldID(cls, name, signature);
163
164   if (!id) {
165     char *m = bprintf("Cannot find field %s %s in %s", signature, name, classname);
166
167     jxbt_throw_jni(env, m);
168
169     free(m);
170     return 0;
171   }
172
173   return id;
174 }
175
176 void jxbt_throw_by_name(JNIEnv * env, const char *name, char *msg)
177 {
178   jclass cls = env->FindClass(name);
179
180   xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg, name);
181
182   env->ThrowNew(cls, msg);
183
184   free(msg);
185 }
186
187 void jxbt_throw_jni(JNIEnv * env, const char *msg)
188 {
189   jxbt_throw_by_name(env, "org/simgrid/msg/JniException", bprintf("Internal or JNI error: %s", msg));
190 }
191
192 void jxbt_throw_notbound(JNIEnv * env, const char *kind, void *pointer)
193 {
194   jxbt_throw_by_name(env, "org/simgrid/msg/JniException", bprintf("Internal error: %s %p not bound", kind, pointer));
195 }
196
197 void jxbt_throw_null(JNIEnv * env, char *msg)
198 {
199   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
200 }
201
202 void jxbt_throw_illegal(JNIEnv * env, char *msg)
203 {
204   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
205 }
206
207 void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name)
208 {
209   jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", bprintf("No such host: %s", invalid_name));
210 }
211
212 void jxbt_throw_storage_not_found(JNIEnv * env, const char *invalid_name)
213 {
214   jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", bprintf("No such storage: %s", invalid_name));
215 }
216
217 void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name)
218 {
219   jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", bprintf("No such process: %s", invalid_name));
220 }
221
222 void jxbt_throw_transfer_failure(JNIEnv * env, char *details)
223 {
224   jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
225 }
226
227 void jxbt_throw_host_failure(JNIEnv * env, char *details)
228 {
229   jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", bprintf("Host Failure %s", details));
230 }
231
232 void jxbt_throw_time_out_failure(JNIEnv * env, char *details)
233 {
234   jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
235 }
236
237 void jxbt_throw_task_cancelled(JNIEnv * env, char *details)
238 {
239   jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);
240 }
241
242 SG_END_DECL()