Logo AND Algorithmique Numérique Distribuée

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