-
Notifications
You must be signed in to change notification settings - Fork 13
/
JniJavaVM.cpp
122 lines (104 loc) · 2.7 KB
/
JniJavaVM.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <JniJavaVM.h>
#include <JniJavaEnv.h>
namespace JNI {
JniJavaVM::JniJavaVM(JavaVM * v, jint ver) : val(v), version(ver), doDestroy(false) {}
JniJavaVM::JniJavaVM(const JniJavaVM& vm) : val(vm.val), version(vm.version), doDestroy(false) {}
JniJavaVM::JniJavaVM(JniJavaVM&& vm) : val(vm.val), version(vm.version), doDestroy(vm.doDestroy) {
vm.val = nullptr;
vm.version = 0;
vm.doDestroy = false;
}
JniJavaVM::JniJavaVM(jint ver) : val(nullptr), version(ver), doDestroy(true) {
JNIEnv * env;
JavaVMInitArgs args;
args.version = version;
if(JNI_GetDefaultJavaVMInitArgs(&args) != JNI_OK) {
printf("JNI_GetDefaultJavaVMInitArgs failed!\n");
return;
}
if(JNI_CreateJavaVM(&val, (void**) &env, &args) != JNI_OK) {
printf("Error: cannot create JavaVM!\n");
}
}
JniJavaVM::JniJavaVM(const std::string& classPath, jint ver) : val(nullptr), version(ver), doDestroy(true) {
JNIEnv * env;
JavaVMInitArgs args;
args.version = version;
JavaVMOption options[1];
args.nOptions = 1;
std::string argCP = "-Djava.class.path=" + classPath;
options[0].optionString = (char*) argCP.c_str();
args.options = options;
if(JNI_CreateJavaVM(&val, (void**) &env, &args) != JNI_OK) {
printf("Error: cannot create JavaVM!\n");
}
}
JavaVM * JniJavaVM::Val() {
return val;
}
const JavaVM * JniJavaVM::Val() const {
return val;
}
jint JniJavaVM::Version() const {
return version;
}
bool JniJavaVM::Valid() const {
return val != nullptr;
}
bool JniJavaVM::operator==(const JniJavaVM& jvm) const {
return val == jvm.val;
}
bool JniJavaVM::operator!=(const JniJavaVM& jvm) const {
return !(*this == jvm);
}
JniJavaVM::~JniJavaVM() {
Destroy();
}
JniJavaVM& JniJavaVM::operator=(const JniJavaVM& vm) {
Destroy();
val = vm.val;
version = vm.version;
doDestroy = false;
return *this;
}
JniJavaVM& JniJavaVM::operator=(JniJavaVM&& vm) {
std::swap(val, vm.val);
std::swap(version, vm.version);
std::swap(doDestroy, vm.doDestroy);
return *this;
}
JavaEnv JniJavaVM::GetEnv() const {
JNIEnv * env = nullptr;
JavaVMAttachArgs attachArgs;
jint res = val->GetEnv((void**) &env, Version());
switch(res)
{
case JNI_OK:
break;
case JNI_EDETACHED:
attachArgs.version = Version();
attachArgs.name = (char*) "";
attachArgs.group = nullptr;
res = val->AttachCurrentThread((void**) &env, &attachArgs);
if(res != JNI_OK)
{
printf("getJNIEnv: failed to attach to current thread.");
return JavaEnv();
}
break;
case JNI_EVERSION:
printf("getJNIEnv: Unsupported JNI version.");
return JavaEnv();
default:
printf("getJNIEnv: JNI API error.");
return JavaEnv();
}
return JavaEnv(env);
}
void JniJavaVM::Destroy() {
if(doDestroy && Valid()) {
val->DestroyJavaVM();
val = nullptr;
}
}
}