Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 20 | |
| 21 | #include "jni.h" |
| 22 | |
| 23 | #if defined(NDEBUG) |
| 24 | #error test code compiled without NDEBUG |
| 25 | #endif |
| 26 | |
| 27 | static JavaVM* jvm = NULL; |
| 28 | |
| 29 | extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { |
| 30 | assert(vm != NULL); |
| 31 | assert(jvm == NULL); |
| 32 | jvm = vm; |
| 33 | return JNI_VERSION_1_6; |
| 34 | } |
| 35 | |
| 36 | static void* testFindClassOnAttachedNativeThread(void*) { |
| 37 | assert(jvm != NULL); |
| 38 | |
| 39 | JNIEnv* env = NULL; |
| 40 | JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, NULL }; |
| 41 | int attach_result = jvm->AttachCurrentThread(&env, &args); |
| 42 | assert(attach_result == 0); |
| 43 | |
| 44 | jclass clazz = env->FindClass("JniTest"); |
| 45 | assert(clazz != NULL); |
| 46 | assert(!env->ExceptionCheck()); |
| 47 | |
| 48 | jobjectArray array = env->NewObjectArray(0, clazz, NULL); |
| 49 | assert(array != NULL); |
| 50 | assert(!env->ExceptionCheck()); |
| 51 | |
| 52 | int detach_result = jvm->DetachCurrentThread(); |
| 53 | assert(detach_result == 0); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 57 | // http://b/10994325 |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 58 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testFindClassOnAttachedNativeThread(JNIEnv*, |
| 59 | jclass) { |
| 60 | pthread_t pthread; |
| 61 | int pthread_create_result = pthread_create(&pthread, |
| 62 | NULL, |
| 63 | testFindClassOnAttachedNativeThread, |
| 64 | NULL); |
| 65 | assert(pthread_create_result == 0); |
| 66 | int pthread_join_result = pthread_join(pthread, NULL); |
| 67 | assert(pthread_join_result == 0); |
| 68 | } |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 69 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 70 | static void* testFindFieldOnAttachedNativeThread(void*) { |
| 71 | assert(jvm != NULL); |
| 72 | |
| 73 | JNIEnv* env = NULL; |
| 74 | JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, NULL }; |
| 75 | int attach_result = jvm->AttachCurrentThread(&env, &args); |
| 76 | assert(attach_result == 0); |
| 77 | |
| 78 | jclass clazz = env->FindClass("JniTest"); |
| 79 | assert(clazz != NULL); |
| 80 | assert(!env->ExceptionCheck()); |
| 81 | |
| 82 | jfieldID field = env->GetStaticFieldID(clazz, "testFindFieldOnAttachedNativeThreadField", "Z"); |
| 83 | assert(field != NULL); |
| 84 | assert(!env->ExceptionCheck()); |
| 85 | |
| 86 | env->SetStaticBooleanField(clazz, field, JNI_TRUE); |
| 87 | |
| 88 | int detach_result = jvm->DetachCurrentThread(); |
| 89 | assert(detach_result == 0); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testFindFieldOnAttachedNativeThreadNative(JNIEnv*, |
| 94 | jclass) { |
| 95 | pthread_t pthread; |
| 96 | int pthread_create_result = pthread_create(&pthread, |
| 97 | NULL, |
| 98 | testFindFieldOnAttachedNativeThread, |
| 99 | NULL); |
| 100 | assert(pthread_create_result == 0); |
| 101 | int pthread_join_result = pthread_join(pthread, NULL); |
| 102 | assert(pthread_join_result == 0); |
| 103 | } |
| 104 | |
| 105 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 106 | // http://b/11243757 |
| 107 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env, |
| 108 | jclass) { |
| 109 | jclass super_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SuperClass"); |
| 110 | assert(super_class != NULL); |
| 111 | |
| 112 | jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V"); |
| 113 | assert(execute != NULL); |
| 114 | |
| 115 | jclass sub_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SubClass"); |
| 116 | assert(sub_class != NULL); |
| 117 | |
| 118 | env->CallStaticVoidMethod(sub_class, execute); |
| 119 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 120 | |
| 121 | extern "C" JNIEXPORT jobject JNICALL Java_JniTest_testGetMirandaMethodNative(JNIEnv* env, jclass) { |
| 122 | jclass abstract_class = env->FindClass("JniTest$testGetMirandaMethod_MirandaAbstract"); |
| 123 | assert(abstract_class != NULL); |
| 124 | jmethodID miranda_method = env->GetMethodID(abstract_class, "inInterface", "()Z"); |
| 125 | assert(miranda_method != NULL); |
| 126 | return env->ToReflectedMethod(abstract_class, miranda_method, JNI_FALSE); |
| 127 | } |