From 478abf834a40ddebaba3cf900447de218797eb8a Mon Sep 17 00:00:00 2001 From: Nicolas Berthier Date: Tue, 5 Nov 2024 16:02:31 +0100 Subject: [PATCH] Fix and reformat in `libcob/java.c` --- libcob/coblocal.h | 2 + libcob/java.c | 105 +++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/libcob/coblocal.h b/libcob/coblocal.h index 71be6fcd8..55c8d5465 100644 --- a/libcob/coblocal.h +++ b/libcob/coblocal.h @@ -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 diff --git a/libcob/java.c b/libcob/java.c index e414ebef4..34e70101a 100644 --- a/libcob/java.c +++ b/libcob/java.c @@ -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 . + You should have received a copy of the GNU Lesser General Public License + along with GnuCOBOL. If not, see . */ #include @@ -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 @@ -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); @@ -121,10 +120,10 @@ 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); @@ -132,20 +131,20 @@ call_java (const cob_java_handle *method_handle) } 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, "", "()V")); - jobject printWriterObj = (*env)->NewObject(env, - printWriter, - (*env)->GetMethodID(env, printWriter, "", "(Ljava/io/Writer;)V"), - stringWriterObj); + jobject stringWriterObj = (*env)->NewObject(env, + stringWriter, + (*env)->GetMethodID(env, stringWriter, "", "()V")); + jobject printWriterObj = (*env)->NewObject(env, + printWriter, + (*env)->GetMethodID(env, printWriter, "", "(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);