Logo AND Algorithmique Numérique Distribuée

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