-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (188 loc) · 6.04 KB
/
main.cpp
File metadata and controls
218 lines (188 loc) · 6.04 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
#include <iostream>
#include "java4cpp/jvm_launcher.h"
#include "java4cpp/java_classes.h"
#include "allocation.h"
#include "superclass.h"
#include "enumeration.h"
#include "arrays.h"
#include "exceptionHandling.h"
#include "benchmark.h"
/*
template<typename T>
class Java4CppList
{
public:
Java4CppList(jobject obj) {
setJavaObject(obj);
}
~Java4CppList()
{
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
if(javaEnv)
javaEnv->DeleteGlobalRef(_obj);
}
void setJavaObject(jobject obj)
{
if( _obj != NULL )
{
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
if(javaEnv)
javaEnv->DeleteGlobalRef(_obj);
}
if( obj != NULL )
{
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
if(javaEnv)
_obj = javaEnv->NewGlobalRef(obj);
}
else
_obj = NULL;
}
void clear() {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "clear", "()V");
javaEnv->CallVoidMethod(_obj, mid);
Java4CppRuntime::handleJavaException(javaEnv);
}
size_t size() const {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "size", "()I");
jint jresult = javaEnv->CallIntMethod(_obj, mid);
Java4CppRuntime::handleJavaException(javaEnv);
return (size_t)jresult;
}
bool empty() const {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "isEmpty", "()Z");
jboolean jresult = javaEnv->CallBooleanMethod(_obj, mid);
Java4CppRuntime::handleJavaException(javaEnv);
return (bool)jresult;
}
const T& operator[](int pos) const {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "get", "(I)Ljava/lang/Object;");
jobject jresult = (jobject)javaEnv->CallObjectMethod(_obj, mid, (jint)pos);
Java4CppRuntime::handleJavaException(javaEnv);
T result;
if (jresult != NULL) {
jclass cls1 = Java4CppRuntime::getClass(javaEnv, "Ljava/lang/Integer;");
jmethodID mid1 = Java4CppRuntime::getMethodID(javaEnv, cls1, "intValue", "()I");
result = (int)javaEnv->CallIntMethod(jresult, mid1);
javaEnv->DeleteLocalRef(jresult);
}
return result;
}
void push_back(const T& arg1) {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "add", "(Ljava/lang/Object;)Z");
jobject jarg1 = NULL;
if (arg1) {
jclass cls1 = Java4CppRuntime::getClass(javaEnv, "Ljava/lang/Integer;");
jmethodID mid1 = Java4CppRuntime::getStaticMethodID(javaEnv, cls1, "valueOf", "(I)Ljava/lang/Integer;");
jarg1 = javaEnv->CallStaticObjectMethod(cls1, mid1, (jint)arg1.get());
}
javaEnv->CallBooleanMethod(_obj, mid, jarg1);
Java4CppRuntime::handleJavaException(javaEnv);
}
class const_iterator {
public:
typedef std::random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
const_iterator(jobject obj, int pos) {
_obj = obj;
_pos = pos;
}
bool operator==(const const_iterator& other) const {
return _pos == other._pos;
}
bool operator!=(const const_iterator& other) const {
return _pos != other._pos;
}
bool operator<(const const_iterator& other) const {
return _pos < other._pos;
}
const_iterator& operator=(const const_iterator& other) {
_obj = other._obj;
_pos = other._pos;
return *this;
}
const_iterator& operator--() {
--_pos;
return *this;
}
const_iterator operator--(int) {
return const_iterator(_obj, _pos-1);
}
const_iterator& operator++() {
++_pos;
return *this;
}
const_iterator operator++(int) {
return const_iterator(_obj, _pos+1);
}
const_iterator operator+(ptrdiff_t dist) {
return const_iterator(_obj, _pos+dist);
}
ptrdiff_t operator-(const const_iterator& other) const {
return other._pos - _pos;
}
const_iterator operator-(ptrdiff_t dist) {
return const_iterator(_obj, _pos-dist);
}
T operator*() {
JNIEnv *javaEnv = Java4CppRuntime::attachCurrentThread();
jclass cls = Java4CppRuntime::getClass(javaEnv, "java/util/List");
jmethodID mid = Java4CppRuntime::getMethodID(javaEnv, cls, "get", "(I)Ljava/lang/Object;");
jobject jresult = (jobject)javaEnv->CallObjectMethod(_obj, mid, (jint)_pos);
Java4CppRuntime::handleJavaException(javaEnv);
T result;
if (jresult != NULL) {
jclass cls1 = Java4CppRuntime::getClass(javaEnv, "Ljava/lang/Integer;");
jmethodID mid1 = Java4CppRuntime::getMethodID(javaEnv, cls1, "intValue", "()I");
result = (int)javaEnv->CallIntMethod(jresult, mid1);
javaEnv->DeleteLocalRef(jresult);
}
return result;
}
private:
jobject _obj;
int _pos;
};
const_iterator begin() const {
return const_iterator(_obj, 0);
}
const_iterator end() const {
return const_iterator(_obj, size());
}
private:
jobject _obj;
};
*/
int main(void)
{
// jw_setJrePath("/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/libserver.dylib");
jw_setJrePath("/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre/lib/server/libjvm.dylib");
jw_addClassPath("../jars/java4cpp-sample-0.0.1-SNAPSHOT.jar");
try
{
allAllocation();
allSuperclass();
allEnumeration();
allArrays();
allExceptionHandling();
allBenchmark();
}
catch(std::exception& e)
{
std::cout << e.what();
}
return 0;
}