+ if out_java_struct is not None:
+ out_java_struct.write("\t\t")
+ if ret_info.java_ty != "void":
+ out_java_struct.write(ret_info.java_ty + " ret = ")
+ out_java_struct.write("bindings." + re_match.group(2) + "(")
+ for idx, info in enumerate(arg_names):
+ if idx != 0:
+ out_java_struct.write(", ")
+ if info.arg_name == "this_ptr" or info.arg_name == "this_arg":
+ out_java_struct.write("this.ptr")
+ elif info.arg_name in default_constructor_args:
+ out_java_struct.write("bindings." + info.java_hu_ty + "_new(")
+ for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
+ if explode_idx != 0:
+ out_java_struct.write(", ")
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ out_java_struct.write(explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
+ out_java_struct.write(")")
+ elif info.from_hu_conv is not None:
+ out_java_struct.write(info.from_hu_conv[0])
+ else:
+ out_java_struct.write(info.arg_name)
+ out_java_struct.write(");\n")
+ if ret_info.to_hu_conv is not None:
+ out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
+
+ for info in arg_names:
+ if info.arg_name == "this_ptr" or info.arg_name == "this_arg":
+ pass
+ elif info.arg_name in default_constructor_args:
+ for explode_arg in default_constructor_args[info.arg_name]:
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ out_java_struct.write("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name, expl_arg_name).replace("this", ret_info.to_hu_conv_name) + ";\n")
+ elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
+ if ret_info.rust_obj == "LDK" + struct_meth and ret_info.to_hu_conv_name is not None:
+ out_java_struct.write("\t\t" + info.from_hu_conv[1].replace("this", ret_info.to_hu_conv_name) + ";\n")
+ else:
+ out_java_struct.write("\t\t" + info.from_hu_conv[1] + ";\n")
+
+ if ret_info.to_hu_conv_name is not None:
+ out_java_struct.write("\t\treturn " + ret_info.to_hu_conv_name + ";\n")
+ elif ret_info.java_ty != "void" and ret_info.rust_obj != "LDK" + struct_meth:
+ out_java_struct.write("\t\treturn ret;\n")
+ out_java_struct.write("\t}\n\n")
+ out_java_struct.close()
+
+ def map_unitary_enum(struct_name, field_lines):
+ with open(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum:
+ out_java_enum.write("package org.ldk.enums;\n\n")
+ unitary_enums.add(struct_name)
+ out_c.write("static inline " + struct_name + " " + struct_name + "_from_java(JNIEnv *env, jclass val) {\n")
+ out_c.write("\tswitch ((*env)->CallIntMethod(env, val, ordinal_meth)) {\n")
+ ord_v = 0
+ for idx, struct_line in enumerate(field_lines):
+ if idx == 0:
+ out_java_enum.write("public enum " + struct_name + " {\n")
+ elif idx == len(field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(field_lines) - 2:
+ out_java_enum.write("\t; static native void init();\n")
+ out_java_enum.write("\tstatic { init(); }\n")
+ out_java_enum.write("}")
+ out_java.write("\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n")
+ elif idx == len(field_lines) - 1:
+ assert(struct_line == "")
+ else:
+ out_java_enum.write(struct_line + "\n")
+ out_c.write("\t\tcase %d: return %s;\n" % (ord_v, struct_line.strip().strip(",")))
+ ord_v = ord_v + 1
+ out_c.write("\t}\n")
+ out_c.write("\tabort();\n")
+ out_c.write("}\n")
+
+ ord_v = 0
+ out_c.write("static jclass " + struct_name + "_class = NULL;\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ out_c.write("static jfieldID " + struct_name + "_" + variant + " = NULL;\n")
+ out_c.write("JNIEXPORT void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass clz) {\n")
+ out_c.write("\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n")
+ out_c.write("\tCHECK(" + struct_name + "_class != NULL);\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ out_c.write("\t" + struct_name + "_" + variant + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
+ out_c.write("\tCHECK(" + struct_name + "_" + variant + " != NULL);\n")
+ out_c.write("}\n")
+ out_c.write("static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n")
+ out_c.write("\tswitch (val) {\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ out_c.write("\t\tcase " + variant + ":\n")
+ out_c.write("\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + variant + ");\n")
+ ord_v = ord_v + 1
+ out_c.write("\t\tdefault: abort();\n")
+ out_c.write("\t}\n")
+ out_c.write("}\n\n")
+
+ def map_complex_enum(struct_name, union_enum_items):
+ java_hu_type = struct_name.replace("LDK", "")
+ complex_enums.add(struct_name)
+ with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
+ out_java_enum.write(hu_struct_file_prefix)
+ out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
+ out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
+ out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
+ out_java_enum.write("\tprotected void finalize() throws Throwable {\n")
+ out_java_enum.write("\t\tsuper.finalize();\n")
+ out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n")
+ out_java_enum.write("\t}\n")
+ out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
+ out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
+ java_hu_subclasses = ""
+
+ tag_field_lines = union_enum_items["field_lines"]
+ init_meth_jty_strs = {}
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx == 0:
+ out_java.write("\tpublic static class " + struct_name + " {\n")
+ out_java.write("\t\tprivate " + struct_name + "() {}\n")
+ elif idx == len(tag_field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(tag_field_lines) - 2:
+ out_java.write("\t\tstatic native void init();\n")
+ out_java.write("\t}\n")
+ elif idx == len(tag_field_lines) - 1:
+ assert(struct_line == "")
+ else:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
+ java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
+ out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
+ out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n")
+ out_c.write("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
+ out_c.write("static jmethodID " + struct_name + "_" + var_name + "_meth = NULL;\n")
+ init_meth_jty_str = ""
+ init_meth_params = ""
+ init_meth_body = ""
+ hu_conv_body = ""
+ if "LDK" + var_name in union_enum_items:
+ enum_var_lines = union_enum_items["LDK" + var_name]
+ for idx, field in enumerate(enum_var_lines):
+ if idx != 0 and idx < len(enum_var_lines) - 2:
+ field_ty = map_type(field.strip(' ;'), False, None, False, True)
+ out_java.write("\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_ty.to_hu_conv is not None:
+ hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
+ hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
+ hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
+ else:
+ hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
+ init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
+ if idx > 1:
+ init_meth_params = init_meth_params + ", "
+ 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 + "; "
+ out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
+ out_java.write(init_meth_body)
+ out_java.write("}\n")
+ out_java.write("\t\t}\n")
+ out_java_enum.write("\t\t}\n")
+ java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
+ java_hu_subclasses = java_hu_subclasses + hu_conv_body
+ java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n"
+ init_meth_jty_strs[var_name] = init_meth_jty_str
+ out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
+ out_java_enum.write(java_hu_subclasses)
+ out_java.write("\tstatic { " + struct_name + ".init(); }\n")
+ out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
+
+ out_c.write("JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass _a) {\n")
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx != 0 and idx < len(tag_field_lines) - 3:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ out_c.write("\t" + struct_name + "_" + var_name + "_class =\n")
+ out_c.write("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
+ out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
+ out_c.write("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
+ out_c.write("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
+ out_c.write("}\n")
+ out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
+ out_c.write("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+ out_c.write("\tswitch(obj->tag) {\n")
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx != 0 and idx < len(tag_field_lines) - 3:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ out_c.write("\t\tcase " + struct_name + "_" + var_name + ": {\n")
+ c_params_text = ""
+ if "LDK" + var_name in union_enum_items:
+ enum_var_lines = union_enum_items["LDK" + var_name]
+ for idx, field in enumerate(enum_var_lines):
+ if idx != 0 and idx < len(enum_var_lines) - 2:
+ field_map = map_type(field.strip(' ;'), False, None, False, True)
+ if field_map.ret_conv is not None:
+ out_c.write("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
+ out_c.write("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
+ out_c.write(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
+ c_params_text = c_params_text + ", " + field_map.ret_conv_name
+ else:
+ c_params_text = c_params_text + ", obj->" + camel_to_snake(var_name) + "." + field_map.arg_name
+ out_c.write("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
+ out_c.write("\t\t}\n")
+ out_c.write("\t\tdefault: abort();\n")
+ out_c.write("\t}\n}\n")
+ out_java_enum.write("}\n")