Logo AND Algorithmique Numérique Distribuée

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