Fix calling Java/TS methods that return strings
[ldk-java] / java_strings.py
index 82efd47b72be17330f3a27a09ee0c16007b9273c..b6b1f13fdaae7c1d9abe8f9afe68f9a09855a458 100644 (file)
@@ -44,7 +44,7 @@ public class bindings {
                try {
                        // Try to load natively first, this works on Android and in testing.
                        System.loadLibrary(\"lightningjni\");
-               } catch (UnsatisfiedLinkError _ignored) {
+               } catch (UnsatisfiedLinkError system_load_err) {
                        // Otherwise try to load from the library jar.
                        File tmpdir = new File(System.getProperty("java.io.tmpdir"), "ldk-java-nativelib");
                        tmpdir.mkdir(); // If it fails to create, assume it was there already
@@ -56,6 +56,9 @@ public class bindings {
                                Files.copy(is, libpath, StandardCopyOption.REPLACE_EXISTING);
                                Runtime.getRuntime().load(libpath.toString());
                        } catch (IOException e) {
+                               System.err.println("Failed to load LDK native library.");
+                               System.err.println("System LDK native library load failed with: " + system_load_err);
+                               System.err.println("Resource-based LDK native library load failed with: " + e);
                                throw new IllegalArgumentException(e);
                        }
                }
@@ -484,6 +487,7 @@ import javax.annotation.Nullable;
         self.ptr_native_ty = "long"
         self.result_c_ty = "jclass"
         self.ptr_arr = "jobjectArray"
+        self.is_arr_some_check = ("", " != NULL")
         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
 
     def construct_jenv(self):
@@ -845,10 +849,13 @@ import javax.annotation.Nullable;
                     out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
                 elif fn_line.ret_ty_info.c_ty == "void":
                     out_c += "\t(*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
+                elif fn_line.ret_ty_info.java_ty == "String":
+                    # Manually write out String methods as they're just an Object
+                    out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
                 elif not fn_line.ret_ty_info.passed_as_ptr:
                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
                 else:
-                    out_c = out_c + "\t" + fn_line.ret_ty_info.rust_obj + "* ret = (" + fn_line.ret_ty_info.rust_obj + "*)(*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
+                    out_c = out_c + "\tuint64_t ret = (*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
 
                 for idx, arg_info in enumerate(fn_line.args_ty):
                     if arg_info.ret_conv is not None:
@@ -1023,7 +1030,7 @@ import javax.annotation.Nullable;
             init_meth_params = ""
             init_meth_body = ""
             hu_conv_body = ""
-            for idx, field_ty in enumerate(var.fields):
+            for idx, (field_ty, field_docs) in enumerate(var.fields):
                 if idx > 0:
                     init_meth_params = init_meth_params + ", "
 
@@ -1034,7 +1041,12 @@ import javax.annotation.Nullable;
                     init_meth_params = init_meth_params + field_path + " " + field_ty.arg_name
                 else:
                     out_java += "\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n"
-                    java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
+                    if field_docs is not None:
+                        java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n"
+                    java_hu_subclasses += "\t\t"
+                    if field_ty.nullable:
+                        java_hu_subclasses += "@Nullable "
+                    java_hu_subclasses += "public final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
                     init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
                 init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
                 if field_ty.to_hu_conv is not None:
@@ -1068,7 +1080,7 @@ import javax.annotation.Nullable;
         for var in variant_list:
             out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
             c_params = []
-            for idx, field_map in enumerate(var.fields):
+            for idx, (field_map, field_docs) in enumerate(var.fields):
                 if field_map.ret_conv is not None:
                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
                     if var.tuple_variant:
@@ -1167,7 +1179,10 @@ import javax.annotation.Nullable;
                     else:
                         if arg.nullable:
                             out_java_struct += "@Nullable "
-                        out_java_struct += (arg.java_hu_ty + " " + arg.arg_name)
+                        ty_string = arg.java_hu_ty
+                        if arg.java_fn_ty_arg[0] == "L" and arg.java_fn_ty_arg[len(arg.java_fn_ty_arg) - 1] == ";":
+                            ty_string = arg.java_fn_ty_arg.strip("L;").replace("/", ".")
+                        out_java_struct += ty_string + " " + arg.arg_name
         out_java += (");\n")
         out_c += (") {\n")
         if out_java_struct is not None: