]> git.bitcoin.ninja Git - ldk-java/commitdiff
Cache enum fields
authorMatt Corallo <git@bluematt.me>
Wed, 7 Oct 2020 23:55:37 +0000 (19:55 -0400)
committerMatt Corallo <git@bluematt.me>
Wed, 7 Oct 2020 23:55:37 +0000 (19:55 -0400)
genbindings.py
src/main/java/org/ldk/enums/LDKAccessError.java
src/main/java/org/ldk/enums/LDKChannelMonitorUpdateErr.java
src/main/java/org/ldk/enums/LDKConfirmationTarget.java
src/main/java/org/ldk/enums/LDKLevel.java
src/main/java/org/ldk/enums/LDKNetwork.java
src/main/java/org/ldk/enums/LDKSecp256k1Error.java
src/main/java/org/ldk/impl/bindings.java
src/main/jni/bindings.c

index d9999d963516d923377cbb821bc887cd1e03c78d..ab1df82027b622c26f3f70a76e189559a80b528c 100755 (executable)
@@ -737,7 +737,10 @@ _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen),
                             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:
@@ -749,19 +752,27 @@ _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen),
                         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("\tDO_ASSERT(" + 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("\tDO_ASSERT(" + 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("\t// TODO: This is pretty inefficient, we really need to cache the field IDs and class\n")
-                        out_c.write("\tjclass enum_class = (*env)->FindClass(env, \"Lorg/ldk/enums/" + struct_name + ";\");\n")
-                        out_c.write("\tDO_ASSERT(enum_class != NULL);\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\tjfieldID field = (*env)->GetStaticFieldID(env, enum_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
-                                out_c.write("\t\t\tDO_ASSERT(field != NULL);\n")
-                                out_c.write("\t\t\treturn (*env)->GetStaticObjectField(env, enum_class, field);\n")
-                                out_c.write("\t\t}\n")
+                                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")
index 35f4cfe58e27d918a11f17b7f5d26256c5990666..dc7b74d9ebec5118729579371660a7e2443c5fa3 100644 (file)
@@ -3,4 +3,6 @@ package org.ldk.enums;
 public enum LDKAccessError {
    LDKAccessError_UnknownChain,
    LDKAccessError_UnknownTx,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index b46efad2c17d0ca51389264c959eb00c7cb23c79..15256928aefde1af73c37780078b132235add32b 100644 (file)
@@ -3,4 +3,6 @@ package org.ldk.enums;
 public enum LDKChannelMonitorUpdateErr {
    LDKChannelMonitorUpdateErr_TemporaryFailure,
    LDKChannelMonitorUpdateErr_PermanentFailure,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index 2a5be54b8c49b5151c3edba7e030ad5bf8765d66..de9471fa4bf4d90b830d849df994acde5a3e9172 100644 (file)
@@ -4,4 +4,6 @@ public enum LDKConfirmationTarget {
    LDKConfirmationTarget_Background,
    LDKConfirmationTarget_Normal,
    LDKConfirmationTarget_HighPriority,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index f947ee24b356032252ff1edc9c0c0cf7d495138b..543101a22bc4d21a23745030913836b7a5730225 100644 (file)
@@ -7,4 +7,6 @@ public enum LDKLevel {
    LDKLevel_Info,
    LDKLevel_Debug,
    LDKLevel_Trace,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index 38eb2b28b6f56e9eeee32e1d5b14249e3029df3d..5a554c4b816b0d09553e22944c9b5c16e5ea6221 100644 (file)
@@ -4,4 +4,6 @@ public enum LDKNetwork {
    LDKNetwork_Bitcoin,
    LDKNetwork_Testnet,
    LDKNetwork_Regtest,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index 4857a95820dba018e2d39dc1c78f3a048ec1e2a2..e9e3dbcfe7d17fc6db755fc0f6ebf4f8ad719c7a 100644 (file)
@@ -10,4 +10,6 @@ public enum LDKSecp256k1Error {
    LDKSecp256k1Error_InvalidTweak,
    LDKSecp256k1Error_NotEnoughMemory,
    LDKSecp256k1Error_CallbackPanicked,
+       ; static native void init();
+       static { init(); }
 }
\ No newline at end of file
index cdf23173e53bb327ec417186273d0823ee076833..e153db6d40ba6b980b1633c945d277df85f06a9e 100644 (file)
@@ -26,6 +26,12 @@ public class bindings {
 
        public static native long LDKSecretKey_new();
 
+       static { LDKAccessError.values(); /* Force enum statics to run */ }
+       static { LDKChannelMonitorUpdateErr.values(); /* Force enum statics to run */ }
+       static { LDKConfirmationTarget.values(); /* Force enum statics to run */ }
+       static { LDKLevel.values(); /* Force enum statics to run */ }
+       static { LDKNetwork.values(); /* Force enum statics to run */ }
+       static { LDKSecp256k1Error.values(); /* Force enum statics to run */ }
        public static native VecOrSliceDef LDKCVecTempl_u8_arr_info(long vec_ptr);
        public static native boolean LDKCResult_NoneChannelMonitorUpdateErrZ_result_ok(long arg);
        public static native long LDKCResult_NoneChannelMonitorUpdateErrZ_get_inner(long arg);
index 7b83eff6f416bd8582f1303e8865f176540ec5b7..362b0b8afd0da35499739e5fd15294dbb42ef31f 100644 (file)
@@ -120,21 +120,23 @@ static inline LDKAccessError LDKAccessError_from_java(JNIEnv *env, jclass val) {
        }
        abort();
 }
+static jclass LDKAccessError_class = NULL;
+static jfieldID LDKAccessError_LDKAccessError_UnknownChain = NULL;
+static jfieldID LDKAccessError_LDKAccessError_UnknownTx = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKAccessError_init (JNIEnv * env, jclass clz) {
+       LDKAccessError_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKAccessError_class != NULL);
+       LDKAccessError_LDKAccessError_UnknownChain = (*env)->GetStaticFieldID(env, LDKAccessError_class, "LDKAccessError_UnknownChain", "Lorg/ldk/enums/LDKAccessError;");
+       DO_ASSERT(LDKAccessError_LDKAccessError_UnknownChain != NULL);
+       LDKAccessError_LDKAccessError_UnknownTx = (*env)->GetStaticFieldID(env, LDKAccessError_class, "LDKAccessError_UnknownTx", "Lorg/ldk/enums/LDKAccessError;");
+       DO_ASSERT(LDKAccessError_LDKAccessError_UnknownTx != NULL);
+}
 static inline jclass LDKAccessError_to_java(JNIEnv *env, LDKAccessError val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKAccessError;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKAccessError_UnknownChain: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKAccessError_UnknownChain", "Lorg/ldk/enums/LDKAccessError;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKAccessError_UnknownTx: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKAccessError_UnknownTx", "Lorg/ldk/enums/LDKAccessError;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKAccessError_UnknownChain:
+                       return (*env)->GetStaticObjectField(env, LDKAccessError_class, LDKAccessError_LDKAccessError_UnknownChain);
+               case LDKAccessError_UnknownTx:
+                       return (*env)->GetStaticObjectField(env, LDKAccessError_class, LDKAccessError_LDKAccessError_UnknownTx);
                default: abort();
        }
 }
@@ -146,21 +148,23 @@ static inline LDKChannelMonitorUpdateErr LDKChannelMonitorUpdateErr_from_java(JN
        }
        abort();
 }
+static jclass LDKChannelMonitorUpdateErr_class = NULL;
+static jfieldID LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_TemporaryFailure = NULL;
+static jfieldID LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_PermanentFailure = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKChannelMonitorUpdateErr_init (JNIEnv * env, jclass clz) {
+       LDKChannelMonitorUpdateErr_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKChannelMonitorUpdateErr_class != NULL);
+       LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_TemporaryFailure = (*env)->GetStaticFieldID(env, LDKChannelMonitorUpdateErr_class, "LDKChannelMonitorUpdateErr_TemporaryFailure", "Lorg/ldk/enums/LDKChannelMonitorUpdateErr;");
+       DO_ASSERT(LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_TemporaryFailure != NULL);
+       LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_PermanentFailure = (*env)->GetStaticFieldID(env, LDKChannelMonitorUpdateErr_class, "LDKChannelMonitorUpdateErr_PermanentFailure", "Lorg/ldk/enums/LDKChannelMonitorUpdateErr;");
+       DO_ASSERT(LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_PermanentFailure != NULL);
+}
 static inline jclass LDKChannelMonitorUpdateErr_to_java(JNIEnv *env, LDKChannelMonitorUpdateErr val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKChannelMonitorUpdateErr;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKChannelMonitorUpdateErr_TemporaryFailure: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKChannelMonitorUpdateErr_TemporaryFailure", "Lorg/ldk/enums/LDKChannelMonitorUpdateErr;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKChannelMonitorUpdateErr_PermanentFailure: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKChannelMonitorUpdateErr_PermanentFailure", "Lorg/ldk/enums/LDKChannelMonitorUpdateErr;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKChannelMonitorUpdateErr_TemporaryFailure:
+                       return (*env)->GetStaticObjectField(env, LDKChannelMonitorUpdateErr_class, LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_TemporaryFailure);
+               case LDKChannelMonitorUpdateErr_PermanentFailure:
+                       return (*env)->GetStaticObjectField(env, LDKChannelMonitorUpdateErr_class, LDKChannelMonitorUpdateErr_LDKChannelMonitorUpdateErr_PermanentFailure);
                default: abort();
        }
 }
@@ -173,26 +177,28 @@ static inline LDKConfirmationTarget LDKConfirmationTarget_from_java(JNIEnv *env,
        }
        abort();
 }
+static jclass LDKConfirmationTarget_class = NULL;
+static jfieldID LDKConfirmationTarget_LDKConfirmationTarget_Background = NULL;
+static jfieldID LDKConfirmationTarget_LDKConfirmationTarget_Normal = NULL;
+static jfieldID LDKConfirmationTarget_LDKConfirmationTarget_HighPriority = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKConfirmationTarget_init (JNIEnv * env, jclass clz) {
+       LDKConfirmationTarget_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKConfirmationTarget_class != NULL);
+       LDKConfirmationTarget_LDKConfirmationTarget_Background = (*env)->GetStaticFieldID(env, LDKConfirmationTarget_class, "LDKConfirmationTarget_Background", "Lorg/ldk/enums/LDKConfirmationTarget;");
+       DO_ASSERT(LDKConfirmationTarget_LDKConfirmationTarget_Background != NULL);
+       LDKConfirmationTarget_LDKConfirmationTarget_Normal = (*env)->GetStaticFieldID(env, LDKConfirmationTarget_class, "LDKConfirmationTarget_Normal", "Lorg/ldk/enums/LDKConfirmationTarget;");
+       DO_ASSERT(LDKConfirmationTarget_LDKConfirmationTarget_Normal != NULL);
+       LDKConfirmationTarget_LDKConfirmationTarget_HighPriority = (*env)->GetStaticFieldID(env, LDKConfirmationTarget_class, "LDKConfirmationTarget_HighPriority", "Lorg/ldk/enums/LDKConfirmationTarget;");
+       DO_ASSERT(LDKConfirmationTarget_LDKConfirmationTarget_HighPriority != NULL);
+}
 static inline jclass LDKConfirmationTarget_to_java(JNIEnv *env, LDKConfirmationTarget val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKConfirmationTarget;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKConfirmationTarget_Background: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKConfirmationTarget_Background", "Lorg/ldk/enums/LDKConfirmationTarget;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKConfirmationTarget_Normal: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKConfirmationTarget_Normal", "Lorg/ldk/enums/LDKConfirmationTarget;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKConfirmationTarget_HighPriority: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKConfirmationTarget_HighPriority", "Lorg/ldk/enums/LDKConfirmationTarget;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKConfirmationTarget_Background:
+                       return (*env)->GetStaticObjectField(env, LDKConfirmationTarget_class, LDKConfirmationTarget_LDKConfirmationTarget_Background);
+               case LDKConfirmationTarget_Normal:
+                       return (*env)->GetStaticObjectField(env, LDKConfirmationTarget_class, LDKConfirmationTarget_LDKConfirmationTarget_Normal);
+               case LDKConfirmationTarget_HighPriority:
+                       return (*env)->GetStaticObjectField(env, LDKConfirmationTarget_class, LDKConfirmationTarget_LDKConfirmationTarget_HighPriority);
                default: abort();
        }
 }
