Skip to content

Commit

Permalink
Fix and reformat in libcob/java.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nberth committed Nov 5, 2024
1 parent 17171e2 commit 478abf8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
2 changes: 2 additions & 0 deletions libcob/coblocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ COB_HIDDEN void cob_runtime_warning_ss (const char *, const char *);
COB_EXPIMP int cob_ncase_cmp (char *, const char *, unsigned );
COB_EXPIMP char * cob_str_case_str (char *, const char *);

COB_HIDDEN int cob_jni_init (cob_java_api *api);

/* static inline of smaller helpers */

static COB_INLINE int
Expand Down
105 changes: 52 additions & 53 deletions libcob/java.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
Copyright (C) 2024 Free Software Foundation, Inc.
Written by Vedant Tewari, Nicolas Berthier,
Copyright (C) 2024 Free Software Foundation, Inc.
Written by Vedant Tewari, Nicolas Berthier,
This file is part of GnuCOBOL.
This file is part of GnuCOBOL.
The GnuCOBOL runtime library is free software: you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The GnuCOBOL runtime library is free software: you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
GnuCOBOL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
GnuCOBOL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdio.h>
Expand All @@ -40,31 +40,30 @@ typedef struct __cob_java_static_method {

static int /* non-zero means there's an error */
jvm_load (void) {
/* JDK/JRE 6 VM initialization arguments */
JavaVMInitArgs args;
JavaVMOption* options;
/* JDK/JRE 6 VM initialization arguments */
JavaVMInitArgs args;
JavaVMOption* options = { 0, };
const char *classpath;
size_t option_len;
char option_buffer[COB_NORMAL_BUFF];

args.version = JNI_VERSION_1_6;
char *classpath = getenv("CLASSPATH");
if (classpath == NULL) {
classpath = "";
}
/* inline */
args.nOptions = 1;
option_len = strlen("-Djava.class.path=") + strlen(classpath) + 1;
if (option_len > sizeof(option_buffer)) {
return -1;
}
classpath = getenv ("CLASSPATH");
if (classpath == NULL) {
classpath = "";
}
args.nOptions = 1;
option_len = strlen("-Djava.class.path=") + strlen(classpath) + 1;
if (option_len > sizeof(option_buffer)) {
return -1;
}
strcpy(option_buffer, "-Djava.class.path=");
strcat(option_buffer, classpath);
strcat(option_buffer, classpath);
options[0].optionString = option_buffer;
args.options = options;
args.ignoreUnrecognized = 1;
/* loading and initializing a Java VM, returning as JNI interface */
return JNI_CreateJavaVM(&jvm, (void**)&env, &args);
args.options = options;
args.ignoreUnrecognized = 1;
/* loading and initializing a Java VM, returning as JNI interface */
return JNI_CreateJavaVM(&jvm, (void**)&env, &args);
}

static
Expand All @@ -80,26 +79,26 @@ resolve_java (const char *class_name,
cls = (*env)->FindClass(env, jni_class_name);
cob_free(jni_class_name);
if (!cls) {
cob_runtime_error(_("Java class '%s' not found"), class_name);
cob_add_exception(COB_EC_FUNCTION_NOT_FOUND);
return NULL;
cob_runtime_error (_("Java class '%s' not found"), class_name);
cob_set_exception (COB_EC_FUNCTION_NOT_FOUND);
return NULL;
}

mid = (*env)->GetStaticMethodID(env, cls, method_name, method_signature);
if (!mid) {
cob_runtime_error(_("Java method '%s' with signature '%s' not found in class '%s'"),
method_name, method_signature, class_name);
cob_runtime_error (_("Java method '%s' with signature '%s' not found in class '%s'"),
method_name, method_signature, class_name);
(*env)->DeleteLocalRef(env, cls);
cob_add_exception(COB_EC_OO_METHOD);
return NULL;
cob_set_exception (COB_EC_OO_METHOD);
return NULL;
}

handle = (cob_java_handle*)cob_malloc(sizeof(cob_java_handle));
if (!handle) {
cob_runtime_error(_("Memory allocation failed for Java method handle"));
(*env)->DeleteLocalRef(env, cls);
cob_add_exception(COB_EC_STORAGE_NOT_AVAIL);
return NULL;
cob_runtime_error (_("Memory allocation failed for Java method handle"));
(*env)->DeleteLocalRef (env, cls);
cob_set_exception (COB_EC_STORAGE_NOT_AVAIL);
return NULL;
}

handle->cls = (*env)->NewGlobalRef(env, cls);
Expand All @@ -121,31 +120,31 @@ call_java (const cob_java_handle *method_handle)
jthrowable exception = (*env)->ExceptionOccurred(env);
if(exception) {
jclass throwable = (*env)->FindClass(env, "java/lang/Throwable");
jmethodID getMessage = (*env)->GetMethodID(env,
throwable, "getMessage", "()Ljava/lang/String;");
jmethodID getMessage = (*env)->GetMethodID(env,
throwable, "getMessage", "()Ljava/lang/String;");
if(getMessage != NULL) {
jstring message = (jstring)(*env)->CallObjectMethod(env, exception, getMessage);
jstring message = (jstring)(*env)->CallObjectMethod(env, exception, getMessage);
const char *messageChars = (*env)->GetStringUTFChars(env, message, NULL);
cob_runtime_error(_("Java exception: %s"), messageChars);
(*env)->ReleaseStringUTFChars(env, message, messageChars);
(*env)->DeleteLocalRef(env, message);
}
jclass stringWriter = (*env)->FindClass(env, "java/io/StringWriter");
jclass printWriter = (*env)->FindClass(env, "java/io/PrintWriter");
jobject stringWriterObj = (*env)->NewObject(env,
stringWriter,
(*env)->GetMethodID(env, stringWriter, "<init>", "()V"));
jobject printWriterObj = (*env)->NewObject(env,
printWriter,
(*env)->GetMethodID(env, printWriter, "<init>", "(Ljava/io/Writer;)V"),
stringWriterObj);
jobject stringWriterObj = (*env)->NewObject(env,
stringWriter,
(*env)->GetMethodID(env, stringWriter, "<init>", "()V"));
jobject printWriterObj = (*env)->NewObject(env,
printWriter,
(*env)->GetMethodID(env, printWriter, "<init>", "(Ljava/io/Writer;)V"),
stringWriterObj);
jmethodID printStackTrace = (*env)->GetMethodID(env, throwable, "printStackTrace", "(Ljava/io/PrintWriter;)V");
(*env)->CallVoidMethod(env, exception, printStackTrace, printWriter);
jmethodID toString = (*env)->GetMethodID(env, stringWriter, "toString", "()Ljava/lang/String;");
jstring stackTrace = (jstring)(*env)->CallObjectMethod(env, stringWriterObj, toString);
const char *stackTraceChars = (*env)->GetStringUTFChars(env, stackTrace, NULL);
cob_runtime_error(_("Java stack trace: %s"), stackTraceChars);

(*env)->ReleaseStringUTFChars(env, stackTrace, stackTraceChars);
(*env)->DeleteLocalRef(env, stackTrace);
(*env)->DeleteLocalRef(env, stringWriterObj);
Expand Down

0 comments on commit 478abf8

Please sign in to comment.