Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion jq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,52 @@ jq_exception_clear()
return;
}

/*
* Helper Method from JNIHelp.c from android source code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you modify the comment for functions following the format below?

/*
 * function name: description about its functionality
 */

*/
static jmethodID FindMethod(JNIEnv* env,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you naming the function following the naming rule? For example, jq_find_method

const char* className,
const char* methodName,
const char* descriptor) {
// This method is only valid for classes in the core library which are

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cover the comment using /* */

// not unloaded during the lifetime of managed code execution.
jclass clazz = (*env)->FindClass(env, className);
jmethodID methodId = (*env)->GetMethodID(env, clazz, methodName, descriptor);
(*env)->DeleteLocalRef(env, clazz);
return methodId;
}

/*
* Helper Method from JNIHelp.c from android source code
*/
static jobject NewStringWriter(JNIEnv* env) {
jclass clazz = (*env)->FindClass(env, "java/io/StringWriter");
jmethodID init = (*env)->GetMethodID(env, clazz, "<init>", "()V");
jobject instance = (*env)->NewObject(env, clazz, init);
(*env)->DeleteLocalRef(env, clazz);
return instance;
}

/*
* Helper Method from JNIHelp.c from android source code
*/
static jstring StringWriterToString(JNIEnv* env, jobject stringWriter) {
jmethodID toString =
FindMethod(env, "java/io/StringWriter", "toString", "()Ljava/lang/String;");
return (jstring) (*env)->CallObjectMethod(env, stringWriter, toString);
}

/*
* Helper Method from JNIHelp.c from android source code
*/
static jobject NewPrintWriter(JNIEnv* env, jobject writer) {
jclass clazz = (*env)->FindClass(env, "java/io/PrintWriter");
jmethodID init = (*env)->GetMethodID(env, clazz, "<init>", "(Ljava/io/Writer;)V");
jobject instance = (*env)->NewObject(env, clazz, init, writer);
(*env)->DeleteLocalRef(env, clazz);
return instance;
}

/*
* jq_get_exception: get the JNI exception is currently being thrown convert
* to String for ouputing error message
Expand All @@ -1290,7 +1336,9 @@ jq_get_exception()
jclass objectClass;
jstring exceptionMsg;
char *exceptionString;
char *exceptionStackTraceString;
char *err_msg = NULL;
char *stackTrace_msg = NULL;

/* determines if an exception is being thrown */
exc = (*Jenv)->ExceptionOccurred(Jenv);
Expand All @@ -1300,12 +1348,43 @@ jq_get_exception()
{
ereport(ERROR, (errmsg("java/lang/Object class could not be created")));
}
// Stack Trace

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a new function, for example jq_print_stack_trace(), to cover L1351 - L1381

{
jobject sw = NewStringWriter(Jenv);
if (sw == NULL) {
ereport(ERROR, (errmsg("String Writer is null")));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because our purpose is to print stack trace at debug level (DEBUG3), so I think this error should only be displayed if DEBUG3 level is set in postgresql.conf.

}

jobject pw = NewPrintWriter(Jenv, sw);
if (pw == NULL) {
(*Jenv)->DeleteLocalRef(Jenv, sw);
ereport(ERROR, (errmsg("Trace is null")));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message should be 'Print Writer is null'

}

jmethodID printStackTrace =
FindMethod(Jenv, "java/lang/Throwable", "printStackTrace", "(Ljava/io/PrintWriter;)V");
(*Jenv)->CallVoidMethod(Jenv, exc, printStackTrace, pw);

jstring trace = StringWriterToString(Jenv, sw);
(*Jenv)->DeleteLocalRef(Jenv, pw);
pw = NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to set 'sw' and 'pw' to NULL.

(*Jenv)->DeleteLocalRef(Jenv, sw);
sw = NULL;

if (trace == NULL) {
ereport(ERROR, (errmsg("Trace is null!!!")));
}

exceptionStackTraceString = jdbc_convert_string_to_cstring((jobject) trace);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the ident in this line of code.

stackTrace_msg = pstrdup(exceptionStackTraceString);
ereport(DEBUG3, (errmsg("%s", stackTrace_msg)));
}
exceptionMsgID = (*Jenv)->GetMethodID(Jenv, objectClass, "toString", "()Ljava/lang/String;");
exceptionMsg = (jstring) (*Jenv)->CallObjectMethod(Jenv, exc, exceptionMsgID);
exceptionString = jdbc_convert_string_to_cstring((jobject) exceptionMsg);
err_msg = pstrdup(exceptionString);
ereport(ERROR, (errmsg("remote server returned an error")));
ereport(DEBUG3, (errmsg("%s", err_msg)));
ereport(ERROR, (errmsg("remote server returned an error")));
}
return;
}
Expand Down