[Java] Expose the finalizer of traits, as some traits are locks
authorMatt Corallo <git@bluematt.me>
Wed, 8 Mar 2023 04:48:58 +0000 (04:48 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 8 Mar 2023 05:12:58 +0000 (05:12 +0000)
java_strings.py

index c44dca6503de951d422cca64ee5bc62a82f7c2ac..87ec4f86e1c5c58fd9969c3b0c82d5f5576d74be 100644 (file)
@@ -777,12 +777,26 @@ import javax.annotation.Nullable;
                     out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
             else:
                 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(" + var[1] + ");\n"
-        out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n"
-        out_java_trait = out_java_trait + "\t}\n"
-        out_java_trait = out_java_trait + "\t@Override @SuppressWarnings(\"deprecation\")\n"
-        out_java_trait = out_java_trait + "\tprotected void finalize() throws Throwable {\n"
-        out_java_trait = out_java_trait + "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n"
-        out_java_trait = out_java_trait + "\t}\n\n"
+        out_java_trait += f"""         this.bindings_instance = arg;
+       }}
+       @Override @SuppressWarnings("deprecation")
+       protected void finalize() throws Throwable {{
+               if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }} super.finalize();
+       }}
+       /**
+        * Destroys the object, freeing associated resources. After this call, any access
+        * to this object may result in a SEGFAULT or worse.
+        *
+        * You should generally NEVER call this method. You should let the garbage collector
+        * do this for you when it finalizes objects. However, it may be useful for types
+        * which represent locks and should be closed immediately to avoid holding locks
+        * until the GC runs.
+        */
+       public void destroy() {{
+               if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }}
+               ptr = 0;
+       }}
+"""
 
         java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
         java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"