1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Google and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 #ifndef NSK_EXCEPTIONCHECKINGJNIENV_DEFINED
  25 #define NSK_EXCEPTIONCHECKINGJNIENV_DEFINED
  26 
  27 #include <jni.h>
  28 
  29 /**
  30  * ExceptionCheckingJniEnv wraps around the JNIEnv data structure and
  31  * methods to enable automatic exception checking. This allows test writers
  32  * and readers to concentrate on what the test is to do and leave the
  33  * error checking and throwing to this data structure and subsystem.
  34  *
  35  * For example:
  36  *
  37  * ... JNIEnv* env ...
  38  *  jclass klass = env->GetObjectClass(o);
  39  *  if (klass == NULL) {
  40  *      printf("Error: GetObjectClass returned NULL\n");
  41  *      return;
  42  *  }
  43  *  if (env->ExceptionCheck()) {
  44  *    ...
  45  *  }
  46  *
  47  *  Can be simplified to:
  48  * ... ExceptionCheckingJniEnv* env ...
  49  *  jclass klass = env->GetObjectClass(o);
  50  *
  51  *  Where now the JNI Exception checking and the NULL return checking are done
  52  *  internally and will perform whatever action the ErrorHandler requires.
  53  *
  54  *  By default, the error handler describes the exception via the JNI
  55  *  ExceptionDescribe method and calls FatalError.
  56  *
  57  *  Note: at a future date, this will also include the tracing mechanism done in
  58  *  NSK_VERIFY, which will thus embed its logic into the ExceptionCheckingJniEnv
  59  *  and clearing that up for the code readers and writers.
  60  */
  61 class ExceptionCheckingJniEnv {
  62  public:
  63   // JNIEnv API redefinitions.
  64   jfieldID GetFieldID(jclass klass, const char *name, const char* type);
  65   jclass GetObjectClass(jobject obj);
  66   jobject GetObjectField(jobject obj, jfieldID field);
  67   void SetObjectField(jobject obj, jfieldID field, jobject value);
  68 
  69   jobject NewGlobalRef(jobject obj);
  70   void DeleteGlobalRef(jobject obj);
  71 
  72   // ExceptionCheckingJniEnv methods.
  73   JNIEnv* GetJNIEnv() {
  74     return _jni_env;
  75   }
  76 
  77   void HandleError(const char* msg) {
  78     if (_error_handler) {
  79       _error_handler(_jni_env, msg);
  80     }
  81   }
  82 
  83   typedef void (*ErrorHandler)(JNIEnv* env, const char* error_message);
  84 
  85   static void FatalError(JNIEnv* env, const char* message) {
  86     if (env->ExceptionCheck()) {
  87       env->ExceptionDescribe();
  88     }
  89     env->FatalError(message);
  90   }
  91 
  92   ExceptionCheckingJniEnv(JNIEnv* jni_env, ErrorHandler error_handler) :
  93     _jni_env(jni_env), _error_handler(error_handler) {}
  94 
  95  private:
  96   JNIEnv* _jni_env;
  97   ErrorHandler _error_handler;
  98 };
  99 
 100 // We cannot use unique_ptr due to this being gnu98++, so use this instead:
 101 class ExceptionCheckingJniEnvPtr {
 102  private:
 103   ExceptionCheckingJniEnv _env;
 104 
 105  public:
 106   ExceptionCheckingJniEnv* operator->() {
 107     return &_env;
 108   }
 109 
 110   ExceptionCheckingJniEnvPtr(
 111       JNIEnv* jni_env,
 112       ExceptionCheckingJniEnv::ErrorHandler error_handler = ExceptionCheckingJniEnv::FatalError) :
 113           _env(jni_env, error_handler) {
 114   }
 115 };
 116 
 117 #endif