annotate test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp @ 52098:c9e901ad4c8f

8211432: [REDO] Handle JNIGlobalRefLocker.cpp Summary: Adding a JNI verification wrapper for tests Reviewed-by: amenkov, sspitsyn, phh
author jcbeyler
date Thu, 11 Oct 2018 09:30:10 -0700
parents
children 04d4f1e4aff2
rev   line source
jcbeyler@52098 1 /*
jcbeyler@52098 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
jcbeyler@52098 3 * Copyright (c) 2018, Google and/or its affiliates. All rights reserved.
jcbeyler@52098 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jcbeyler@52098 5 *
jcbeyler@52098 6 * This code is free software; you can redistribute it and/or modify it
jcbeyler@52098 7 * under the terms of the GNU General Public License version 2 only, as
jcbeyler@52098 8 * published by the Free Software Foundation.
jcbeyler@52098 9 *
jcbeyler@52098 10 * This code is distributed in the hope that it will be useful, but WITHOUT
jcbeyler@52098 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jcbeyler@52098 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jcbeyler@52098 13 * version 2 for more details (a copy is included in the LICENSE file that
jcbeyler@52098 14 * accompanied this code).
jcbeyler@52098 15 *
jcbeyler@52098 16 * You should have received a copy of the GNU General Public License version
jcbeyler@52098 17 * 2 along with this work; if not, write to the Free Software Foundation,
jcbeyler@52098 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jcbeyler@52098 19 *
jcbeyler@52098 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jcbeyler@52098 21 * or visit www.oracle.com if you need additional information or have any
jcbeyler@52098 22 * questions.
jcbeyler@52098 23 */
jcbeyler@52098 24
jcbeyler@52098 25 #include "ExceptionCheckingJniEnv.hpp"
jcbeyler@52098 26
jcbeyler@52098 27 namespace {
jcbeyler@52098 28
jcbeyler@52098 29 template<class T = void*>
jcbeyler@52098 30 class JNIVerifier {
jcbeyler@52098 31 public:
jcbeyler@52098 32 JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg)
jcbeyler@52098 33 : _env(env), _base_msg(base_msg), _return_error(NULL) {
jcbeyler@52098 34 }
jcbeyler@52098 35
jcbeyler@52098 36 ~JNIVerifier() {
jcbeyler@52098 37 JNIEnv* jni_env = _env->GetJNIEnv();
jcbeyler@52098 38 if (jni_env->ExceptionCheck()) {
jcbeyler@52098 39 _env->HandleError(_base_msg);
jcbeyler@52098 40 return;
jcbeyler@52098 41 }
jcbeyler@52098 42
jcbeyler@52098 43 if (_return_error != NULL) {
jcbeyler@52098 44 ProcessReturnError();
jcbeyler@52098 45 }
jcbeyler@52098 46 }
jcbeyler@52098 47
jcbeyler@52098 48 void ProcessReturnError() {
jcbeyler@52098 49 int len = snprintf(NULL, 0, "%s : %s", _base_msg, _return_error) + 1;
jcbeyler@52098 50
jcbeyler@52098 51 if (len <= 0) {
jcbeyler@52098 52 _env->HandleError(_return_error);
jcbeyler@52098 53 return;
jcbeyler@52098 54 }
jcbeyler@52098 55
jcbeyler@52098 56 char* full_message = (char*) malloc(len);
jcbeyler@52098 57 if (full_message == NULL) {
jcbeyler@52098 58 _env->HandleError(_return_error);
jcbeyler@52098 59 return;
jcbeyler@52098 60 }
jcbeyler@52098 61
jcbeyler@52098 62 snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
jcbeyler@52098 63
jcbeyler@52098 64 _env->HandleError(full_message);
jcbeyler@52098 65 free(full_message);
jcbeyler@52098 66 }
jcbeyler@52098 67
jcbeyler@52098 68 T ResultNotNull(T ptr) {
jcbeyler@52098 69 if (ptr == NULL) {
jcbeyler@52098 70 _return_error = "Return is NULL";
jcbeyler@52098 71 }
jcbeyler@52098 72 return ptr;
jcbeyler@52098 73 }
jcbeyler@52098 74
jcbeyler@52098 75 private:
jcbeyler@52098 76 ExceptionCheckingJniEnv* _env;
jcbeyler@52098 77 const char* const _base_msg;
jcbeyler@52098 78 const char* _return_error;
jcbeyler@52098 79 };
jcbeyler@52098 80
jcbeyler@52098 81 }
jcbeyler@52098 82
jcbeyler@52098 83 jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj) {
jcbeyler@52098 84 JNIVerifier<jclass> marker(this, "GetObjectClass");
jcbeyler@52098 85 return marker.ResultNotNull(_jni_env->GetObjectClass(obj));
jcbeyler@52098 86 }
jcbeyler@52098 87
jcbeyler@52098 88 jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name, const char* type) {
jcbeyler@52098 89 JNIVerifier<jfieldID> marker(this, "GetObjectClass");
jcbeyler@52098 90 return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type));
jcbeyler@52098 91 }
jcbeyler@52098 92
jcbeyler@52098 93 jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field) {
jcbeyler@52098 94 JNIVerifier<jobject> marker(this, "GetObjectField");
jcbeyler@52098 95 return marker.ResultNotNull(_jni_env->GetObjectField(obj, field));
jcbeyler@52098 96 }
jcbeyler@52098 97
jcbeyler@52098 98 void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value) {
jcbeyler@52098 99 JNIVerifier<> marker(this, "SetObjectField");
jcbeyler@52098 100 _jni_env->SetObjectField(obj, field, value);
jcbeyler@52098 101 }
jcbeyler@52098 102
jcbeyler@52098 103 jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj) {
jcbeyler@52098 104 JNIVerifier<jobject> marker(this, "GetObjectField");
jcbeyler@52098 105 return marker.ResultNotNull(_jni_env->NewGlobalRef(obj));
jcbeyler@52098 106 }
jcbeyler@52098 107
jcbeyler@52098 108 void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj) {
jcbeyler@52098 109 JNIVerifier<> marker(this, "DeleteGlobalRef");
jcbeyler@52098 110 _jni_env->DeleteGlobalRef(obj);
jcbeyler@52098 111 }