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   jsize GetArrayLength(jarray array);
  70   jsize GetStringLength(jstring str);
  71 
  72   void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy);
  73   void ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode);
  74   const jchar* GetStringCritical(jstring str, jboolean* isCopy);
  75   void ReleaseStringCritical(jstring str, const jchar* carray);
  76 
  77   jobject NewGlobalRef(jobject obj);
  78   void DeleteGlobalRef(jobject obj);
  79   jobject NewLocalRef(jobject ref);
  80   void DeleteLocalRef(jobject ref);
  81   jweak NewWeakGlobalRef(jobject obj);
  82   void DeleteWeakGlobalRef(jweak obj);
  83 
  84   // ExceptionCheckingJniEnv methods.
  85   JNIEnv* GetJNIEnv() {
  86     return _jni_env;
  87   }
  88 
  89   void HandleError(const char* msg) {
  90     if (_error_handler) {
  91       _error_handler(_jni_env, msg);
  92     }
  93   }
  94 
  95   typedef void (*ErrorHandler)(JNIEnv* env, const char* error_message);
  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   ExceptionCheckingJniEnv(JNIEnv* jni_env, ErrorHandler error_handler) :
 105     _jni_env(jni_env), _error_handler(error_handler) {}
 106 
 107  private:
 108   JNIEnv* _jni_env;
 109   ErrorHandler _error_handler;
 110 };
 111 
 112 // We cannot use unique_ptr due to this being gnu98++, so use this instead:
 113 class ExceptionCheckingJniEnvPtr {
 114  private:
 115   ExceptionCheckingJniEnv _env;
 116 
 117  public:
 118   ExceptionCheckingJniEnv* operator->() {
 119     return &_env;
 120   }
 121 
 122   ExceptionCheckingJniEnvPtr(
 123       JNIEnv* jni_env,
 124       ExceptionCheckingJniEnv::ErrorHandler error_handler = ExceptionCheckingJniEnv::FatalError) :
 125           _env(jni_env, error_handler) {
 126   }
 127 };
 128 
 129 #endif