@@ -208,41 +214,43 @@ static inline LDKLevel LDKLevel_from_java(JNIEnv *env, jclass val) {
        }
        abort();
 }
+static jclass LDKLevel_class = NULL;
+static jfieldID LDKLevel_LDKLevel_Off = NULL;
+static jfieldID LDKLevel_LDKLevel_Error = NULL;
+static jfieldID LDKLevel_LDKLevel_Warn = NULL;
+static jfieldID LDKLevel_LDKLevel_Info = NULL;
+static jfieldID LDKLevel_LDKLevel_Debug = NULL;
+static jfieldID LDKLevel_LDKLevel_Trace = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKLevel_init (JNIEnv * env, jclass clz) {
+       LDKLevel_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKLevel_class != NULL);
+       LDKLevel_LDKLevel_Off = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Off", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Off != NULL);
+       LDKLevel_LDKLevel_Error = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Error", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Error != NULL);
+       LDKLevel_LDKLevel_Warn = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Warn", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Warn != NULL);
+       LDKLevel_LDKLevel_Info = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Info", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Info != NULL);
+       LDKLevel_LDKLevel_Debug = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Debug", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Debug != NULL);
+       LDKLevel_LDKLevel_Trace = (*env)->GetStaticFieldID(env, LDKLevel_class, "LDKLevel_Trace", "Lorg/ldk/enums/LDKLevel;");
+       DO_ASSERT(LDKLevel_LDKLevel_Trace != NULL);
+}
 static inline jclass LDKLevel_to_java(JNIEnv *env, LDKLevel val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKLevel;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKLevel_Off: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Off", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKLevel_Error: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Error", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKLevel_Warn: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Warn", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKLevel_Info: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Info", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKLevel_Debug: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Debug", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKLevel_Trace: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKLevel_Trace", "Lorg/ldk/enums/LDKLevel;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKLevel_Off:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Off);
+               case LDKLevel_Error:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Error);
+               case LDKLevel_Warn:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Warn);
+               case LDKLevel_Info:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Info);
+               case LDKLevel_Debug:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Debug);
+               case LDKLevel_Trace:
+                       return (*env)->GetStaticObjectField(env, LDKLevel_class, LDKLevel_LDKLevel_Trace);
                default: abort();
        }
 }
