-
Notifications
You must be signed in to change notification settings - Fork 3
/
JniCpp11.h
335 lines (262 loc) · 10.9 KB
/
JniCpp11.h
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
#pragma once
#include <exception>
#include <memory>
#include <sstream>
#include <string>
#include <jni.h>
#define CONCAT(A, B, C) A##B##C
#define JNI_FUNC(JAVA_CLASS, METHOD) JNIEXPORT void JNICALL CONCAT(JAVA_CLASS, _, METHOD)
namespace jnicpp11 {
typedef std::shared_ptr<_jobject> shared_jobject;
typedef std::shared_ptr<_jclass> shared_jclass;
class Jni {
public:
static JNIEnv *getEnv();
static JavaVM *getJvm();
/**
* Please make sure to call setJvm if you are not using cocos2d-x
*
* jint JNI_OnLoad(JavaVM *vm, void *reserved) {
* Jni::setJvm(vm);
* return JNI_VERSION_1_4;
* }
*/
static void setJvm(JavaVM *jvm);
private:
static Jni &get();
JavaVM *_jvm = nullptr;
};
class JniException : public std::exception {
public:
static void checkException(JNIEnv *env) throw(JniException);
JniException(const std::string &message);
~JniException() noexcept override {}
const char *what() const noexcept override;
void log() const noexcept;
private:
std::string _message;
};
class JavaObject;
class JavaClass {
public:
static JavaClass getClass(const std::string &classPath);
virtual ~JavaClass() = default;
jclass getJClass() const;
std::string getTypeSignature() const;
std::string getClassPath() const;
template <typename... Args> JavaObject newObject(Args... args) const;
template <typename ReturnType, typename... Args>
ReturnType staticCall(const std::string &methodName, const ReturnType &defaultValue, Args... args) const;
template <typename... Args> void staticCallVoid(const std::string &methodName, Args... args) const;
template <typename ReturnType> ReturnType staticField(const std::string &fieldName, const ReturnType &defaultValue) const;
operator bool() const;
bool operator==(const std::nullptr_t &null) const;
private:
friend class JavaObject;
JNIEnv *checkAndGetEnv() const throw(JniException);
JavaClass(jclass clazz);
JavaClass(const std::string &classPath);
JavaClass(jclass clazz, const std::string &classPath);
JavaObject _newObject(JNIEnv *env, jmethodID methodId, ...) const;
template <typename ReturnType, typename... Args> ReturnType _staticCall(JNIEnv *env, jmethodID methodId, Args... args) const;
template <typename ReturnType> ReturnType __staticCall(JNIEnv *env, jmethodID methodId, ...) const;
template <typename ReturnType> ReturnType _staticField(JNIEnv *env, jfieldID fieldId) const;
std::string _classPath;
shared_jclass _jclazz;
};
class JavaObject {
public:
JavaObject(jobject obj);
JavaObject(jobject obj, jclass clazz);
JavaObject(jobject obj, const JavaClass &clazz);
JavaObject(jobject obj, const std::string &classPath);
static JavaObject null(const std::string &classPath);
virtual ~JavaObject() = default;
jclass getJClass() const;
jobject getJObject() const;
JavaObject asType(const JavaClass &clazz) const;
virtual std::string getClassPath() const;
virtual std::string getTypeSignature() const;
template <typename ReturnType> ReturnType field(const std::string &fieldName, const ReturnType &defaultValue) const;
template <typename ReturnType, typename... Args>
ReturnType call(const std::string &methodName, const ReturnType &defaultValue, Args... args) const;
template <typename... Args> void callVoid(const std::string &methodName, Args... args) const;
operator bool() const;
bool operator==(const std::nullptr_t &null) const;
protected:
JNIEnv *checkAndGetEnv() const throw(JniException);
template <typename ReturnType> ReturnType _field(JNIEnv *env, jfieldID fieldId) const;
template <typename ReturnType, typename... Args> ReturnType _call(JNIEnv *env, jmethodID methodId, Args... args) const;
template <typename ReturnType> ReturnType __call(JNIEnv *env, jmethodID methodId, ...) const;
shared_jobject _jobject;
JavaClass _javaClass;
};
template <typename T> class JavaArray : public JavaObject {
public:
std::string getTypeSignature() const override;
};
template <> class JavaArray<JavaObject> : public JavaObject {
public:
JavaArray(jobject obj);
JavaArray(jobject obj, const std::string &elementClassPath);
static JavaArray<JavaObject> null(const std::string &elementClassPath);
std::string getTypeSignature() const override;
std::string getElementClassPath() const;
private:
std::string _elementClassPath;
};
#pragma mark - jstring cast methods
std::string fromJString(jstring jstr, const std::string &defaultValue = "", bool deleteLocalRef = false);
std::string fromJString(const JavaObject &jstr, const std::string &defaultValue = "");
JavaObject toJString(const std::string &str);
#pragma mark - makeArg, adaptArg
JavaObject makeArg(const std::string &str);
template <typename T> const T &makeArg(const T &arg) { return arg; }
template <typename T> const T &adaptArg(const T &arg) { return arg; }
jobject adaptArg(const JavaObject &javaObject);
#pragma mark - TypeSignature, MethodSignature
struct TypeSignature {
static inline std::string get(const JavaObject &jobj) { return jobj.getTypeSignature(); }
template <typename T> static inline std::string get(const JavaArray<T> &jarr) { return jarr.getTypeSignature(); }
template <typename T = void> static std::string get();
template <typename T> static std::string get(const T &) { return get<T>(); }
};
struct MethodSignature {
template <typename ReturnType, typename... Ts> static std::string get(const ReturnType &ret, Ts... args) {
return getSigned(TypeSignature::get(ret), std::forward<Ts>(args)...);
}
template <typename... Ts> static std::string getVoid(Ts... args) {
return getSigned(TypeSignature::get(), std::forward<Ts>(args)...);
}
template <typename... Ts> static std::string getSigned(const std::string &returnTypeSignature, Ts... args) {
std::ostringstream os;
os << "(";
build(os, std::forward<Ts>(args)...);
os << ")" << returnTypeSignature;
return os.str();
}
template <typename T, typename... Ts> static void build(std::ostringstream &os, T first, Ts... args) {
os << TypeSignature::get(std::forward<T>(first));
MethodSignature::build(os, std::forward<Ts>(args)...);
}
static void build(std::ostringstream &os) {}
};
#pragma mark - JniEnv utils
namespace env_util {
jclass findClass(JNIEnv *env, const std::string &classPath) throw(JniException);
jmethodID getMethodId(
JNIEnv *env, jclass clazz, const std::string &methodName, const std::string &signature, bool isStatic) throw(JniException);
jfieldID getFieldId(JNIEnv *env, jclass clazz, const std::string &fieldName, const std::string &signature, bool isStatic) throw(
JniException);
}
#pragma mark - JavaClass template methods
template <typename... Args> JavaObject JavaClass::newObject(Args... args) const {
JNIEnv *env = nullptr;
try {
constexpr const char *name = "<init>";
env = checkAndGetEnv();
std::string signature = MethodSignature::getVoid(args...);
jmethodID methodId = env_util::getMethodId(env, getJClass(), name, signature, false);
JavaObject jinstance = _newObject(env, methodId, makeArg(args)...);
JniException::checkException(env);
return jinstance;
} catch (const JniException &e) {
e.log();
}
return nullptr;
}
template <typename ReturnType>
ReturnType JavaClass::staticField(const std::string &fieldName, const ReturnType &defaultValue) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = TypeSignature::get(defaultValue);
jfieldID fieldId = env_util::getFieldId(env, getJClass(), fieldName, signature, true);
auto result = _staticField<ReturnType>(env, fieldId);
JniException::checkException(env);
return result;
} catch (const JniException &e) {
e.log();
}
return defaultValue;
}
template <typename ReturnType, typename... Args>
ReturnType JavaClass::staticCall(const std::string &methodName, const ReturnType &defaultValue, Args... args) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = MethodSignature::get(defaultValue, args...);
jmethodID methodId = env_util::getMethodId(env, getJClass(), methodName, signature, true);
auto result = _staticCall<ReturnType>(env, methodId, makeArg(args)...);
JniException::checkException(env);
return result;
} catch (const JniException &e) {
e.log();
}
return defaultValue;
}
template <typename... Args> void JavaClass::staticCallVoid(const std::string &methodName, Args... args) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = MethodSignature::getVoid(args...);
jmethodID methodId = env_util::getMethodId(env, getJClass(), methodName, signature, true);
_staticCall<void>(env, methodId, makeArg(args)...);
JniException::checkException(env);
} catch (const JniException &e) {
e.log();
}
}
template <typename ReturnType, typename... Args>
ReturnType JavaClass::_staticCall(JNIEnv *env, jmethodID methodId, Args... args) const {
return __staticCall<ReturnType>(env, methodId, adaptArg(args)...);
}
#pragma mark - JavaObject template methods
template <typename ReturnType> ReturnType JavaObject::field(const std::string &fieldName, const ReturnType &defaultValue) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = TypeSignature::get(defaultValue);
jfieldID fieldId = env_util::getFieldId(env, getJClass(), fieldName, signature, false);
auto result = _field<ReturnType>(env, fieldId);
JniException::checkException(env);
return result;
} catch (const JniException &e) {
e.log();
}
return defaultValue;
}
template <typename ReturnType, typename... Args>
ReturnType JavaObject::call(const std::string &methodName, const ReturnType &defaultValue, Args... args) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = MethodSignature::get(defaultValue, args...);
jmethodID methodId = env_util::getMethodId(env, getJClass(), methodName, signature, false);
auto result = _call<ReturnType>(env, methodId, makeArg(args)...);
JniException::checkException(env);
return result;
} catch (const JniException &e) {
e.log();
}
return defaultValue;
}
template <typename... Args> void JavaObject::callVoid(const std::string &methodName, Args... args) const {
JNIEnv *env = nullptr;
try {
env = checkAndGetEnv();
std::string signature = MethodSignature::getVoid(args...);
jmethodID methodId = env_util::getMethodId(env, getJClass(), methodName, signature, false);
_call<void>(env, methodId, makeArg(args)...);
JniException::checkException(env);
} catch (const JniException &e) {
e.log();
}
}
template <typename ReturnType, typename... Args>
ReturnType JavaObject::_call(JNIEnv *env, jmethodID methodId, Args... args) const {
return __call<ReturnType>(env, methodId, adaptArg(args)...);
}
#pragma mark - JavaArray
template <typename T> std::string JavaArray<T>::getTypeSignature() const { return "[" + TypeSignature::get<T>(); }
} // namespace jnicpp11