[Java] Print error stack trace when tests fail
[ldk-java] / java_strings.py
index abe038d0f403a74bb0ba2735818d087921b3248d..7e35d17529d0e8ea127c25e104b0f73996a8f3da 100644 (file)
@@ -72,6 +72,7 @@ public class bindings {
                // Fetching the LDK versions from C also checks that the header and binaries match
                System.err.println("Loaded LDK-Java Bindings " + version.get_ldk_java_bindings_version() + " with LDK " + get_ldk_version() + " and LDK-C-Bindings " + get_ldk_c_bindings_version());
        }
                // Fetching the LDK versions from C also checks that the header and binaries match
                System.err.println("Loaded LDK-Java Bindings " + version.get_ldk_java_bindings_version() + " with LDK " + get_ldk_version() + " and LDK-C-Bindings " + get_ldk_c_bindings_version());
        }
+       public static void run_statics() { /* Useful to force the statics to run */ }
        static native void init(java.lang.Class c);
        static native void init_class_cache();
        static native String get_lib_version_string();
        static native void init(java.lang.Class c);
        static native void init_class_cache();
        static native String get_lib_version_string();
@@ -184,6 +185,34 @@ class CommonBase {
        }
 }"""
 
        }
 }"""
 
+        self.witness_program_defn = """public class WitnessProgram extends CommonBase {
+       /** The witness program bytes themselves */
+       public final byte[] program;
+       /** The witness version */
+       public final WitnessVersion version;
+
+       WitnessProgram(java.lang.Object _dummy, long ptr) {
+               super(ptr);
+               this.program = bindings.WitnessProgram_get_program(ptr);
+               this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
+       }
+       static byte check_args(byte[] program, WitnessVersion version) {
+               if (program.length < 2 || program.length > 40) throw new IllegalArgumentException();
+               if (version.getVal() == 0 && program.length != 20 && program.length != 32) throw new IllegalArgumentException();
+               return version.getVal();
+       }
+       public WitnessProgram(byte[] program, WitnessVersion version) {
+               super(bindings.WitnessProgram_new(check_args(program, version), program));
+               this.program = bindings.WitnessProgram_get_program(ptr);
+               this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
+       }
+
+       @Override @SuppressWarnings(\"deprecation\")
+       protected void finalize() throws Throwable {
+               super.finalize();
+               if (ptr != 0) { bindings.WitnessProgram_free(ptr); }
+       }
+}"""
 
         self.c_file_pfx = """#include <jni.h>
 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
 
         self.c_file_pfx = """#include <jni.h>
 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
@@ -627,7 +656,7 @@ import javax.annotation.Nullable;
         self.usize_c_ty = "int64_t"
         self.usize_native_ty = "long"
         self.native_zero_ptr = "0"
         self.usize_c_ty = "int64_t"
         self.usize_native_ty = "long"
         self.native_zero_ptr = "0"
-        self.result_c_ty = "jclass"
+        self.unitary_enum_c_ty = "jclass"
         self.ptr_arr = "jobjectArray"
         self.is_arr_some_check = ("", " != NULL")
         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
         self.ptr_arr = "jobjectArray"
         self.is_arr_some_check = ("", " != NULL")
         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
@@ -833,7 +862,7 @@ import javax.annotation.Nullable;
             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
             ord_v = ord_v + 1
         out_java_enum = out_java_enum + "\t; static native void init();\n"
             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
             ord_v = ord_v + 1
         out_java_enum = out_java_enum + "\t; static native void init();\n"
-        out_java_enum = out_java_enum + "\tstatic { init(); }\n"
+        out_java_enum = out_java_enum + "\tstatic { org.ldk.impl.bindings.run_statics(); init(); }\n"
         out_java_enum = out_java_enum + "}"
         out_java = out_java + "\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n"
         out_c += "\t}\n"
         out_java_enum = out_java_enum + "}"
         out_java = out_java + "\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n"
         out_c += "\t}\n"
@@ -1361,7 +1390,7 @@ import javax.annotation.Nullable;
         out_opaque_struct_human += self.hu_struct_file_prefix
         out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n"
         out_opaque_struct_human += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
         out_opaque_struct_human += self.hu_struct_file_prefix
         out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n"
         out_opaque_struct_human += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
-        hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
+        hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC4Tuple", "FourTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
         out_opaque_struct_human += ("public class " + hu_name + " extends CommonBase")
         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
             out_opaque_struct_human += (" implements AutoCloseable")
         out_opaque_struct_human += ("public class " + hu_name + " extends CommonBase")
         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
             out_opaque_struct_human += (" implements AutoCloseable")
@@ -1389,6 +1418,9 @@ import javax.annotation.Nullable;
         java_hu_struct += "\tprotected void finalize() throws Throwable {\n"
         java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n"
         java_hu_struct += "\t}\n\n"
         java_hu_struct += "\tprotected void finalize() throws Throwable {\n"
         java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n"
         java_hu_struct += "\t}\n\n"
+        java_hu_struct += "\tprotected void force_free() {\n" # Used by NioPeerHandler
+        java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); ptr = 0; }\n"
+        java_hu_struct += "\t}\n\n"
         java_hu_struct += "\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n"
         java_hu_struct += "\t\tif (bindings." + struct_name.replace("LDK", "") + "_is_ok(ptr)) {\n"
         java_hu_struct += "\t\t\treturn new " + human_ty + "_OK(null, ptr);\n"
         java_hu_struct += "\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n"
         java_hu_struct += "\t\tif (bindings." + struct_name.replace("LDK", "") + "_is_ok(ptr)) {\n"
         java_hu_struct += "\t\t\treturn new " + human_ty + "_OK(null, ptr);\n"