< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp

Print this page
rev 52432 : 8213501: Deploy ExceptionJniWrapper for a few tests
Summary:
Reviewed-by:

@@ -21,143 +21,329 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 #include <stdlib.h>
+#include <iostream>
 
 #include "ExceptionCheckingJniEnv.hpp"
+#include "nsk_tools.h"
 
 namespace {
 
+static const char* get_dirname(const char* fullname) {
+  const char* p;
+  const char* base = fullname;;
+
+  if (fullname == NULL) {
+    return NULL;
+  }
+
+  for (p = fullname; *p != '\0'; p++) {
+    if (*p == '/' || *p == '\\') {
+      base = p + 1;
+    }
+  }
+  return base;
+}
+
 template<class T = void*>
 class JNIVerifier {
  public:
-  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg)
-      : _env(env), _base_msg(base_msg), _return_error(NULL) {
+  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg,
+              int line, const char* file)
+      : _env(env), _base_message(base_msg), _error_message(NULL),
+        _line(line), _file(get_dirname(file)) {
+  }
+
+  // Until C++11 is supported, we have to write multiple template constructors.
+  template <typename U>
+  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg,
+              U parameter,
+              int line, const char* file)
+      : _env(env), _base_message(base_msg), _error_message(NULL),
+        _line(line), _file(get_dirname(file)) {
+          PrintPreCall(parameter);
+  }
+
+  template <typename U, typename V>
+  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg,
+              U parameter1,
+              V parameter2,
+              int line, const char* file)
+      : _env(env), _base_message(base_msg), _error_message(NULL),
+        _line(line), _file(get_dirname(file)) {
+          PrintPreCall(parameter1, parameter2);
+  }
+
+  template <typename U, typename V, typename W>
+  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg,
+              U parameter1, V parameter2, W parameter3,
+              int line, const char* file)
+      : _env(env), _base_message(base_msg), _error_message(NULL),
+        _line(line), _file(get_dirname(file)) {
+          PrintPreCall(parameter1, parameter2, parameter3);
   }
 
   ~JNIVerifier() {
+    PrintPostCall();
+
     JNIEnv* jni_env = _env->GetJNIEnv();
     if (jni_env->ExceptionCheck()) {
-      _env->HandleError(_base_msg);
-      return;
+      _error_message = "internal error";
     }
 
-    if (_return_error != NULL) {
-      ProcessReturnError();
+    if (_error_message != NULL) {
+      GenerateErrorMessage();
     }
   }
 
-  void ProcessReturnError() {
-    int len = snprintf(NULL, 0, "%s : %s", _base_msg, _return_error) + 1;
+  void GenerateErrorMessage() {
+    int len = snprintf(NULL, 0, "JNI method %s : %s from %s:%d", _base_message, _error_message,
+                       _file, _line) + 1;
 
     if (len <= 0) {
-      _env->HandleError(_return_error);
+      _env->HandleError(_error_message);
       return;
     }
 
     char* full_message = (char*) malloc(len);
     if (full_message == NULL) {
-      _env->HandleError(_return_error);
+      _env->HandleError(_error_message);
       return;
     }
 
-    snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
+    snprintf(full_message, len, "JNI method %s : %s from %s:%d", _base_message, _error_message,
+             _file, _line);
 
     _env->HandleError(full_message);
     free(full_message);
   }
 
   T ResultNotNull(T ptr) {
     if (ptr == NULL) {
-      _return_error = "Return is NULL";
+      _error_message = "Return is NULL";
     }
     return ptr;
   }
 
+  T ResultIsZero(T value) {
+    if (value != 0) {
+      _error_message = "Return is not zero";
+    }
+    return value;
+  }
+
+  void PrintPreCallHeader() {
+    if (!nsk_getVerboseMode()) {
+      return;
+    }
+
+    std::cout << ">> Calling JNI method " << _base_message << " from " << _file
+              << ":" << _line << std::endl;
+    std::cout << ">> Calling with these parameter(s):" << std::endl;
+  }
+
+  // Until C++11 is supported, we have to write multiple PrintPreCall.
+  template<class U>
+  void PrintPreCall(U first_parameter) {
+    if (!nsk_getVerboseMode()) {
+      return;
+    }
+
+    PrintPreCallHeader();
+    std::cout << "\t" << first_parameter << std::endl;
+  }
+
+  template<class U, class V>
+  void PrintPreCall(U parameter1, V parameter2) {
+    if (!nsk_getVerboseMode()) {
+      return;
+    }
+
+    PrintPreCallHeader();
+    std::cout << "\t" << parameter1 << std::endl;
+    std::cout << "\t" << parameter2 << std::endl;
+  }
+
+  template<class U, class V, class W>
+  void PrintPreCall(U parameter1, V parameter2, W parameter3) {
+    if (!nsk_getVerboseMode()) {
+      return;
+    }
+
+    PrintPreCallHeader();
+    std::cout << "\t" << parameter1 << std::endl;
+    std::cout << "\t" << parameter2 << std::endl;
+    std::cout << "\t" << parameter3 << std::endl;
+  }
+
+  void PrintPostCall() {
+    if (!nsk_getVerboseMode()) {
+      return;
+    }
+
+    std::cout << "<< Called JNI method " << _base_message << " from " << _file
+              << ":" << _line << std::endl;
+  }
+
  private:
   ExceptionCheckingJniEnv* _env;
-  const char* const _base_msg;
-  const char* _return_error;
+  const char* const _base_message;
+  const char* _error_message;
+  int _line;
+  const char* const _file;
 };
 
 }
 
-jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj) {
-  JNIVerifier<jclass> marker(this, "GetObjectClass");
+jclass ExceptionCheckingJniEnv::FindClass(const char *class_name,
+                                          int line, const char* file_name) {
+  JNIVerifier<jclass> marker(this, "FindClass", class_name, line, file_name);
+  return marker.ResultNotNull(_jni_env->FindClass(class_name));
+}
+
+jint ExceptionCheckingJniEnv::RegisterNatives(jclass clazz,
+                                              const JNINativeMethod *methods,
+                                              jint nMethods,
+                                              int line,
+                                              const char* file_name) {
+  JNIVerifier<jint> marker(this, "RegisterNatives", methods, nMethods, line, file_name);
+  return marker.ResultIsZero(_jni_env->RegisterNatives(clazz, methods, nMethods));
+}
+
+jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj, int line,
+                                               const char* file_name) {
+  JNIVerifier<jclass> marker(this, "GetObjectClass", obj, line, file_name);
   return marker.ResultNotNull(_jni_env->GetObjectClass(obj));
 }
 
-jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name, const char* type) {
-  JNIVerifier<jfieldID> marker(this, "GetFieldID");
+jfieldID ExceptionCheckingJniEnv::GetStaticFieldID(jclass klass, const char *name,
+                                                   const char* type,
+                                                   int line, const char* file_name) {
+  JNIVerifier<jfieldID> marker(this, "GetStaticFieldID", klass, name, type,
+                               line, file_name);
+  return marker.ResultNotNull(_jni_env->GetStaticFieldID(klass, name, type));
+}
+
+jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name,
+                                             const char* type,
+                                             int line, const char* file_name) {
+  JNIVerifier<jfieldID> marker(this, "GetFieldID", klass, name, type, line, file_name);
   return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type));
 }
 
-jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field) {
-  JNIVerifier<jobject> marker(this, "GetObjectField");
+jobject ExceptionCheckingJniEnv::GetStaticObjectField(jclass klass, jfieldID field,
+                                                      int line, const char* file_name) {
+  JNIVerifier<jobject> marker(this, "GetStaticObjectField", klass, field,
+                              line, file_name);
+  return marker.ResultNotNull(_jni_env->GetStaticObjectField(klass, field));
+}
+
+jobject ExceptionCheckingJniEnv::GetObjectField(jobject obj, jfieldID field,
+                                                int line, const char* file_name) {
+  JNIVerifier<jobject> marker(this, "GetObjectField", obj, field, line, file_name);
   return marker.ResultNotNull(_jni_env->GetObjectField(obj, field));
 }
 
-void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value) {
-  JNIVerifier<> marker(this, "SetObjectField");
+void ExceptionCheckingJniEnv::SetObjectField(jobject obj, jfieldID field, jobject value,
+                                             int line, const char* file_name) {
+  JNIVerifier<> marker(this, "SetObjectField", obj, field, value, line, file_name);
   _jni_env->SetObjectField(obj, field, value);
 }
 
-jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj) {
-  JNIVerifier<jobject> marker(this, "NewGlobalRef");
+jobject ExceptionCheckingJniEnv::NewGlobalRef(jobject obj, int line, const char* file_name) {
+  JNIVerifier<jobject> marker(this, "NewGlobalRef", obj, line, file_name);
   return marker.ResultNotNull(_jni_env->NewGlobalRef(obj));
 }
 
-void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj) {
-  JNIVerifier<> marker(this, "DeleteGlobalRef");
+void ExceptionCheckingJniEnv::DeleteGlobalRef(jobject obj, int line, const char* file_name) {
+  JNIVerifier<> marker(this, "DeleteGlobalRef", obj, line, file_name);
   _jni_env->DeleteGlobalRef(obj);
 }
 
-jobject ExceptionCheckingJniEnv::NewLocalRef(jobject obj) {
-  JNIVerifier<jobject> marker(this, "NewLocalRef");
+jobject ExceptionCheckingJniEnv::NewLocalRef(jobject obj, int line, const char* file_name) {
+  JNIVerifier<jobject> marker(this, "NewLocalRef", obj, line, file_name);
   return marker.ResultNotNull(_jni_env->NewLocalRef(obj));
 }
 
-void ExceptionCheckingJniEnv::DeleteLocalRef(jobject obj) {
-  JNIVerifier<> marker(this, "DeleteLocalRef");
+void ExceptionCheckingJniEnv::DeleteLocalRef(jobject obj, int line, const char* file_name) {
+  JNIVerifier<> marker(this, "DeleteLocalRef", obj, line, file_name);
   _jni_env->DeleteLocalRef(obj);
 }
 
-jweak ExceptionCheckingJniEnv::NewWeakGlobalRef(jobject obj) {
-  JNIVerifier<jweak> marker(this, "NewWeakGlobalRef");
+jweak ExceptionCheckingJniEnv::NewWeakGlobalRef(jobject obj, int line, const char* file_name) {
+  JNIVerifier<jweak> marker(this, "NewWeakGlobalRef", obj, line, file_name);
   return marker.ResultNotNull(_jni_env->NewWeakGlobalRef(obj));
 }
 
-void ExceptionCheckingJniEnv::DeleteWeakGlobalRef(jweak weak_ref) {
-  JNIVerifier<> marker(this, "DeleteWeakGlobalRef");
+void ExceptionCheckingJniEnv::DeleteWeakGlobalRef(jweak weak_ref, int line, const char* file_name) {
+  JNIVerifier<> marker(this, "DeleteWeakGlobalRef", weak_ref, line, file_name);
   _jni_env->DeleteWeakGlobalRef(weak_ref);
 }
 
-jsize ExceptionCheckingJniEnv::GetArrayLength(jarray array) {
-  JNIVerifier<> marker(this, "GetArrayLength");
+jsize ExceptionCheckingJniEnv::GetArrayLength(jarray array, int line, const char* file_name) {
+  JNIVerifier<> marker(this, "GetArrayLength", array, line, file_name);
   return _jni_env->GetArrayLength(array);
 }
 
-jsize ExceptionCheckingJniEnv::GetStringLength(jstring str) {
-  JNIVerifier<> marker(this, "GetStringLength");
+jsize ExceptionCheckingJniEnv::GetStringLength(jstring str, int line, const char* file_name) {
+  JNIVerifier<> marker(this, "GetStringLength", str, line, file_name);
   return _jni_env->GetStringLength(str);
 }
 
-void* ExceptionCheckingJniEnv::GetPrimitiveArrayCritical(jarray array, jboolean* isCopy) {
-  JNIVerifier<> marker(this, "GetPrimitiveArrayCritical");
-  return marker.ResultNotNull(_jni_env->GetPrimitiveArrayCritical(array, isCopy));
+void* ExceptionCheckingJniEnv::GetPrimitiveArrayCritical(jarray array, jboolean* is_copy,
+                                                         int line, const char* file_name) {
+  JNIVerifier<> marker(this, "GetPrimitiveArrayCritical", array, is_copy, line, file_name);
+  return marker.ResultNotNull(_jni_env->GetPrimitiveArrayCritical(array, is_copy));
 }
 
-void ExceptionCheckingJniEnv::ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode) {
-  JNIVerifier<> marker(this, "ReleasePrimitiveArrayCritical");
+void ExceptionCheckingJniEnv::ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode,
+                                                            int line, const char* file_name) {
+  JNIVerifier<> marker(this, "ReleasePrimitiveArrayCritical", array, carray, mode,
+                       line, file_name);
   _jni_env->ReleasePrimitiveArrayCritical(array, carray, mode);
 }
 
-const jchar* ExceptionCheckingJniEnv::GetStringCritical(jstring str, jboolean* isCopy) {
-  JNIVerifier<const jchar*> marker(this, "GetPrimitiveArrayCritical");
-  return marker.ResultNotNull(_jni_env->GetStringCritical(str, isCopy));
+const jchar* ExceptionCheckingJniEnv::GetStringCritical(jstring str, jboolean* is_copy,
+                                                        int line, const char* file_name) {
+  JNIVerifier<const jchar*> marker(this, "GetPrimitiveArrayCritical", str, is_copy,
+                                   line, file_name);
+  return marker.ResultNotNull(_jni_env->GetStringCritical(str, is_copy));
 }
 
-void ExceptionCheckingJniEnv::ReleaseStringCritical(jstring str, const jchar* carray) {
-  JNIVerifier<> marker(this, "ReleaseStringCritical");
+void ExceptionCheckingJniEnv::ReleaseStringCritical(jstring str, const jchar* carray,
+                                                    int line, const char* file_name) {
+  JNIVerifier<> marker(this, "ReleaseStringCritical", str, carray, line, file_name);
   _jni_env->ReleaseStringCritical(str, carray);
 }
+
+jbyte* ExceptionCheckingJniEnv::GetByteArrayElements(jbyteArray array, jboolean* is_copy,
+                                                   int line, const char* file_name) {
+  JNIVerifier<jbyte*> marker(this, "GetByteArrayElements", array, is_copy, line, file_name);
+  return marker.ResultNotNull(_jni_env->GetByteArrayElements(array, is_copy));
+}
+
+void ExceptionCheckingJniEnv::ReleaseByteArrayElements(jbyteArray array, jbyte* byte_array, jint mode,
+                                                       int line, const char* file_name) {
+  JNIVerifier<> marker(this, "ReleaseByteArrayElements", array, byte_array, mode,
+                       line, file_name);
+  _jni_env->ReleaseByteArrayElements(array, byte_array, mode);
+}
+
+jmethodID ExceptionCheckingJniEnv::GetMethodID(jclass klass, const char* name, const char* sig,
+                                               int line, const char* file_name) {
+  JNIVerifier<jmethodID> marker(this, "GetMethodID", klass, name, sig, line, file_name);
+  return marker.ResultNotNull(_jni_env->GetMethodID(klass, name, sig));
+}
+
+jobject ExceptionCheckingJniEnv::NewObject(jclass klass, jmethodID methodID,
+                                           int line, const char* file_name, ...) {
+  // In the case of NewObject, we miss the extra arguments passed to NewObject sadly.
+  JNIVerifier<jobject> marker(this, "NewObject", klass, methodID, line, file_name);
+
+  va_list args;
+  va_start(args, file_name);
+  jobject result = marker.ResultNotNull(_jni_env->NewObjectV(klass, methodID, args));
+  va_end(args);
+  return result;
+}
< prev index next >