Logo AND Algorithmique Numérique Distribuée

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