Logo AND Algorithmique Numérique Distribuée

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