- 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")
- 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 = ""
- 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 = java_c_types(field.strip(' ;'), None)
- out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.var_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.var_name
- init_meth_body = init_meth_body + "this." + field_ty.var_name + " = " + field_ty.var_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")
- init_meth_jty_strs[var_name] = init_meth_jty_str
- 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)
- 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")
+ 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")
+ write_c("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
+ write_c("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");
+
+ write_c("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:]
+ write_c("\t" + struct_name + "_" + var_name + "_class =\n")
+ write_c("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
+ write_c("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
+ write_c("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
+ write_c("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
+ write_c("}\n")
+ write_c("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
+ write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+ write_c("\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:]
+ write_c("\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:
+ write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
+ write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
+ write_c(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
+ write_c("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
+ write_c("\t\t}\n")
+ write_c("\t\tdefault: abort();\n")
+ write_c("\t}\n}\n")
+ out_java_enum.write("}\n")