blob: db0dd327711ded34221c475e65d210b87bdb52ab [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <stdio.h>
19#include <pthread.h>
Narayan Kamathef809d02013-12-19 17:52:47 +000020#include <vector>
Brian Carlstromce888532013-10-10 00:32:58 -070021
22#include "jni.h"
23
24#if defined(NDEBUG)
25#error test code compiled without NDEBUG
26#endif
27
Mathieu Chartier2cebb242015-04-21 16:50:40 -070028static JavaVM* jvm = nullptr;
Brian Carlstromce888532013-10-10 00:32:58 -070029
30extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
Andreas Gampe718ac652014-08-11 18:51:53 -070031 assert(vm != nullptr);
32 assert(jvm == nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -070033 jvm = vm;
34 return JNI_VERSION_1_6;
35}
36
Andreas Gampe718ac652014-08-11 18:51:53 -070037static void* AttachHelper(void* arg) {
38 assert(jvm != nullptr);
Brian Carlstromce888532013-10-10 00:32:58 -070039
Andreas Gampe718ac652014-08-11 18:51:53 -070040 JNIEnv* env = nullptr;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, nullptr };
Brian Carlstromce888532013-10-10 00:32:58 -070042 int attach_result = jvm->AttachCurrentThread(&env, &args);
43 assert(attach_result == 0);
44
Andreas Gampe718ac652014-08-11 18:51:53 -070045 typedef void (*Fn)(JNIEnv*);
46 Fn fn = reinterpret_cast<Fn>(arg);
47 fn(env);
Brian Carlstromce888532013-10-10 00:32:58 -070048
49 int detach_result = jvm->DetachCurrentThread();
50 assert(detach_result == 0);
Andreas Gampe718ac652014-08-11 18:51:53 -070051 return nullptr;
52}
53
54static void PthreadHelper(void (*fn)(JNIEnv*)) {
55 pthread_t pthread;
56 int pthread_create_result = pthread_create(&pthread, nullptr, AttachHelper,
57 reinterpret_cast<void*>(fn));
58 assert(pthread_create_result == 0);
59 int pthread_join_result = pthread_join(pthread, nullptr);
60 assert(pthread_join_result == 0);
61}
62
63static void testFindClassOnAttachedNativeThread(JNIEnv* env) {
64 jclass clazz = env->FindClass("Main");
65 assert(clazz != nullptr);
66 assert(!env->ExceptionCheck());
67
68 jobjectArray array = env->NewObjectArray(0, clazz, nullptr);
69 assert(array != nullptr);
70 assert(!env->ExceptionCheck());
Brian Carlstromce888532013-10-10 00:32:58 -070071}
72
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070073// http://b/10994325
Andreas Gampe718ac652014-08-11 18:51:53 -070074extern "C" JNIEXPORT void JNICALL Java_Main_testFindClassOnAttachedNativeThread(JNIEnv*, jclass) {
75 PthreadHelper(&testFindClassOnAttachedNativeThread);
Brian Carlstromce888532013-10-10 00:32:58 -070076}
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070077
Andreas Gampe718ac652014-08-11 18:51:53 -070078static void testFindFieldOnAttachedNativeThread(JNIEnv* env) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070079 jclass clazz = env->FindClass("Main");
Andreas Gampe718ac652014-08-11 18:51:53 -070080 assert(clazz != nullptr);
Jeff Hao62509b62013-12-10 17:44:56 -080081 assert(!env->ExceptionCheck());
82
83 jfieldID field = env->GetStaticFieldID(clazz, "testFindFieldOnAttachedNativeThreadField", "Z");
Andreas Gampe718ac652014-08-11 18:51:53 -070084 assert(field != nullptr);
Jeff Hao62509b62013-12-10 17:44:56 -080085 assert(!env->ExceptionCheck());
86
87 env->SetStaticBooleanField(clazz, field, JNI_TRUE);
Jeff Hao62509b62013-12-10 17:44:56 -080088}
89
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070090extern "C" JNIEXPORT void JNICALL Java_Main_testFindFieldOnAttachedNativeThreadNative(JNIEnv*,
Andreas Gampe718ac652014-08-11 18:51:53 -070091 jclass) {
92 PthreadHelper(&testFindFieldOnAttachedNativeThread);
Jeff Hao62509b62013-12-10 17:44:56 -080093}
94
Andreas Gampe718ac652014-08-11 18:51:53 -070095static void testReflectFieldGetFromAttachedNativeThread(JNIEnv* env) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070096 jclass clazz = env->FindClass("Main");
Andreas Gampe718ac652014-08-11 18:51:53 -070097 assert(clazz != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010098 assert(!env->ExceptionCheck());
99
100 jclass class_clazz = env->FindClass("java/lang/Class");
Andreas Gampe718ac652014-08-11 18:51:53 -0700101 assert(class_clazz != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100102 assert(!env->ExceptionCheck());
103
104 jmethodID getFieldMetodId = env->GetMethodID(class_clazz, "getField",
105 "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Andreas Gampe718ac652014-08-11 18:51:53 -0700106 assert(getFieldMetodId != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100107 assert(!env->ExceptionCheck());
108
109 jstring field_name = env->NewStringUTF("testReflectFieldGetFromAttachedNativeThreadField");
Andreas Gampe718ac652014-08-11 18:51:53 -0700110 assert(field_name != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100111 assert(!env->ExceptionCheck());
112
113 jobject field = env->CallObjectMethod(clazz, getFieldMetodId, field_name);
Andreas Gampe718ac652014-08-11 18:51:53 -0700114 assert(field != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100115 assert(!env->ExceptionCheck());
116
117 jclass field_clazz = env->FindClass("java/lang/reflect/Field");
Andreas Gampe718ac652014-08-11 18:51:53 -0700118 assert(field_clazz != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100119 assert(!env->ExceptionCheck());
120
121 jmethodID getBooleanMetodId = env->GetMethodID(field_clazz, "getBoolean",
122 "(Ljava/lang/Object;)Z");
Andreas Gampe718ac652014-08-11 18:51:53 -0700123 assert(getBooleanMetodId != nullptr);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100124 assert(!env->ExceptionCheck());
125
126 jboolean value = env->CallBooleanMethod(field, getBooleanMetodId, /* ignored */ clazz);
127 assert(value == false);
128 assert(!env->ExceptionCheck());
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100129}
130
131// http://b/15539150
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700132extern "C" JNIEXPORT void JNICALL Java_Main_testReflectFieldGetFromAttachedNativeThreadNative(
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100133 JNIEnv*, jclass) {
Andreas Gampe718ac652014-08-11 18:51:53 -0700134 PthreadHelper(&testReflectFieldGetFromAttachedNativeThread);
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +0100135}
136
Jeff Hao62509b62013-12-10 17:44:56 -0800137
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700138// http://b/11243757
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700139extern "C" JNIEXPORT void JNICALL Java_Main_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env,
Andreas Gampe718ac652014-08-11 18:51:53 -0700140 jclass) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700141 jclass super_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SuperClass");
Andreas Gampe718ac652014-08-11 18:51:53 -0700142 assert(super_class != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700143
144 jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V");
Andreas Gampe718ac652014-08-11 18:51:53 -0700145 assert(execute != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700146
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700147 jclass sub_class = env->FindClass("Main$testCallStaticVoidMethodOnSubClass_SubClass");
Andreas Gampe718ac652014-08-11 18:51:53 -0700148 assert(sub_class != nullptr);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700149
150 env->CallStaticVoidMethod(sub_class, execute);
151}
Jeff Hao201803f2013-11-20 18:11:39 -0800152
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700153extern "C" JNIEXPORT jobject JNICALL Java_Main_testGetMirandaMethodNative(JNIEnv* env, jclass) {
154 jclass abstract_class = env->FindClass("Main$testGetMirandaMethod_MirandaAbstract");
Andreas Gampe718ac652014-08-11 18:51:53 -0700155 assert(abstract_class != nullptr);
Jeff Hao201803f2013-11-20 18:11:39 -0800156 jmethodID miranda_method = env->GetMethodID(abstract_class, "inInterface", "()Z");
Andreas Gampe718ac652014-08-11 18:51:53 -0700157 assert(miranda_method != nullptr);
Jeff Hao201803f2013-11-20 18:11:39 -0800158 return env->ToReflectedMethod(abstract_class, miranda_method, JNI_FALSE);
159}
Narayan Kamathef809d02013-12-19 17:52:47 +0000160
161// https://code.google.com/p/android/issues/detail?id=63055
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700162extern "C" void JNICALL Java_Main_testZeroLengthByteBuffers(JNIEnv* env, jclass) {
Narayan Kamathef809d02013-12-19 17:52:47 +0000163 std::vector<uint8_t> buffer(1);
164 jobject byte_buffer = env->NewDirectByteBuffer(&buffer[0], 0);
Andreas Gampe718ac652014-08-11 18:51:53 -0700165 assert(byte_buffer != nullptr);
Narayan Kamathef809d02013-12-19 17:52:47 +0000166 assert(!env->ExceptionCheck());
167
168 assert(env->GetDirectBufferAddress(byte_buffer) == &buffer[0]);
169 assert(env->GetDirectBufferCapacity(byte_buffer) == 0);
170}
Andreas Gamped1104322014-05-01 14:38:56 -0700171
172constexpr size_t kByteReturnSize = 7;
173jbyte byte_returns[kByteReturnSize] = { 0, 1, 2, 127, -1, -2, -128 };
174
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700175extern "C" jbyte JNICALL Java_Main_byteMethod(JNIEnv*, jclass, jbyte b1, jbyte b2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700176 jbyte b3, jbyte b4, jbyte b5, jbyte b6,
177 jbyte b7, jbyte b8, jbyte b9, jbyte b10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700178 // We use b1 to drive the output.
179 assert(b2 == 2);
180 assert(b3 == -3);
181 assert(b4 == 4);
182 assert(b5 == -5);
183 assert(b6 == 6);
184 assert(b7 == -7);
185 assert(b8 == 8);
186 assert(b9 == -9);
187 assert(b10 == 10);
188
189 assert(0 <= b1);
190 assert(b1 < static_cast<jbyte>(kByteReturnSize));
191
192 return byte_returns[b1];
193}
194
195constexpr size_t kShortReturnSize = 9;
196jshort short_returns[kShortReturnSize] = { 0, 1, 2, 127, 32767, -1, -2, -128,
197 static_cast<jshort>(0x8000) };
198// The weird static_cast is because short int is only guaranteed down to -32767, not Java's -32768.
199
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700200extern "C" jshort JNICALL Java_Main_shortMethod(JNIEnv*, jclass, jshort s1, jshort s2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700201 jshort s3, jshort s4, jshort s5, jshort s6,
202 jshort s7, jshort s8, jshort s9, jshort s10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700203 // We use s1 to drive the output.
204 assert(s2 == 2);
205 assert(s3 == -3);
206 assert(s4 == 4);
207 assert(s5 == -5);
208 assert(s6 == 6);
209 assert(s7 == -7);
210 assert(s8 == 8);
211 assert(s9 == -9);
212 assert(s10 == 10);
213
214 assert(0 <= s1);
215 assert(s1 < static_cast<jshort>(kShortReturnSize));
216
217 return short_returns[s1];
218}
219
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700220extern "C" jboolean JNICALL Java_Main_booleanMethod(JNIEnv*, jclass, jboolean b1,
Andreas Gampe718ac652014-08-11 18:51:53 -0700221 jboolean b2, jboolean b3, jboolean b4,
222 jboolean b5, jboolean b6, jboolean b7,
223 jboolean b8, jboolean b9, jboolean b10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700224 // We use b1 to drive the output.
225 assert(b2 == JNI_TRUE);
226 assert(b3 == JNI_FALSE);
227 assert(b4 == JNI_TRUE);
228 assert(b5 == JNI_FALSE);
229 assert(b6 == JNI_TRUE);
230 assert(b7 == JNI_FALSE);
231 assert(b8 == JNI_TRUE);
232 assert(b9 == JNI_FALSE);
233 assert(b10 == JNI_TRUE);
234
235 assert(b1 == JNI_TRUE || b1 == JNI_FALSE);
236 return b1;
237}
238
239constexpr size_t kCharReturnSize = 8;
240jchar char_returns[kCharReturnSize] = { 0, 1, 2, 127, 255, 256, 15000, 34000 };
241
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700242extern "C" jchar JNICALL Java_Main_charMethod(JNIEnv*, jclass, jchar c1, jchar c2,
Andreas Gampe718ac652014-08-11 18:51:53 -0700243 jchar c3, jchar c4, jchar c5, jchar c6, jchar c7,
244 jchar c8, jchar c9, jchar c10) {
Andreas Gamped1104322014-05-01 14:38:56 -0700245 // We use c1 to drive the output.
246 assert(c2 == 'a');
247 assert(c3 == 'b');
248 assert(c4 == 'c');
249 assert(c5 == '0');
250 assert(c6 == '1');
251 assert(c7 == '2');
252 assert(c8 == 1234);
253 assert(c9 == 2345);
254 assert(c10 == 3456);
255
256 assert(c1 < static_cast<jchar>(kCharReturnSize));
257
258 return char_returns[c1];
259}
Narayan Kamath1268b742014-07-11 19:15:11 +0100260
Mathieu Chartier22c1caa2015-06-02 13:40:12 -0700261extern "C" JNIEXPORT void JNICALL Java_Main_removeLocalObject(JNIEnv* env, jclass, jclass o) {
262 // Delete the arg to see if it crashes.
263 env->DeleteLocalRef(o);
264}
265
Narayan Kamath1268b742014-07-11 19:15:11 +0100266extern "C" JNIEXPORT jboolean JNICALL Java_Main_nativeIsAssignableFrom(JNIEnv* env, jclass,
267 jclass from, jclass to) {
268 return env->IsAssignableFrom(from, to);
269}
Andreas Gampe718ac652014-08-11 18:51:53 -0700270
271static void testShallowGetCallingClassLoader(JNIEnv* env) {
272 // Test direct call.
273 {
274 jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack");
275 assert(vmstack_clazz != nullptr);
276 assert(!env->ExceptionCheck());
277
278 jmethodID getCallingClassLoaderMethodId = env->GetStaticMethodID(vmstack_clazz,
279 "getCallingClassLoader",
280 "()Ljava/lang/ClassLoader;");
281 assert(getCallingClassLoaderMethodId != nullptr);
282 assert(!env->ExceptionCheck());
283
284 jobject class_loader = env->CallStaticObjectMethod(vmstack_clazz,
285 getCallingClassLoaderMethodId);
286 assert(class_loader == nullptr);
287 assert(!env->ExceptionCheck());
288 }
289
290 // Test one-level call. Use System.loadLibrary().
291 {
292 jclass system_clazz = env->FindClass("java/lang/System");
293 assert(system_clazz != nullptr);
294 assert(!env->ExceptionCheck());
295
296 jmethodID loadLibraryMethodId = env->GetStaticMethodID(system_clazz, "loadLibrary",
297 "(Ljava/lang/String;)V");
298 assert(loadLibraryMethodId != nullptr);
299 assert(!env->ExceptionCheck());
300
301 // Create a string object.
Nicolas Geoffray005f6972014-12-03 18:10:39 +0000302 jobject library_string = env->NewStringUTF("non_existing_library");
Andreas Gampe718ac652014-08-11 18:51:53 -0700303 assert(library_string != nullptr);
304 assert(!env->ExceptionCheck());
305
306 env->CallStaticVoidMethod(system_clazz, loadLibraryMethodId, library_string);
Nicolas Geoffray005f6972014-12-03 18:10:39 +0000307 assert(env->ExceptionCheck());
Andreas Gampe718ac652014-08-11 18:51:53 -0700308
Nicolas Geoffray005f6972014-12-03 18:10:39 +0000309 // We expect UnsatisfiedLinkError.
310 jthrowable thrown = env->ExceptionOccurred();
311 env->ExceptionClear();
312
313 jclass unsatisfied_link_error_clazz = env->FindClass("java/lang/UnsatisfiedLinkError");
314 jclass thrown_class = env->GetObjectClass(thrown);
315 assert(env->IsSameObject(unsatisfied_link_error_clazz, thrown_class));
Andreas Gampe718ac652014-08-11 18:51:53 -0700316 }
317}
318
319// http://b/16867274
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetCallingClassLoader(JNIEnv*,
Andreas Gampe718ac652014-08-11 18:51:53 -0700321 jclass) {
322 PthreadHelper(&testShallowGetCallingClassLoader);
323}
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700324
325static void testShallowGetStackClass2(JNIEnv* env) {
326 jclass vmstack_clazz = env->FindClass("dalvik/system/VMStack");
327 assert(vmstack_clazz != nullptr);
328 assert(!env->ExceptionCheck());
329
330 // Test direct call.
331 {
332 jmethodID getStackClass2MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass2",
333 "()Ljava/lang/Class;");
334 assert(getStackClass2MethodId != nullptr);
335 assert(!env->ExceptionCheck());
336
337 jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass2MethodId);
338 assert(caller_class == nullptr);
339 assert(!env->ExceptionCheck());
340 }
341
342 // Test one-level call. Use VMStack.getStackClass1().
343 {
344 jmethodID getStackClass1MethodId = env->GetStaticMethodID(vmstack_clazz, "getStackClass1",
345 "()Ljava/lang/Class;");
346 assert(getStackClass1MethodId != nullptr);
347 assert(!env->ExceptionCheck());
348
349 jobject caller_class = env->CallStaticObjectMethod(vmstack_clazz, getStackClass1MethodId);
350 assert(caller_class == nullptr);
351 assert(!env->ExceptionCheck());
352 }
353
354 // For better testing we would need to compile against libcore and have a two-deep stack
355 // ourselves.
356}
357
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700358extern "C" JNIEXPORT void JNICALL Java_Main_nativeTestShallowGetStackClass2(JNIEnv*, jclass) {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700359 PthreadHelper(&testShallowGetStackClass2);
360}
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700361
362class JniCallNonvirtualVoidMethodTest {
363 public:
364 explicit JniCallNonvirtualVoidMethodTest(JNIEnv* env)
365 : env_(env),
366 check_jni_ri_(true),
367 check_jni_android_(true),
368 super_(GetClass("JniCallNonvirtualTest")),
369 sub_(GetClass("JniCallNonvirtualTestSubclass")),
370 super_constructor_(GetMethodID(super_, true, "<init>")),
371 super_static_(GetMethodID(super_, false, "staticMethod")),
372 super_nonstatic_(GetMethodID(super_, true, "nonstaticMethod")),
373 sub_constructor_(GetMethodID(sub_, true, "<init>")),
374 sub_static_(GetMethodID(sub_, false, "staticMethod")),
375 sub_nonstatic_(GetMethodID(sub_, true, "nonstaticMethod")),
376 super_field_(GetFieldID(super_, "nonstaticMethodSuperCalled")),
377 sub_field_(GetFieldID(super_, "nonstaticMethodSubCalled")) {}
378
379 void Test() {
380 TestStaticCallNonvirtualMethod();
381 TestNewObject();
382 TestnonstaticCallNonvirtualMethod();
383 }
384
385 JNIEnv* const env_;
386
387 bool const check_jni_ri_;
388 bool const check_jni_android_;
389
390 jclass const super_;
391 jclass const sub_;
392
393 jmethodID const super_constructor_;
394 jmethodID const super_static_;
395 jmethodID const super_nonstatic_;
396 jmethodID const sub_constructor_;
397 jmethodID const sub_static_;
398 jmethodID const sub_nonstatic_;
399
400 jfieldID const super_field_;
401 jfieldID const sub_field_;
402
403 private:
404 jclass GetClass(const char* class_name) {
405 jclass c = env_->FindClass(class_name);
406 if (env_->ExceptionCheck()) {
407 env_->ExceptionDescribe();
408 env_->FatalError(__FUNCTION__);
409 }
410 assert(!env_->ExceptionCheck());
411 assert(c != nullptr);
412 return c;
413 }
414
415 jmethodID GetMethodID(jclass c, bool nonstatic, const char* method_name) {
416 jmethodID m = ((nonstatic) ?
417 env_->GetMethodID(c, method_name, "()V") :
418 env_->GetStaticMethodID(c, method_name, "()V"));
419 if (env_->ExceptionCheck()) {
420 env_->ExceptionDescribe();
421 env_->FatalError(__FUNCTION__);
422 }
423 assert(m != nullptr);
424 return m;
425 }
426
427 jobject CallConstructor(jclass c, jmethodID m) {
428 jobject o = env_->NewObject(c, m);
429 if (env_->ExceptionCheck()) {
430 env_->ExceptionDescribe();
431 env_->FatalError(__FUNCTION__);
432 }
433 assert(o != nullptr);
434 return o;
435 }
436
437 void CallMethod(jobject o, jclass c, jmethodID m, bool nonstatic, const char* test_case) {
438 printf("RUNNING %s\n", test_case);
439 env_->CallNonvirtualVoidMethod(o, c, m);
440 bool exception_check = env_->ExceptionCheck();
441 if (c == nullptr || !nonstatic) {
442 if (!exception_check) {
443 printf("FAILED %s due to missing exception\n", test_case);
444 env_->FatalError("Expected NullPointerException with null jclass");
445 }
446 env_->ExceptionClear();
447 } else if (exception_check) {
448 printf("FAILED %s due to pending exception\n", test_case);
449 env_->ExceptionDescribe();
450 env_->FatalError(test_case);
451 }
452 printf("PASSED %s\n", test_case);
453 }
454
455 jfieldID GetFieldID(jclass c, const char* field_name) {
456 jfieldID m = env_->GetFieldID(c, field_name, "Z");
457 if (env_->ExceptionCheck()) {
458 env_->ExceptionDescribe();
459 env_->FatalError(__FUNCTION__);
460 }
461 assert(m != nullptr);
462 return m;
463 }
464
465 jboolean GetBooleanField(jobject o, jfieldID f) {
466 jboolean b = env_->GetBooleanField(o, f);
467 if (env_->ExceptionCheck()) {
468 env_->ExceptionDescribe();
469 env_->FatalError(__FUNCTION__);
470 }
471 return b;
472 }
473
474 void TestStaticCallNonvirtualMethod() {
475 if (!check_jni_ri_&& !check_jni_android_) {
476 CallMethod(nullptr, nullptr, super_static_, false, "null object, null class, super static");
477 }
478 if (!check_jni_android_) {
479 CallMethod(nullptr, super_, super_static_, false, "null object, super class, super static");
480 }
481 if (!check_jni_android_) {
482 CallMethod(nullptr, sub_, super_static_, false, "null object, sub class, super static");
483 }
484
485 if (!check_jni_ri_ && !check_jni_android_) {
486 CallMethod(nullptr, nullptr, sub_static_, false, "null object, null class, sub static");
487 }
488 if (!check_jni_android_) {
489 CallMethod(nullptr, sub_, sub_static_, false, "null object, super class, sub static");
490 }
491 if (!check_jni_android_) {
492 CallMethod(nullptr, super_, sub_static_, false, "null object, super class, sub static");
493 }
494 }
495
496 void TestNewObject() {
497 jobject super_super = CallConstructor(super_, super_constructor_);
498 jobject super_sub = CallConstructor(super_, sub_constructor_);
499 jobject sub_super = CallConstructor(sub_, super_constructor_);
500 jobject sub_sub = CallConstructor(sub_, sub_constructor_);
501
502 assert(env_->IsInstanceOf(super_super, super_));
503 assert(!env_->IsInstanceOf(super_super, sub_));
504
505 // Note that even though we called (and ran) the subclass
506 // constructor, we are not the subclass.
507 assert(env_->IsInstanceOf(super_sub, super_));
508 assert(!env_->IsInstanceOf(super_sub, sub_));
509
510 // Note that even though we called the superclass constructor, we
511 // are still the subclass.
512 assert(env_->IsInstanceOf(sub_super, super_));
513 assert(env_->IsInstanceOf(sub_super, sub_));
514
515 assert(env_->IsInstanceOf(sub_sub, super_));
516 assert(env_->IsInstanceOf(sub_sub, sub_));
517 }
518
519 void TestnonstaticCallNonvirtualMethod(bool super_object, bool super_class, bool super_method, const char* test_case) {
520 if (check_jni_android_) {
521 if (super_object && !super_method) {
522 return; // We don't allow a call with sub class method on the super class instance.
523 }
524 if (super_class && !super_method) {
525 return; // We don't allow a call with the sub class method with the super class argument.
526 }
527 }
528 jobject o = ((super_object) ?
529 CallConstructor(super_, super_constructor_) :
530 CallConstructor(sub_, sub_constructor_));
531 jclass c = (super_class) ? super_ : sub_;
532 jmethodID m = (super_method) ? super_nonstatic_ : sub_nonstatic_;
533 CallMethod(o, c, m, true, test_case);
534 jboolean super_field = GetBooleanField(o, super_field_);
535 jboolean sub_field = GetBooleanField(o, sub_field_);
536 assert(super_field == super_method);
537 assert(sub_field != super_method);
538 }
539
540 void TestnonstaticCallNonvirtualMethod() {
541 TestnonstaticCallNonvirtualMethod(true, true, true, "super object, super class, super nonstatic");
542 TestnonstaticCallNonvirtualMethod(true, false, true, "super object, sub class, super nonstatic");
543 TestnonstaticCallNonvirtualMethod(true, false, false, "super object, sub class, sub nonstatic");
544 TestnonstaticCallNonvirtualMethod(true, true, false, "super object, super class, sub nonstatic");
545
546 TestnonstaticCallNonvirtualMethod(false, true, true, "sub object, super class, super nonstatic");
547 TestnonstaticCallNonvirtualMethod(false, false, true, "sub object, sub class, super nonstatic");
548 TestnonstaticCallNonvirtualMethod(false, false, false, "sub object, sub class, sub nonstatic");
549 TestnonstaticCallNonvirtualMethod(false, true, false, "sub object, super class, sub nonstatic");
550 }
551};
552
553extern "C" void JNICALL Java_Main_testCallNonvirtual(JNIEnv* env, jclass) {
554 JniCallNonvirtualVoidMethodTest(env).Test();
555}
Jeff Hao848f70a2014-01-15 13:49:50 -0800556
557extern "C" JNIEXPORT void JNICALL Java_Main_testNewStringObject(JNIEnv* env, jclass) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800558 jclass c = env->FindClass("java/lang/String");
Jeff Hao39b6c242015-05-19 20:30:23 -0700559 assert(c != nullptr);
560
561 jmethodID mid1 = env->GetMethodID(c, "<init>", "()V");
562 assert(mid1 != nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800563 assert(!env->ExceptionCheck());
Jeff Hao39b6c242015-05-19 20:30:23 -0700564 jmethodID mid2 = env->GetMethodID(c, "<init>", "([B)V");
565 assert(mid2 != nullptr);
566 assert(!env->ExceptionCheck());
567 jmethodID mid3 = env->GetMethodID(c, "<init>", "([C)V");
568 assert(mid3 != nullptr);
569 assert(!env->ExceptionCheck());
570 jmethodID mid4 = env->GetMethodID(c, "<init>", "(Ljava/lang/String;)V");
571 assert(mid4 != nullptr);
572 assert(!env->ExceptionCheck());
573
574 const char* test_array = "Test";
575 int byte_array_length = strlen(test_array);
576 jbyteArray byte_array = env->NewByteArray(byte_array_length);
577 env->SetByteArrayRegion(byte_array, 0, byte_array_length, reinterpret_cast<const jbyte*>(test_array));
578
579 // Test NewObject
580 jstring s = reinterpret_cast<jstring>(env->NewObject(c, mid2, byte_array));
581 assert(s != nullptr);
582 assert(env->GetStringLength(s) == byte_array_length);
583 assert(env->GetStringUTFLength(s) == byte_array_length);
Jeff Hao848f70a2014-01-15 13:49:50 -0800584 const char* chars = env->GetStringUTFChars(s, nullptr);
Jeff Hao39b6c242015-05-19 20:30:23 -0700585 assert(strcmp(test_array, chars) == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800586 env->ReleaseStringUTFChars(s, chars);
Jeff Hao39b6c242015-05-19 20:30:23 -0700587
588 // Test AllocObject and Call(Nonvirtual)VoidMethod
589 jstring s1 = reinterpret_cast<jstring>(env->AllocObject(c));
590 assert(s1 != nullptr);
591 jstring s2 = reinterpret_cast<jstring>(env->AllocObject(c));
592 assert(s2 != nullptr);
593 jstring s3 = reinterpret_cast<jstring>(env->AllocObject(c));
594 assert(s3 != nullptr);
595 jstring s4 = reinterpret_cast<jstring>(env->AllocObject(c));
596 assert(s4 != nullptr);
597
598 jcharArray char_array = env->NewCharArray(5);
599 jstring string_arg = env->NewStringUTF("helloworld");
600
601 // With Var Args
602 env->CallVoidMethod(s1, mid1);
603 env->CallNonvirtualVoidMethod(s2, c, mid2, byte_array);
604
605 // With JValues
606 jvalue args3[1];
607 args3[0].l = char_array;
608 jvalue args4[1];
609 args4[0].l = string_arg;
610 env->CallVoidMethodA(s3, mid3, args3);
611 env->CallNonvirtualVoidMethodA(s4, c, mid4, args4);
Jeff Hao450c62b2015-05-28 14:32:07 -0700612
613 // Test with global and weak global references
614 jstring s5 = reinterpret_cast<jstring>(env->AllocObject(c));
615 assert(s5 != nullptr);
616 s5 = reinterpret_cast<jstring>(env->NewGlobalRef(s5));
617 jstring s6 = reinterpret_cast<jstring>(env->AllocObject(c));
618 assert(s6 != nullptr);
619 s6 = reinterpret_cast<jstring>(env->NewWeakGlobalRef(s6));
620
621 env->CallVoidMethod(s5, mid1);
622 env->CallNonvirtualVoidMethod(s6, c, mid2, byte_array);
623 assert(env->GetStringLength(s5) == 0);
624 assert(env->GetStringLength(s6) == byte_array_length);
625 const char* chars6 = env->GetStringUTFChars(s6, nullptr);
626 assert(strcmp(test_array, chars6) == 0);
627 env->ReleaseStringUTFChars(s6, chars6);
Jeff Hao848f70a2014-01-15 13:49:50 -0800628}
Mathieu Chartier72156e22015-07-10 18:26:41 -0700629
630extern "C" JNIEXPORT jlong JNICALL Java_Main_testGetMethodID(JNIEnv* env, jclass, jclass c) {
631 return reinterpret_cast<jlong>(env->GetMethodID(c, "a", "()V"));
632}