Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bad cast.
[simgrid.git] / src / bindings / java / jxbt_utilities.cpp
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "jxbt_utilities.hpp"
9 #include "xbt/string.hpp"
10 #include "xbt/sysdep.h"
11
12 #include <cstdlib> /* abort */
13
14 jclass jxbt_get_class(JNIEnv * env, const char *name)
15 {
16   jclass cls = env->FindClass(name);
17
18   if (not cls) {
19     jxbt_throw_jni(env, std::string("Class ") + name + " not found");
20     return nullptr;
21   }
22
23   return cls;
24 }
25
26 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
27 {
28   jmethodID id;
29
30   if (not cls)
31     return 0;
32   id = env->GetMethodID(cls, name, signature);
33
34   if (not id) {
35
36     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
37     jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
38     const char *classname = env->GetStringUTFChars(jclassname, 0);
39
40     env->ReleaseStringUTFChars(jclassname, classname);
41
42     jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
43     return 0;
44   }
45
46   return id;
47 }
48
49 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
50 {
51   jmethodID id;
52
53   if (not cls)
54     return 0;
55   id = env->GetStaticMethodID(cls, name, signature);
56
57   if (not id) {
58     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
59     jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr);
60     const char *classname = env->GetStringUTFChars(jclassname, 0);
61
62     env->ReleaseStringUTFChars(jclassname, classname);
63
64     jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
65     return 0;
66   }
67
68   return id;
69 }
70
71 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
72 {
73   jclass cls;
74   jmethodID id;
75   cls = jxbt_get_class(env, classname);
76
77   if (not cls)
78     return 0;
79
80   id = env->GetStaticMethodID(cls, name, signature);
81
82   if (not id) {
83     jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
84     return 0;
85   }
86   return id;
87 }
88
89 jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
90 {
91   jclass cls;
92   jmethodID id;
93   cls = jxbt_get_class(env, classname);
94
95   if (not cls)
96     return 0;
97
98   id = env->GetMethodID(cls, name, signature);
99
100   if (not id) {
101     jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
102     return 0;
103   }
104   return id;
105 }
106
107 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
108 {
109   jfieldID id;
110
111   if (not cls)
112     return 0;
113
114   id = env->GetFieldID(cls, name, signature);
115
116   if (not id) {
117     jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
118     jstring jclassname = (jstring) env->CallObjectMethod(cls, getname_id, nullptr);
119     const char *classname = env->GetStringUTFChars(jclassname, 0);
120
121     env->ReleaseStringUTFChars(jclassname, classname);
122
123     jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
124
125     return 0;
126   }
127
128   return id;
129 }
130
131 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
132 {
133   jclass cls = jxbt_get_class(env, classname);
134   jfieldID id;
135
136   if (not cls)
137     return 0;
138
139   id = env->GetFieldID(cls, name, signature);
140
141   if (not id) {
142     jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
143     return 0;
144   }
145
146   return id;
147 }
148
149 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg)
150 {
151   jclass cls = env->FindClass(name);
152
153   xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
154
155   env->ThrowNew(cls, msg.c_str());
156 }
157
158 void jxbt_throw_jni(JNIEnv* env, const std::string& msg)
159 {
160   jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
161 }
162
163 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer)
164 {
165   jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
166                      simgrid::xbt::string_printf("Internal error: %s %p not bound", kind.c_str(), pointer));
167 }
168
169 void jxbt_throw_null(JNIEnv* env, const std::string& msg)
170 {
171   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
172 }
173
174 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg)
175 {
176   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
177 }
178
179 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
180 {
181   jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
182 }
183
184 void jxbt_throw_storage_not_found(JNIEnv* env, const std::string& invalid_name)
185 {
186   jxbt_throw_by_name(env, "org/simgrid/msg/StorageNotFoundException", "No such storage: " + invalid_name);
187 }
188
189 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
190 {
191   jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
192 }
193
194 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
195 {
196   jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
197 }
198
199 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
200 {
201   jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure " + details);
202 }
203
204 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)
205 {
206   jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
207 }
208
209 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details)
210 {
211   jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);
212 }