JniHelper.h
15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __ANDROID_JNI_HELPER_H__
#define __ANDROID_JNI_HELPER_H__
#include <jni.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
#include "platform/CCPlatformMacros.h"
#include "math/Vec3.h"
NS_CC_BEGIN
typedef struct JniMethodInfo_
{
JNIEnv * env;
jclass classID;
jmethodID methodID;
} JniMethodInfo;
class CC_DLL JniHelper
{
public:
typedef std::unordered_map<JNIEnv*, std::vector<jobject>> LocalRefMapType;
static void setJavaVM(JavaVM *javaVM);
static JavaVM* getJavaVM();
static JNIEnv* getEnv();
static jobject getActivity();
static bool setClassLoaderFrom(jobject activityInstance);
static bool getStaticMethodInfo(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode);
static bool getMethodInfo(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode);
static std::string jstring2string(jstring str);
static jmethodID loadclassMethod_methodID;
static jobject classloader;
static std::function<void()> classloaderCallback;
/**
@brief Call of Java static void method
@if no such method will log error
*/
template <typename... Ts>
static void callStaticVoidMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
t.env->CallStaticVoidMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
}
/**
@brief Call of Java static boolean method
@return value from Java static boolean method if there are proper JniMethodInfo; otherwise false.
*/
template <typename... Ts>
static bool callStaticBooleanMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jboolean jret = JNI_FALSE;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Z";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return (jret == JNI_TRUE);
}
/**
@brief Call of Java static int method
@return value from Java static int method if there are proper JniMethodInfo; otherwise 0.
*/
template <typename... Ts>
static int callStaticIntMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jint ret = 0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")I";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return ret;
}
/**
@brief Call of Java static float method
@return value from Java static float method if there are proper JniMethodInfo; otherwise 0.
*/
template <typename... Ts>
static float callStaticFloatMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jfloat ret = 0.0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")F";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return ret;
}
/**
@brief Call of Java static float* method
@return address of JniMethodInfo if there are proper JniMethodInfo; otherwise nullptr.
*/
template <typename... Ts>
static float* callStaticFloatArrayMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
static float ret[32];
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
jsize len = t.env->GetArrayLength(array);
if (len <= 32) {
jfloat* elems = t.env->GetFloatArrayElements(array, 0);
if (elems) {
memcpy(ret, elems, sizeof(float) * len);
t.env->ReleaseFloatArrayElements(array, elems, 0);
};
}
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
return &ret[0];
} else {
reportError(className, methodName, signature);
}
return nullptr;
}
/**
@brief Call of Java static int* method
@return address of JniMethodInfo if there are proper JniMethodInfo; otherwise nullptr.
*/
template <typename... Ts>
static int* callStaticIntArrayMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
static int ret[32];
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[I";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jintArray array = (jintArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
jsize len = t.env->GetArrayLength(array);
if (len <= 32) {
jint* elems = t.env->GetIntArrayElements(array, 0);
if (elems) {
memcpy(ret, elems, sizeof(int) * len);
t.env->ReleaseIntArrayElements(array, elems, 0);
};
}
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
return &ret[0];
} else {
reportError(className, methodName, signature);
}
return nullptr;
}
/**
@brief Call of Java static Vec3 method
@return JniMethodInfo of Vec3 type if there are proper JniMethodInfo; otherwise Vec3(0, 0, 0).
*/
template <typename... Ts>
static Vec3 callStaticVec3Method(const std::string& className,
const std::string& methodName,
Ts... xs) {
Vec3 ret;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
jsize len = t.env->GetArrayLength(array);
if (len == 3) {
jfloat* elems = t.env->GetFloatArrayElements(array, 0);
ret.x = elems[0];
ret.y = elems[1];
ret.z = elems[2];
t.env->ReleaseFloatArrayElements(array, elems, 0);
}
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return ret;
}
/**
@brief Call of Java static double method
@return value from Java static double method if there are proper JniMethodInfo; otherwise 0.
*/
template <typename... Ts>
static double callStaticDoubleMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jdouble ret = 0.0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")D";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return ret;
}
/**
@brief Call of Java static string method
@return JniMethodInfo of string type if there are proper JniMethodInfo; otherwise empty string.
*/
template <typename... Ts>
static std::string callStaticStringMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
std::string ret;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Ljava/lang/String;";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
LocalRefMapType localRefs;
jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
ret = cocos2d::JniHelper::jstring2string(jret);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(jret);
deleteLocalRefs(t.env, localRefs);
} else {
reportError(className, methodName, signature);
}
return ret;
}
private:
static JNIEnv* cacheEnv(JavaVM* jvm);
static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode);
static JavaVM* _psJavaVM;
static jobject _activity;
static jstring convert(LocalRefMapType& localRefs, cocos2d::JniMethodInfo& t, const char* x);
static jstring convert(LocalRefMapType& localRefs, cocos2d::JniMethodInfo& t, const std::string& x);
inline static jint convert(LocalRefMapType&, cocos2d::JniMethodInfo&, int32_t value) { return static_cast<jint>(value);}
inline static jlong convert(LocalRefMapType&, cocos2d::JniMethodInfo&, int64_t value) { return static_cast<jlong>(value);}
inline static jfloat convert(LocalRefMapType&, cocos2d::JniMethodInfo&, float value) { return static_cast<jfloat>(value);}
inline static jdouble convert(LocalRefMapType&, cocos2d::JniMethodInfo&, double value) { return static_cast<jdouble>(value);}
inline static jboolean convert(LocalRefMapType&, cocos2d::JniMethodInfo&, bool value) { return static_cast<jboolean>(value);}
inline static jbyte convert(LocalRefMapType&, cocos2d::JniMethodInfo&, int8_t value) { return static_cast<jbyte>(value);}
inline static jchar convert(LocalRefMapType&, cocos2d::JniMethodInfo&, uint8_t value) { return static_cast<jchar>(value);}
inline static jshort convert(LocalRefMapType&, cocos2d::JniMethodInfo&, int16_t value) { return static_cast<jshort>(value);}
template <typename T>
static T convert(LocalRefMapType& localRefs, cocos2d::JniMethodInfo&, T x) {
return x;
}
static void deleteLocalRefs(JNIEnv* env, LocalRefMapType& localRefs);
static std::string getJNISignature() {
return "";
}
static std::string getJNISignature(bool) {
return "Z";
}
static std::string getJNISignature(char) {
return "C";
}
static std::string getJNISignature(short) {
return "S";
}
static std::string getJNISignature(int) {
return "I";
}
static std::string getJNISignature(long) {
return "J";
}
static std::string getJNISignature(float) {
return "F";
}
static std::string getJNISignature(double) {
return "D";
}
static std::string getJNISignature(const char*) {
return "Ljava/lang/String;";
}
static std::string getJNISignature(const std::string&) {
return "Ljava/lang/String;";
}
template <typename T>
static std::string getJNISignature(T x) {
// This template should never be instantiated
static_assert(sizeof(x) == 0, "Unsupported argument type");
return "";
}
template <typename T, typename... Ts>
static std::string getJNISignature(T x, Ts... xs) {
return getJNISignature(x) + getJNISignature(xs...);
}
static void reportError(const std::string& className, const std::string& methodName, const std::string& signature);
};
NS_CC_END
#endif // __ANDROID_JNI_HELPER_H__