@@ -255,26 +263,28 @@ static inline LDKNetwork LDKNetwork_from_java(JNIEnv *env, jclass val) {
        }
        abort();
 }
+static jclass LDKNetwork_class = NULL;
+static jfieldID LDKNetwork_LDKNetwork_Bitcoin = NULL;
+static jfieldID LDKNetwork_LDKNetwork_Testnet = NULL;
+static jfieldID LDKNetwork_LDKNetwork_Regtest = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKNetwork_init (JNIEnv * env, jclass clz) {
+       LDKNetwork_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKNetwork_class != NULL);
+       LDKNetwork_LDKNetwork_Bitcoin = (*env)->GetStaticFieldID(env, LDKNetwork_class, "LDKNetwork_Bitcoin", "Lorg/ldk/enums/LDKNetwork;");
+       DO_ASSERT(LDKNetwork_LDKNetwork_Bitcoin != NULL);
+       LDKNetwork_LDKNetwork_Testnet = (*env)->GetStaticFieldID(env, LDKNetwork_class, "LDKNetwork_Testnet", "Lorg/ldk/enums/LDKNetwork;");
+       DO_ASSERT(LDKNetwork_LDKNetwork_Testnet != NULL);
+       LDKNetwork_LDKNetwork_Regtest = (*env)->GetStaticFieldID(env, LDKNetwork_class, "LDKNetwork_Regtest", "Lorg/ldk/enums/LDKNetwork;");
+       DO_ASSERT(LDKNetwork_LDKNetwork_Regtest != NULL);
+}
 static inline jclass LDKNetwork_to_java(JNIEnv *env, LDKNetwork val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKNetwork;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKNetwork_Bitcoin: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKNetwork_Bitcoin", "Lorg/ldk/enums/LDKNetwork;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKNetwork_Testnet: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKNetwork_Testnet", "Lorg/ldk/enums/LDKNetwork;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKNetwork_Regtest: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKNetwork_Regtest", "Lorg/ldk/enums/LDKNetwork;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKNetwork_Bitcoin:
+                       return (*env)->GetStaticObjectField(env, LDKNetwork_class, LDKNetwork_LDKNetwork_Bitcoin);
+               case LDKNetwork_Testnet:
+                       return (*env)->GetStaticObjectField(env, LDKNetwork_class, LDKNetwork_LDKNetwork_Testnet);
+               case LDKNetwork_Regtest:
+                       return (*env)->GetStaticObjectField(env, LDKNetwork_class, LDKNetwork_LDKNetwork_Regtest);
                default: abort();
        }
 }
