< prev index next >

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

Print this page
rev 52293 : [mq]: verify_deprecation

@@ -21,23 +21,37 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 #include <stdlib.h>
+#include <iostream>
 
 #include "ExceptionCheckingJniEnv.hpp"
 
 namespace {
 
 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 = -1, const char* file = 0)
+      : _env(env), _base_msg(base_msg), _return_error(NULL),
+        _line(line), _file(file) {
+  }
+
+  template <typename U>
+  JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_msg,
+              U first_parameter,
+              int line = -1, const char* file = 0)
+      : _env(env), _base_msg(base_msg), _return_error(NULL),
+        _line(line), _file(file) {
+          printPreCall<U>(first_parameter);
   }
 
   ~JNIVerifier() {
+    printPostCall();
+
     JNIEnv* jni_env = _env->GetJNIEnv();
     if (jni_env->ExceptionCheck()) {
       _env->HandleError(_base_msg);
       return;
     }

@@ -46,11 +60,12 @@
       ProcessReturnError();
     }
   }
 
   void ProcessReturnError() {
-    int len = snprintf(NULL, 0, "%s : %s", _base_msg, _return_error) + 1;
+    int len = snprintf(NULL, 0, "JNI method %s : %s from %s:%d", _base_msg, _return_error,
+                       _file, _line) + 1;
 
     if (len <= 0) {
       _env->HandleError(_return_error);
       return;
     }

@@ -59,11 +74,12 @@
     if (full_message == NULL) {
       _env->HandleError(_return_error);
       return;
     }
 
-    snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
+    snprintf(full_message, len, "JNI method %s : %s from %s:%d", _base_msg, _return_error,
+             _file, _line);
 
     _env->HandleError(full_message);
     free(full_message);
   }
 

@@ -72,23 +88,48 @@
       _return_error = "Return is NULL";
     }
     return ptr;
   }
 
+  void printPreCallHeader() {
+    std::cout << ">> Calling JNI method " << _base_msg << " from " << _file
+              << ":" << _line << std::endl;
+    std::cout << ">> Calling with these parameter(s):" << std::endl;
+  }
+
+  template<class U>
+  void printPreCall(U first_parameter) {
+    printPreCallHeader();
+    std::cout << "\t" << first_parameter << std::endl;
+  }
+
+  void printPostCall() {
+    std::cout << "<< Called JNI method " << _base_msg << " from " << _file
+              << ":" << _line << std::endl;
+  }
+
  private:
   ExceptionCheckingJniEnv* _env;
   const char* const _base_msg;
   const char* _return_error;
+  int _line;
+  const char* const _file;
 };
 
 }
 
 jclass ExceptionCheckingJniEnv::GetObjectClass(jobject obj) {
   JNIVerifier<jclass> marker(this, "GetObjectClass");
   return marker.ResultNotNull(_jni_env->GetObjectClass(obj));
 }
 
+jclass ExceptionCheckingJniEnv::FindClass(const char* kls, int line, const char* file) {
+  JNIVerifier<jclass> marker(this, "FindClass", kls, line, file);
+  // Force a failure to see error messages...
+  return marker.ResultNotNull(NULL); //_jni_env->FindClass(kls));
+}
+
 jfieldID ExceptionCheckingJniEnv::GetFieldID(jclass klass, const char *name, const char* type) {
   JNIVerifier<jfieldID> marker(this, "GetFieldID");
   return marker.ResultNotNull(_jni_env->GetFieldID(klass, name, type));
 }
 
< prev index next >