< prev index next >
test/hotspot/jtreg/vmTestbase/nsk/share/jni/ExceptionCheckingJniEnv.cpp
Print this page
rev 52828 : 8213501: Deploy ExceptionJniWrapper for a few tests
Summary:
Reviewed-by:
rev 52829 : [mq]: new_version
@@ -22,163 +22,412 @@
* questions.
*/
#include <stdlib.h>
#include <string.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_message,
+ int line, const char* file)
+ : _env(env), _base_message(base_message), _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_message,
+ U parameter,
+ int line, const char* file)
+ : _env(env), _base_message(base_message), _error_message(NULL),
+ _line(line), _file(get_dirname(file)) {
+ PrintPreCall(parameter);
+ }
+
+ template <typename U, typename V>
+ JNIVerifier(ExceptionCheckingJniEnv *env, const char* base_message,
+ U parameter1,
+ V parameter2,
+ int line, const char* file)
+ : _env(env), _base_message(base_message), _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_message,
+ U parameter1, V parameter2, W parameter3,
+ int line, const char* file)
+ : _env(env), _base_message(base_message), _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);
+ if (jni_env->ExceptionCheck() && !_error_message) {
+ _error_message = "internal error";
+ }
+
+ if (_error_message != NULL) {
+ GenerateErrorMessage();
+ }
+ }
+
+ int DecimalToAsciiRec(char *str, long line) {
+ if (line == 0) {
+ return 0;
+ }
+
+ int remainder = line % 10;
+ long quotient = line / 10;
+
+ int pos = DecimalToAsciiRec(str, quotient);
+ str[pos] = '0' + remainder;
+ return pos + 1;
+ }
+
+ // Implementing a simple version of sprintf for "%d"...
+ void DecimalToAscii(char *str, int line) {
+ // Go to long so that the INT_MIN case can be handled seemlessly.
+ long internal_line = line;
+ if (internal_line == 0) {
+ str[0] = '0';
+ str[1] = '\0';
return;
}
- if (_return_error != NULL) {
- ProcessReturnError();
+ if (internal_line < 0) {
+ *str = '-';
+ internal_line *= -1;
+ str++;
}
+
+ str[DecimalToAsciiRec(str, internal_line)] = '\0';
}
- void ProcessReturnError() {
+ void GenerateErrorMessage() {
// This is error prone, but:
// - Seems like we cannot use std::string (due to windows/solaris not
// building when used, seemingly due to exception libraries not linking).
// - Seems like we cannot use sprintf due to VS2013 (JDK-8213622).
//
// We are aiming to do:
- // 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);
// but will use strlen + memcpy instead.
- size_t base_len = strlen(_base_msg);
+ const char* pre_message = "JNI method ";
const char* between_msg = " : ";
- size_t between_len = strlen(between_msg);
- size_t return_len = strlen(_return_error);
+ const char* from_msg = " from ";
+
+ const char* file_name = _file ? _file : "Unknown File";
+ const char* strs[] = {
+ pre_message,
+ _base_message,
+ between_msg,
+ _error_message,
+ from_msg,
+ file_name,
+ between_msg,
+ };
+
+ size_t msg_number = sizeof(strs) / sizeof(strs[0]);
+ size_t len = 0;
+ for (size_t i = 0; i < msg_number; i++) {
+ len += strlen(strs[i]);
+ }
- // +1 for the '\0'
- size_t len = base_len + between_len + return_len + 1;
+ // 32-bit signed means 11 characters due to the '-'.
+ const int MAX_INTEGER_DIGITS = 11;
+ // Add for the line number and 1 for the '\0'.
+ len += MAX_INTEGER_DIGITS + 1;
char* full_message = (char*) malloc(len);
if (full_message == NULL) {
- _env->HandleError(_return_error);
+ _env->HandleError(_error_message);
return;
}
- // Now we construct the string using memcpy to not use sprintf/std::string
+ // Now we construct the string using strncat to not use sprintf/std::string
// instead of:
- // snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
- memcpy(full_message, _base_msg, base_len);
- memcpy(full_message + base_len, between_msg, between_len);
- memcpy(full_message + base_len + between_len, _return_error, return_len);
- full_message[len - 1] = '\0';
-
- // -1 due to the '\0' not counted by strlen but is counted for the allocation.
- if (strlen(full_message) != len - 1) {
+ // snprintf(full_message, len, "JNI method %s : %s from %s:%d", _base_message,
+ // _error_message, _file, _line);
+ full_message[0] = '\0';
+ size_t current_len = 0;
+ for (size_t i = 0; i < msg_number; i++) {
+ size_t current_src_len = strlen(strs[i]);
+ current_len += current_src_len;
+ if (current_len >= len) {
_env->GetJNIEnv()->FatalError("Length of message is not what was expected");
}
+ strncat(full_message, strs[i], current_src_len);
+ }
+
+ // 10 is the max for an integer transformation.
+ if (current_len + MAX_INTEGER_DIGITS >= len) {
+ _env->GetJNIEnv()->FatalError("Length of message is not what was expected with line");
+ }
+
+ DecimalToAscii(full_message + current_len, _line);
+
+ if (strlen(full_message) >= len) {
+ _env->GetJNIEnv()->FatalError("Final length of message is not what was expected");
+ }
+
_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 >