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 25 #include <string> 26 27 #include "ExceptionCheckingJniEnv.hpp" 28 29 namespace { 30 31 template<class T = void*> 32 class JNIVerifier { 33 public: 34 JNIVerifier(ExceptionCheckingJniEnv *env, const std::string& base_msg) 35 : _env(env), _base_msg(base_msg) { 36 } 37 38 ~JNIVerifier() { 39 JNIEnv* jni_env = _env->GetJNIEnv(); 40 if (jni_env->ExceptionCheck()) { 41 _env->HandleError(_base_msg.c_str()); 42 } else { 43 if (!_return_error.empty()) { 44 const std::string msg = _base_msg + ":" + _return_error; 45 _env->HandleError(msg.c_str()); 46 } 47 } 48 } 49 50 T ResultNotNull(T ptr) { 51 if (ptr == NULL) { 52 _return_error = "Return is NULL"; 53 } 54 return ptr; 55 } 56 57 private: 58 ExceptionCheckingJniEnv* _env; 59 std::string _base_msg; 60 std::string _return_error; 61 }; 62 63 } 64 65 jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj) { 66 JNIVerifier<jclass> marker(this, "GetObjectClass"); 67 return marker.ResultNotNull(_jni_env->GetObjectClass(obj)); 68 } 69 70 jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name, const char* type) { 71 JNIVerifier<jfieldID> marker(this, "GetObjectClass"); 72 return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type)); 73 } 74 75 jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field) { 76 JNIVerifier<jobject> marker(this, "GetObjectField"); 77 return marker.ResultNotNull(_jni_env->GetObjectField(obj, field)); 78 } 79 80 void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value) { 81 JNIVerifier<> marker(this, "SetObjectField"); 82 _jni_env->SetObjectField(obj, field, value); 83 } 84 85 jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj) { 86 JNIVerifier<jobject> marker(this, "GetObjectField"); 87 return marker.ResultNotNull(_jni_env->NewGlobalRef(obj)); 88 } 89 90 void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj) { 91 JNIVerifier<> marker(this, "DeleteGlobalRef"); 92 _jni_env->DeleteGlobalRef(obj); 93 }