1 /*
2  * Copyright (C) 2016 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 <jni.h>
18 
19 namespace {
20 
21 bool g_jni_onload_called = false;
22 
add42(JNIEnv *,jclass,jint x)23 jint add42(JNIEnv* /* env */, jclass /* clazz */, jint x) {
24   return x + 42;
25 }
26 
27 }  // namespace
28 
29 extern "C" {
30 
JNI_OnLoad(JavaVM *,void *)31 jint JNI_OnLoad(JavaVM* /* vm */, void* /* reserved */) {
32   g_jni_onload_called = true;
33   return JNI_VERSION_1_6;
34 }
35 
Java_com_berberis_jnitests_JniTests_intFromJNI(JNIEnv *,jclass)36 JNIEXPORT jint JNICALL Java_com_berberis_jnitests_JniTests_intFromJNI(JNIEnv* /* env */,
37                                                                       jclass /* clazz */) {
38   return 42;
39 }
40 
41 JNIEXPORT jboolean JNICALL
Java_com_berberis_jnitests_JniTests_isJNIOnLoadCalled(JNIEnv *,jclass)42 Java_com_berberis_jnitests_JniTests_isJNIOnLoadCalled(JNIEnv* /* env */, jclass /* clazz */) {
43   return g_jni_onload_called;
44 }
45 
Java_com_berberis_jnitests_JniTests_checkGetVersion(JNIEnv * env,jclass)46 JNIEXPORT jboolean JNICALL Java_com_berberis_jnitests_JniTests_checkGetVersion(JNIEnv* env,
47                                                                                jclass /* clazz */) {
48   return env->GetVersion() == JNI_VERSION_1_6;
49 }
50 
51 JNIEXPORT jboolean JNICALL
Java_com_berberis_jnitests_JniTests_checkJavaVMCorrespondsToJNIEnv(JNIEnv * env,jclass)52 Java_com_berberis_jnitests_JniTests_checkJavaVMCorrespondsToJNIEnv(JNIEnv* env,
53                                                                    jclass /* clazz */) {
54   JavaVM* vm;
55   if (env->GetJavaVM(&vm) != JNI_OK) {
56     return false;
57   }
58 
59   void* env_copy;
60   if (vm->GetEnv(&env_copy, JNI_VERSION_1_6) != JNI_OK) {
61     return false;
62   }
63 
64   return env == env_copy;
65 }
66 
Java_com_berberis_jnitests_JniTests_callRegisterNatives(JNIEnv * env,jclass clazz)67 JNIEXPORT jboolean JNICALL Java_com_berberis_jnitests_JniTests_callRegisterNatives(JNIEnv* env,
68                                                                                    jclass clazz) {
69   JNINativeMethod methods[] = {{"add42", "(I)I", reinterpret_cast<void*>(&add42)}};
70   return env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[1])) == JNI_OK;
71 }
72 
Java_com_berberis_jnitests_JniTests_callAdd(JNIEnv * env,jclass clazz,jint x,jint y)73 JNIEXPORT jint JNICALL Java_com_berberis_jnitests_JniTests_callAdd(JNIEnv* env,
74                                                                    jclass clazz,
75                                                                    jint x,
76                                                                    jint y) {
77   jmethodID method_id = env->GetStaticMethodID(clazz, "add", "(II)I");
78   // ATTENTION: JNIEnv converts Call*Method(...) to Call*MethodV(va_list)!
79   return env->CallStaticIntMethod(clazz, method_id, x, y);
80 }
81 
Java_com_berberis_jnitests_JniTests_callAddA(JNIEnv * env,jclass clazz,jint x,jint y)82 JNIEXPORT jint JNICALL Java_com_berberis_jnitests_JniTests_callAddA(JNIEnv* env,
83                                                                     jclass clazz,
84                                                                     jint x,
85                                                                     jint y) {
86   jmethodID method_id = env->GetStaticMethodID(clazz, "add", "(II)I");
87   jvalue args[2];
88   args[0].i = x;
89   args[1].i = y;
90   return env->CallStaticIntMethodA(clazz, method_id, args);
91 }
92 
Java_com_berberis_jnitests_JniTests_callCallIntFromJNI(JNIEnv * env,jclass clazz)93 JNIEXPORT jint JNICALL Java_com_berberis_jnitests_JniTests_callCallIntFromJNI(JNIEnv* env,
94                                                                               jclass clazz) {
95   jmethodID method_id = env->GetStaticMethodID(clazz, "callIntFromJNI", "()I");
96   return env->CallStaticIntMethod(clazz, method_id);
97 }
98 
99 }  // extern "C"
100