1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include <jni.h>
  25 
  26 static jmethodID _callable_method_id;
  27 static jmethodID _callable_nested_method_id;
  28 
  29 static jmethodID get_method_id(JNIEnv *env, jclass clz, jstring jname, jstring jsig) {
  30   jmethodID mid;
  31   const char *name, *sig;
  32   name = (*env)->GetStringUTFChars(env, jname, NULL);
  33   sig  = (*env)->GetStringUTFChars(env, jsig, NULL);
  34   mid  = (*env)->GetMethodID(env, clz, name, sig);
  35   (*env)->ReleaseStringUTFChars(env, jname, name);
  36   (*env)->ReleaseStringUTFChars(env, jsig, sig);
  37   return mid;
  38 }
  39 
  40 JNIEXPORT void JNICALL
  41 Java_TestCheckedJniExceptionCheck_initMethodIds(JNIEnv *env,
  42                                                 jobject obj,
  43                                                 jstring callable_method_name,
  44                                                 jstring callable_method_sig,
  45                                                 jstring callable_nested_method_name,
  46                                                 jstring callable_nested_method_sig) {
  47   jclass clz = (*env)->GetObjectClass(env, obj);
  48 
  49   _callable_method_id = get_method_id(env, clz,
  50                                       callable_method_name,
  51                                       callable_method_sig);
  52 
  53   _callable_nested_method_id = get_method_id(env, clz,
  54                                              callable_nested_method_name,
  55                                              callable_nested_method_sig);
  56 }
  57 
  58 JNIEXPORT void JNICALL
  59 Java_TestCheckedJniExceptionCheck_callJavaFromNative(JNIEnv *env,
  60                                                      jobject obj,
  61                                                      jint nofCalls,
  62                                                      jboolean checkExceptions) {
  63   int i;
  64   for (i = 0; i < nofCalls; i++) {
  65     (*env)->CallVoidMethod(env, obj, _callable_method_id);
  66     if (checkExceptions == JNI_TRUE) {
  67       (*env)->ExceptionCheck(env);
  68     }
  69   }
  70 }
  71 
  72 JNIEXPORT void JNICALL
  73 Java_TestCheckedJniExceptionCheck_callNestedJavaFromNative(JNIEnv *env,
  74                                                            jobject obj,
  75                                                            jint nofCalls,
  76                                                            jboolean checkExceptions) {
  77   int i;
  78   for (i = 0; i < nofCalls; i++) {
  79     (*env)->CallVoidMethod(env, obj, _callable_nested_method_id, nofCalls, checkExceptions);
  80     if (checkExceptions == JNI_TRUE) {
  81       (*env)->ExceptionCheck(env);
  82     }
  83   }
  84 }