Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
java: setup a RAII wrapper to properly deal with GetStringUTFChars/ReleaseStringUTFChar
[simgrid.git] / src / bindings / java / jxbt_utilities.cpp
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007-2021. 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 jclass jxbt_get_class(JNIEnv * env, const char *name)
13 {
14   jclass cls = env->FindClass(name);
15
16   if (not cls) {
17     jxbt_throw_jni(env, std::string("Class ") + name + " not found");
18     return nullptr;
19   }
20
21   return cls;
22 }
23
24 jmethodID jxbt_get_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
25 {
26   jmethodID id;
27
28   if (not cls)
29     return nullptr;
30   id = env->GetMethodID(cls, name, signature);
31
32   if (not id) {
33     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
34     auto jclassname    = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
35     jstring_wrapper classname(env, jclassname);
36     auto msg = std::string("Cannot find method") + name + "(" + signature + ") in " + classname.value;
37
38     jxbt_throw_jni(env, msg);
39     return nullptr;
40   }
41
42   return id;
43 }
44
45 jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature)
46 {
47   jmethodID id;
48
49   if (not cls)
50     return nullptr;
51   id = env->GetStaticMethodID(cls, name, signature);
52
53   if (not id) {
54     jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
55     auto jclassname       = (jstring)env->CallObjectMethod(cls, tostr_id, nullptr);
56     jstring_wrapper classname(env, jclassname);
57     auto msg = std::string("Cannot find static method") + name + "(" + signature + ") in " + classname.value;
58
59     jxbt_throw_jni(env, msg);
60     return nullptr;
61   }
62
63   return id;
64 }
65
66 jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature)
67 {
68   jclass cls;
69   jmethodID id;
70   cls = jxbt_get_class(env, classname);
71
72   if (not cls)
73     return nullptr;
74
75   id = env->GetStaticMethodID(cls, name, signature);
76
77   if (not id) {
78     jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname);
79     return nullptr;
80   }
81   return id;
82 }
83
84 jmethodID jxbt_get_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 (not cls)
91     return nullptr;
92
93   id = env->GetMethodID(cls, name, signature);
94
95   if (not id) {
96     jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname);
97     return nullptr;
98   }
99   return id;
100 }
101
102 jfieldID jxbt_get_jfield(JNIEnv * env, jclass cls, const char *name, const char *signature)
103 {
104   jfieldID id;
105
106   if (not cls)
107     return nullptr;
108
109   id = env->GetFieldID(cls, name, signature);
110
111   if (not id) {
112     jmethodID getname_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
113     auto jclassname       = (jstring)env->CallObjectMethod(cls, getname_id, nullptr);
114     const char* classname = env->GetStringUTFChars(jclassname, nullptr);
115
116     env->ReleaseStringUTFChars(jclassname, classname);
117
118     jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
119
120     return nullptr;
121   }
122
123   return id;
124 }
125
126 jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature)
127 {
128   jclass cls = jxbt_get_class(env, classname);
129   jfieldID id;
130
131   if (not cls)
132     return nullptr;
133
134   id = env->GetFieldID(cls, name, signature);
135
136   if (not id) {
137     jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname);
138     return nullptr;
139   }
140
141   return id;
142 }
143
144 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg)
145 {
146   jclass cls = env->FindClass(name);
147
148   xbt_assert(cls, "%s (Plus severe error: class %s not found)\n", msg.c_str(), name);
149
150   env->ThrowNew(cls, msg.c_str());
151 }
152
153 void jxbt_throw_jni(JNIEnv* env, const std::string& msg)
154 {
155   jxbt_throw_by_name(env, "org/simgrid/msg/JniException", "Internal or JNI error: " + msg);
156 }
157
158 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer)
159 {
160   jxbt_throw_by_name(env, "org/simgrid/msg/JniException",
161                      simgrid::xbt::string_printf("Internal error: %s %p not bound", kind.c_str(), pointer));
162 }
163
164 void jxbt_throw_null(JNIEnv* env, const std::string& msg)
165 {
166   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
167 }
168
169 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg)
170 {
171   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
172 }
173
174 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name)
175 {
176   jxbt_throw_by_name(env, "org/simgrid/msg/HostNotFoundException", "No such host: " + invalid_name);
177 }
178
179 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name)
180 {
181   jxbt_throw_by_name(env, "org/simgrid/msg/ProcessNotFoundException", "No such process: " + invalid_name);
182 }
183
184 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& details)
185 {
186   jxbt_throw_by_name(env, "org/simgrid/msg/TransferFailureException", details);
187 }
188
189 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details)
190 {
191   jxbt_throw_by_name(env, "org/simgrid/msg/HostFailureException", "Host Failure" + details);
192 }
193
194 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details)
195 {
196   jxbt_throw_by_name(env, "org/simgrid/msg/TimeoutException", details);
197 }
198
199 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details)
200 {
201   jxbt_throw_by_name(env, "org/simgrid/msg/TaskCancelledException", details);
202 }