-
Notifications
You must be signed in to change notification settings - Fork 13
/
JniJavaObject.cpp
107 lines (85 loc) · 2.08 KB
/
JniJavaObject.cpp
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
#include <JniJavaObjectClassDef.h>
#include <JniJavaClass.h>
namespace JNI {
#ifdef MSYM_DEBUG_JNI_REF_CNT
int globalRefCnt = 0;
#endif
JavaObject::JavaObject() : JavaObject(JavaEnv(), nullptr) {}
JavaObject::JavaObject(JavaEnv env, jobject o) : _env(env), _obj(o) {
#ifdef MSYM_DEBUG_JNI_REF_CNT
if(Valid())
globalRefCnt++;
#endif
}
JavaObject::JavaObject(const JavaObjectIniter& o) : JavaObject(o.env, o.obj) {}
JavaObject::JavaObject(const JavaObject& o) {
Init(o);
}
JavaObject::JavaObject(JavaObject&& o) : _env(o._env), _obj(o._obj) {
o._env = JavaEnv();
o._obj = nullptr;
}
JavaObject& JavaObject::operator=(const JavaObject& o) {
Reset(o);
return *this;
}
JavaObject& JavaObject::operator=(JavaObject&& o) {
std::swap(_env, o._env);
std::swap(_obj, o._obj);
return *this;
}
JavaObject::~JavaObject() {
Destroy();
}
bool JavaObject::operator==(const JavaObject& obj) const {
return _env.Val()->IsSameObject(Val(), obj.Val());
}
bool JavaObject::operator!=(const JavaObject& obj) const {
return !(*this == obj);
}
JavaClass JavaObject::GetClass() const {
return JavaClass(_env, _env.Val()->GetObjectClass(Val()));
}
JavaObject JavaObject::New(const JavaClass& cls) {
return cls.NewObject(); // JavaObject(cls._env, cls._env.Val()->AllocObject(cls.Val()));
}
JavaObject JavaObject::New(JavaEnv env, const std::string& cls) {
return New(env.FindClass(cls));
}
bool JavaObject::Valid() const {
return _env.Valid() && _obj != nullptr;
}
JavaEnv JavaObject::Env() const {
return _env;
}
jobject JavaObject::Val() const {
return _obj;
}
jint JavaObject::MonitorEnter() const {
return _env.Val()->MonitorEnter(Val());
}
jint JavaObject::MonitorExit() const {
return _env.Val()->MonitorExit(Val());
}
void JavaObject::Reset(const JavaObject& o) {
Destroy();
Init(o);
}
void JavaObject::Init(const JavaObject& o) {
_env = o._env;
if(o.Valid()) {
#ifdef MSYM_DEBUG_JNI_REF_CNT
globalRefCnt++;
#endif
_obj = _env.Val()->NewLocalRef(o._obj);
}
}
void JavaObject::Destroy() {
if(Valid()) {
#ifdef MSYM_DEBUG_JNI_REF_CNT
globalRefCnt--;
#endif
_env.Val()->DeleteLocalRef(_obj);
}
}
}