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, TRACE_JNI_CALL);
  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  *  Note the TRACE_JNI_CALL parameter that allows to trace where the call is
  55  *  happening from for debugging.
  56  *
  57  *  By default, the error handler describes the exception via the JNI
  58  *  ExceptionDescribe method and calls FatalError.
  59  */
  60 
  61 #define TRACE_JNI_CALL __LINE__, __FILE__
  62 
  63 class ExceptionCheckingJniEnv {
  64  public:
  65   // JNIEnv API redefinitions.
  66   jclass FindClass(const char *name, int line, const char* file_name);
  67 
  68   jfieldID GetStaticFieldID(jclass klass, const char* name, const char* type,
  69                             int line, const char* file_name);
  70   jfieldID GetFieldID(jclass klass, const char* name, const char* type,
  71                       int line, const char* file_name);
  72   jmethodID GetMethodID(jclass klass, const char* name, const char* sig,
  73                         int line, const char* file_name);
  74 
  75   jclass GetObjectClass(jobject obj, int line, const char* file_name);
  76   jobject GetObjectField(jobject obj, jfieldID field, int line, const char* file_name);
  77   jobject GetStaticObjectField(jclass kls, jfieldID field, int line, const char* file_name);
  78   void SetObjectField(jobject obj, jfieldID field, jobject value,
  79                       int line, const char* file_name);
  80 
  81   jsize GetArrayLength(jarray array, int line, const char* file_name);
  82   jsize GetStringLength(jstring str, int line, const char* file_name);
  83 
  84   void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy,
  85                                   int line, const char* file_name);
  86   void ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode,
  87                                      int line, const char* file_name);
  88   const jchar* GetStringCritical(jstring str, jboolean* isCopy,
  89                                  int line, const char* file_name);
  90   void ReleaseStringCritical(jstring str, const jchar* carray,
  91                              int line, const char* file_name);
  92 
  93   jbyte* GetByteArrayElements(jbyteArray array, jboolean* isCopy,
  94                               int line, const char* file_name);
  95   void ReleaseByteArrayElements(jbyteArray array, jbyte* byte_array, jint mode,
  96                                 int line, const char* file_name);
  97   jint RegisterNatives(jclass clazz, const JNINativeMethod *methods, jint nMethods,
  98                        int line, const char* file_name);
  99 
 100   jobject NewObject(jclass kls, jmethodID methodID,
 101                     int line, const char* file_name, ...);
 102   jobject NewGlobalRef(jobject obj, int line, const char* file_name);
 103   void DeleteGlobalRef(jobject obj, int line, const char* file_name);
 104   jobject NewLocalRef(jobject ref, int line, const char* file_name);
 105   void DeleteLocalRef(jobject ref, int line, const char* file_name);
 106   jweak NewWeakGlobalRef(jobject obj, int line, const char* file_name);
 107   void DeleteWeakGlobalRef(jweak obj, int line, const char* file_name);
 108 
 109   // ExceptionCheckingJniEnv methods.
 110   JNIEnv* GetJNIEnv() {
 111     return _jni_env;
 112   }
 113 
 114   void HandleError(const char* msg) {
 115     if (_error_handler) {
 116       _error_handler(_jni_env, msg);
 117     }
 118   }
 119 
 120   typedef void (*ErrorHandler)(JNIEnv* env, const char* error_message);
 121 
 122   static void FatalError(JNIEnv* env, const char* message) {
 123     if (env->ExceptionCheck()) {
 124       env->ExceptionDescribe();
 125     }
 126     env->FatalError(message);
 127   }
 128 
 129   ExceptionCheckingJniEnv(JNIEnv* jni_env, ErrorHandler error_handler) :
 130     _jni_env(jni_env), _error_handler(error_handler) {}
 131 
 132  private:
 133   JNIEnv* _jni_env;
 134   ErrorHandler _error_handler;
 135 };
 136 
 137 // We cannot use unique_ptr due to this being gnu98++, so use this instead:
 138 class ExceptionCheckingJniEnvPtr {
 139  private:
 140   ExceptionCheckingJniEnv _env;
 141 
 142  public:
 143   ExceptionCheckingJniEnv* operator->() {
 144     return &_env;
 145   }
 146 
 147   ExceptionCheckingJniEnvPtr(
 148       JNIEnv* jni_env,
 149       ExceptionCheckingJniEnv::ErrorHandler error_handler = ExceptionCheckingJniEnv::FatalError) :
 150           _env(jni_env, error_handler) {
 151   }
 152 };
 153 
 154 #endif