@@ -293,56 +303,58 @@ static inline LDKSecp256k1Error LDKSecp256k1Error_from_java(JNIEnv *env, jclass
        }
        abort();
 }
+static jclass LDKSecp256k1Error_class = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_IncorrectSignature = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidMessage = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidPublicKey = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidSignature = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidSecretKey = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidRecoveryId = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_InvalidTweak = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_NotEnoughMemory = NULL;
+static jfieldID LDKSecp256k1Error_LDKSecp256k1Error_CallbackPanicked = NULL;
+JNIEXPORT void JNICALL Java_org_ldk_enums_LDKSecp256k1Error_init (JNIEnv * env, jclass clz) {
+       LDKSecp256k1Error_class = (*env)->NewGlobalRef(env, clz);
+       DO_ASSERT(LDKSecp256k1Error_class != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_IncorrectSignature = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_IncorrectSignature", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_IncorrectSignature != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidMessage = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidMessage", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidMessage != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidPublicKey = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidPublicKey", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidPublicKey != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidSignature = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidSignature", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidSignature != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidSecretKey = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidSecretKey", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidSecretKey != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidRecoveryId = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidRecoveryId", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidRecoveryId != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_InvalidTweak = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_InvalidTweak", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_InvalidTweak != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_NotEnoughMemory = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_NotEnoughMemory", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_NotEnoughMemory != NULL);
+       LDKSecp256k1Error_LDKSecp256k1Error_CallbackPanicked = (*env)->GetStaticFieldID(env, LDKSecp256k1Error_class, "LDKSecp256k1Error_CallbackPanicked", "Lorg/ldk/enums/LDKSecp256k1Error;");
+       DO_ASSERT(LDKSecp256k1Error_LDKSecp256k1Error_CallbackPanicked != NULL);
+}
 static inline jclass LDKSecp256k1Error_to_java(JNIEnv *env, LDKSecp256k1Error val) {
-       // TODO: This is pretty inefficient, we really need to cache the field IDs and class
-       jclass enum_class = (*env)->FindClass(env, "Lorg/ldk/enums/LDKSecp256k1Error;");
-       DO_ASSERT(enum_class != NULL);
        switch (val) {
-               case LDKSecp256k1Error_IncorrectSignature: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_IncorrectSignature", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidMessage: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidMessage", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidPublicKey: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidPublicKey", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidSignature: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidSignature", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidSecretKey: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidSecretKey", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidRecoveryId: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidRecoveryId", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_InvalidTweak: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_InvalidTweak", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_NotEnoughMemory: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_NotEnoughMemory", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
-               case LDKSecp256k1Error_CallbackPanicked: {
-                       jfieldID field = (*env)->GetStaticFieldID(env, enum_class, "LDKSecp256k1Error_CallbackPanicked", "Lorg/ldk/enums/LDKSecp256k1Error;");
-                       DO_ASSERT(field != NULL);
-                       return (*env)->GetStaticObjectField(env, enum_class, field);
-               }
+               case LDKSecp256k1Error_IncorrectSignature:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_IncorrectSignature);
+               case LDKSecp256k1Error_InvalidMessage:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidMessage);
+               case LDKSecp256k1Error_InvalidPublicKey:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidPublicKey);
+               case LDKSecp256k1Error_InvalidSignature:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidSignature);
+               case LDKSecp256k1Error_InvalidSecretKey:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidSecretKey);
+               case LDKSecp256k1Error_InvalidRecoveryId:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidRecoveryId);
+               case LDKSecp256k1Error_InvalidTweak:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_InvalidTweak);
+               case LDKSecp256k1Error_NotEnoughMemory:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_NotEnoughMemory);
+               case LDKSecp256k1Error_CallbackPanicked:
+                       return (*env)->GetStaticObjectField(env, LDKSecp256k1Error_class, LDKSecp256k1Error_LDKSecp256k1Error_CallbackPanicked);
                default: abort();
        }
 }