blob: 72a3309d9ddc6cfd448a1834191f9a630ff3547f [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>
20
21#include "jni.h"
22
23#if defined(NDEBUG)
24#error test code compiled without NDEBUG
25#endif
26
27static JavaVM* jvm = NULL;
28
29extern "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
36static 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 Carlstrom67fe2b42013-10-15 18:51:42 -070057// http://b/10994325
Brian Carlstromce888532013-10-10 00:32:58 -070058extern "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 Carlstrom67fe2b42013-10-15 18:51:42 -070069
70// http://b/11243757
71extern "C" JNIEXPORT void JNICALL Java_JniTest_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env,
72 jclass) {
73 jclass super_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SuperClass");
74 assert(super_class != NULL);
75
76 jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V");
77 assert(execute != NULL);
78
79 jclass sub_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SubClass");
80 assert(sub_class != NULL);
81
82 env->CallStaticVoidMethod(sub_class, execute);
83}