1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018, 2019, 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 #define TRACE_JNI_CALL_VARARGS(...) __LINE__, __FILE__, __VA_ARGS__ 63 64 class ExceptionCheckingJniEnv { 65 public: 66 // JNIEnv API redefinitions. 67 jclass FindClass(const char *name, int line, const char* file_name); 68 69 jfieldID GetStaticFieldID(jclass klass, const char* name, const char* type, 70 int line, const char* file_name); 71 jfieldID GetFieldID(jclass klass, const char* name, const char* type, 72 int line, const char* file_name); 73 jmethodID GetStaticMethodID(jclass klass, const char* name, const char* sig, 74 int line, const char* file_name); 75 jmethodID GetMethodID(jclass klass, const char* name, const char* sig, 76 int line, const char* file_name); 77 78 jclass GetObjectClass(jobject obj, int line, const char* file_name); 79 jobject GetObjectField(jobject obj, jfieldID field, int line, const char* file_name); 80 jobject GetStaticObjectField(jclass kls, jfieldID field, int line, const char* file_name); 81 void SetObjectField(jobject obj, jfieldID field, jobject value, 82 int line, const char* file_name); 83 84 jsize GetArrayLength(jarray array, int line, const char* file_name); 85 jsize GetStringLength(jstring str, int line, const char* file_name); 86 87 void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy, 88 int line, const char* file_name); 89 void ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode, 90 int line, const char* file_name); 91 const jchar* GetStringCritical(jstring str, jboolean* isCopy, 92 int line, const char* file_name); 93 void ReleaseStringCritical(jstring str, const jchar* carray, 94 int line, const char* file_name); 95 96 jbyte* GetByteArrayElements(jbyteArray array, jboolean* isCopy, 97 int line, const char* file_name); 98 void ReleaseByteArrayElements(jbyteArray array, jbyte* byte_array, jint mode, 99 int line, const char* file_name); 100 jint RegisterNatives(jclass clazz, const JNINativeMethod *methods, jint nMethods, 101 int line, const char* file_name); 102 103 jobject NewObject(jclass kls, jmethodID methodID, 104 int line, const char* file_name, ...); 105 jobject NewGlobalRef(jobject obj, int line, const char* file_name); 106 void DeleteGlobalRef(jobject obj, int line, const char* file_name); 107 jobject NewLocalRef(jobject ref, int line, const char* file_name); 108 void DeleteLocalRef(jobject ref, int line, const char* file_name); 109 jweak NewWeakGlobalRef(jobject obj, int line, const char* file_name); 110 void DeleteWeakGlobalRef(jweak obj, int line, const char* file_name); 111 112 jboolean IsSameObject(jobject ref1, jobject ref2, int line, 113 const char* file_name); 114 115 jobject CallObjectMethod(jobject obj, jmethodID methodID, int line, 116 const char* file_name, ...); 117 void CallVoidMethod(jobject obj, jmethodID methodID, int line, 118 const char* file_name, ...); 119 120 // ExceptionCheckingJniEnv methods. GetJNIEnv()121 JNIEnv* GetJNIEnv() { 122 return _jni_env; 123 } 124 HandleError(const char * msg)125 void HandleError(const char* msg) { 126 if (_error_handler) { 127 _error_handler(_jni_env, msg); 128 } 129 } 130 131 typedef void (*ErrorHandler)(JNIEnv* env, const char* error_message); 132 FatalError(JNIEnv * env,const char * message)133 static void FatalError(JNIEnv* env, const char* message) { 134 if (env->ExceptionCheck()) { 135 env->ExceptionDescribe(); 136 } 137 env->FatalError(message); 138 } 139 ExceptionCheckingJniEnv(JNIEnv * jni_env,ErrorHandler error_handler)140 ExceptionCheckingJniEnv(JNIEnv* jni_env, ErrorHandler error_handler) : 141 _jni_env(jni_env), _error_handler(error_handler) {} 142 143 private: 144 JNIEnv* _jni_env; 145 ErrorHandler _error_handler; 146 }; 147 148 // We cannot use unique_ptr due to this being gnu98++, so use this instead: 149 class ExceptionCheckingJniEnvPtr { 150 private: 151 ExceptionCheckingJniEnv _env; 152 153 public: operator ->()154 ExceptionCheckingJniEnv* operator->() { 155 return &_env; 156 } 157 ExceptionCheckingJniEnvPtr(JNIEnv * jni_env,ExceptionCheckingJniEnv::ErrorHandler error_handler=ExceptionCheckingJniEnv::FatalError)158 ExceptionCheckingJniEnvPtr( 159 JNIEnv* jni_env, 160 ExceptionCheckingJniEnv::ErrorHandler error_handler = ExceptionCheckingJniEnv::FatalError) : 161 _env(jni_env, error_handler) { 162 } 163 }; 164 165 #endif 166