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 <memory>
  28 
  29 #include <jni.h>
  30 
  31 class ExceptionCheckingJniEnv;
  32 typedef std::unique_ptr<ExceptionCheckingJniEnv> ExceptionCheckingJniEnvPtr;
  33 
  34 /**
  35  * ExceptionCheckingJniEnv wraps around the JNIEnv data structure and
  36  * methods to enable automatic exception checking. This allows test writers
  37  * and readers to concentrate on what the test is to do and leave the
  38  * error checking and throwing to this data structure and subsystem.
  39  *
  40  * For example:
  41  *
  42  * ... JNIEnv* env ...
  43  *  jclass klass = env->GetObjectClass(o);
  44  *  if (klass == NULL) {
  45  *      printf("Error: GetObjectClass returned NULL\n");
  46  *      return;
  47  *  }
  48  *  if (env->ExceptionCheck()) {
  49  *    ...
  50  *  }
  51  *
  52  *  Can be simplified to:
  53  * ... ExceptionCheckingJniEnv* env ...
  54  *  jclass klass = env->GetObjectClass(o);
  55  *
  56  *  Where now the JNI Exception checking and the NULL return checking are done
  57  *  internally and will perform whatever action the ErrorHandler requires.
  58  *
  59  *  By default, the error handler describes the exception via the JNI
  60  *  ExceptionDescribe method and calls FatalError.
  61  *
  62  *  Note: at a future date, this will also include the tracing mechanism done in
  63  *  NSK_VERIFY, which will thus embed its logic into the ExceptionCheckingJniEnv
  64  *  and clearing that up for the code readers and writers.
  65  */
  66 class ExceptionCheckingJniEnv {
  67  public:
  68   // JNIEnv API redefinitions.
  69   jfieldID GetFieldID(jclass klass, const char *name, const char* type);
  70   jclass GetObjectClass(jobject obj);
  71   jobject GetObjectField(jobject obj, jfieldID field);
  72   void SetObjectField(jobject obj, jfieldID field, jobject value);
  73 
  74   jobject NewGlobalRef(jobject obj);
  75   void DeleteGlobalRef(jobject obj);
  76 
  77   // ExceptionCheckingJniEnv methods.
  78   JNIEnv* GetJNIEnv() {
  79     return _jni_env;
  80   }
  81 
  82   void HandleError(const char* msg) {
  83     if (_error_handler) {
  84       _error_handler(_jni_env, msg);
  85     }
  86   }
  87 
  88   typedef void (*ErrorHandler)(JNIEnv* env, const char* error_message);
  89 
  90   static ExceptionCheckingJniEnvPtr Wrap(JNIEnv* jni_env,
  91                             ErrorHandler handler = FatalError) {
  92     return std::unique_ptr<ExceptionCheckingJniEnv> {
  93       new ExceptionCheckingJniEnv(jni_env, handler)
  94     };
  95   }
  96 
  97   static void FatalError(JNIEnv* env, const char* message) {
  98     if (env->ExceptionCheck()) {
  99       env->ExceptionDescribe();
 100     }
 101     env->FatalError(message);
 102   }
 103 
 104  private:
 105   JNIEnv* _jni_env;
 106   ErrorHandler _error_handler;
 107 
 108   ExceptionCheckingJniEnv(JNIEnv* jni_env, ErrorHandler error_handler) :
 109     _jni_env(jni_env), _error_handler(error_handler) {}
 110 };
 111 
 112 #endif