Home | History | Annotate | Download | only in JniTest
      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 
     57 extern "C" JNIEXPORT void JNICALL Java_JniTest_testFindClassOnAttachedNativeThread(JNIEnv*,
     58                                                                                    jclass) {
     59   pthread_t pthread;
     60   int pthread_create_result = pthread_create(&pthread,
     61                                              NULL,
     62                                              testFindClassOnAttachedNativeThread,
     63                                              NULL);
     64   assert(pthread_create_result == 0);
     65   int pthread_join_result = pthread_join(pthread, NULL);
     66   assert(pthread_join_result == 0);
     67 }
     68