-
Notifications
You must be signed in to change notification settings - Fork 31
/
method_test_jni.cc
284 lines (228 loc) · 10.4 KB
/
method_test_jni.cc
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
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include "jni_bind.h"
using ::jni::Class;
using ::jni::LocalObject;
using ::jni::Method;
using ::jni::Overload;
using ::jni::Params;
using ::jni::Return;
static std::unique_ptr<jni::JvmRef<jni::kDefaultJvm>> jvm;
// clang-format off
constexpr Class kMethodTestHelper {
"com/jnibind/test/MethodTestHelper",
// Void Method Tests.
Method{"voidMethod", Return<void>{}, Params<>{}},
Method{"voidMethodTakesOneInt", Return<void>{}, Params<jint>{}},
Method{"voidMethodTakesFiveInts", Return<void>{}, Params<jint,jint,jint,jint,jint>{}},
// Boolean Method Tests.
Method{"booleanMethod", Return<jboolean>{}, Params<>{}},
Method{"booleanMethodTakesOneBoolean", Return<jboolean>{}, Params<jboolean>{}},
Method{"booleanMethodTakesFiveBooleans", Return<jboolean>{}, Params<jboolean,jboolean,jboolean,jboolean,jboolean>{}},
// Int Method Tests.
Method{"intMethod", Return<jint>{}, Params<>{}},
Method{"intMethodTakesOneInt", Return<jint>{}, Params<jint>{}},
Method{"intMethodTakesFiveInts", Return<jint>{}, Params<jint,jint,jint,jint,jint>{}},
// Long Method Tests.
Method{"longMethod", Return<jlong>{}, Params<>{}},
Method{"longMethodTakesOneLong", Return<jlong>{}, Params<jlong>{}},
Method{"longMethodTakesFiveLongs", Return<jlong>{}, Params<jlong,jlong,jlong,jlong,jlong>{}},
// Float Method Tests.
Method{"floatMethod", Return<jfloat>{}, Params<>{}},
Method{"floatMethodTakesOneFloat", Return<jfloat>{}, Params<jfloat>{}},
Method{"floatMethodTakesFiveFloats", Return<jfloat>{}, Params<jfloat,jfloat,jfloat,jfloat,jfloat>{}},
// Double Method Tests.
Method{"doubleMethod", Return<jdouble>{}, Params<>{}},
Method{"doubleMethodTakesOneDouble", Return<jdouble>{}, Params<jdouble>{}},
Method{"doubleMethodTakesFiveDoubles", Return<jdouble>{}, Params<jdouble,jdouble,jdouble,jdouble,jdouble>{}},
// Overloaded Method Tests.
Method{"foo",
Overload { Return<int>{}, Params<>{}},
Overload { Return<int>{}, Params<jfloat>{}},
Overload { Return<int>{}, Params<jfloat,jfloat>{}}
},
// Overloaded double/int Tests.
Method{"intDouble",
Overload { Return<void>{}, Params<jint, jdouble>{}},
Overload { Return<void>{}, Params<jdouble, jint>{}}
},
};
// clang-format on
extern "C" {
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) {
jvm.reset(new jni::JvmRef<jni::kDefaultJvm>(pjvm));
return JNI_VERSION_1_6;
}
JNIEXPORT void JNICALL
Java_com_jnibind_test_MethodTest_jniTearDown(JavaVM* pjvm, void* reserved) {
jvm = nullptr;
}
/** Void Method Tests. */
JNIEXPORT void JNICALL Java_com_jnibind_test_MethodTest_jniVoidMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
gtest_helper("voidMethod");
}
JNIEXPORT void JNICALL
Java_com_jnibind_test_MethodTest_jniVoidMethodTakesOneInt(JNIEnv* env, jclass,
jobject object,
jint i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
gtest_helper("voidMethodTakesOneInt", jint{i});
}
JNIEXPORT void JNICALL
Java_com_jnibind_test_MethodTest_jniVoidMethodTakesFiveInts(JNIEnv* env, jclass,
jobject object,
jint i1, jint i2,
jint i3, jint i4,
jint i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
gtest_helper("voidMethodTakesFiveInts", jint{i1}, jint{i2}, jint{i3},
jint{i4}, jint{i5});
}
/** Boolean Method Tests. */
JNIEXPORT jboolean JNICALL Java_com_jnibind_test_MethodTest_jniBooleanMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("booleanMethod");
}
JNIEXPORT jboolean JNICALL
Java_com_jnibind_test_MethodTest_jniBooleanMethodTakesOneBoolean(JNIEnv* env,
jclass,
jobject object,
jboolean i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("booleanMethodTakesOneBoolean", jboolean{i});
}
JNIEXPORT jboolean JNICALL
Java_com_jnibind_test_MethodTest_jniBooleanMethodTakesFiveBooleans(
JNIEnv* env, jclass, jobject object, jboolean i1, jboolean i2, jboolean i3,
jboolean i4, jboolean i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("booleanMethodTakesFiveBooleans", jboolean{i1},
jboolean{i2}, jboolean{i3}, jboolean{i4}, jboolean{i5});
}
/** Int Method Tests. */
JNIEXPORT jint JNICALL Java_com_jnibind_test_MethodTest_jniIntMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("intMethod");
}
JNIEXPORT jint JNICALL Java_com_jnibind_test_MethodTest_jniIntMethodTakesOneInt(
JNIEnv* env, jclass, jobject object, jint i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("intMethodTakesOneInt", jint{i});
}
JNIEXPORT jint JNICALL
Java_com_jnibind_test_MethodTest_jniIntMethodTakesFiveInts(JNIEnv* env, jclass,
jobject object,
jint i1, jint i2,
jint i3, jint i4,
jint i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("intMethodTakesFiveInts", jint{i1}, jint{i2}, jint{i3},
jint{i4}, jint{i5});
}
/** Long Method Tests. */
JNIEXPORT jlong JNICALL Java_com_jnibind_test_MethodTest_jniLongMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("longMethod");
}
JNIEXPORT jlong JNICALL
Java_com_jnibind_test_MethodTest_jniLongMethodTakesOneLong(JNIEnv* env, jclass,
jobject object,
jlong i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("longMethodTakesOneLong", jlong{i});
}
JNIEXPORT jlong JNICALL
Java_com_jnibind_test_MethodTest_jniLongMethodTakesFiveLongs(
JNIEnv* env, jclass, jobject object, jlong i1, jlong i2, jlong i3, jlong i4,
jlong i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("longMethodTakesFiveLongs", jlong{i1}, jlong{i2},
jlong{i3}, jlong{i4}, jlong{i5});
}
/** Float Method Tests. */
JNIEXPORT jfloat JNICALL Java_com_jnibind_test_MethodTest_jniFloatMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("floatMethod");
}
JNIEXPORT jfloat JNICALL
Java_com_jnibind_test_MethodTest_jniFloatMethodTakesOneFloat(JNIEnv* env,
jclass,
jobject object,
jfloat i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("floatMethodTakesOneFloat", jfloat{i});
}
JNIEXPORT jfloat JNICALL
Java_com_jnibind_test_MethodTest_jniFloatMethodTakesFiveFloats(
JNIEnv* env, jclass, jobject object, jfloat i1, jfloat i2, jfloat i3,
jfloat i4, jfloat i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("floatMethodTakesFiveFloats", jfloat{i1}, jfloat{i2},
jfloat{i3}, jfloat{i4}, jfloat{i5});
}
/** Double Method Tests. */
JNIEXPORT jdouble JNICALL Java_com_jnibind_test_MethodTest_jniDoubleMethod(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("doubleMethod");
}
JNIEXPORT jdouble JNICALL
Java_com_jnibind_test_MethodTest_jniDoubleMethodTakesOneDouble(JNIEnv* env,
jclass,
jobject object,
jdouble i) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("doubleMethodTakesOneDouble", jdouble{i});
}
JNIEXPORT jdouble JNICALL
Java_com_jnibind_test_MethodTest_jniDoubleMethodTakesFiveDoubles(
JNIEnv* env, jclass, jobject object, jdouble i1, jdouble i2, jdouble i3,
jdouble i4, jdouble i5) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("doubleMethodTakesFiveDoubles", jdouble{i1}, jdouble{i2},
jdouble{i3}, jdouble{i4}, jdouble{i5});
}
/** Overload Method Tests. */
JNIEXPORT int JNICALL Java_com_jnibind_test_MethodTest_jniCallFooOverload1(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("foo");
}
JNIEXPORT int JNICALL Java_com_jnibind_test_MethodTest_jniCallFooOverload2(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("foo", 123.f);
}
JNIEXPORT int JNICALL Java_com_jnibind_test_MethodTest_jniCallFooOverload3(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
return gtest_helper("foo", 123.f, 456.f);
}
/** Overload int/double disambiguation. */
JNIEXPORT void JNICALL Java_com_jnibind_test_MethodTest_jniCallFooOverload4(
JNIEnv* env, jclass, jobject object) {
LocalObject<kMethodTestHelper> gtest_helper{object};
gtest_helper("intDouble", 123, 456.);
gtest_helper("intDouble", 456., 123);
}
} // extern "C"