public final class {{ module.class_name }} {
    private {{ module.class_name }}() {}
{%- for func in module.functions %}
{%- if func.is_async() %}
{%- match func.async_call %}
{%- when Some with (ac) %}
{% if module.async_mode.is_virtual_thread() %}
{%- if ac.complete_return_plan.is_void() %}
{{ self::javadoc_block(func.doc, "    ") }}    public static void {{ func.name }}({% for param in func.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        BoltFFIAsync.callAsyncVoid(
{%- else %}
{{ self::javadoc_block(func.doc, "    ") }}    public static {{ func.return_type }} {{ func.name }}({% for param in func.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        return BoltFFIAsync.callAsync(
{%- endif %}
{%- else %}
{%- if ac.complete_return_plan.is_void() %}
{{ self::javadoc_block(func.doc, "    ") }}    public static CompletableFuture<Void> {{ func.name }}({% for param in func.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        return BoltFFIAsync.callAsyncVoid(
{%- else %}
{{ self::javadoc_block(func.doc, "    ") }}    public static CompletableFuture<{{ func.boxed_return_type() }}> {{ func.name }}({% for param in func.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        return BoltFFIAsync.callAsync(
{%- endif %}
{%- endif %}
{%- if func.input_bindings.is_empty() %}
                () -> Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}),
{%- else %}
                () -> {
{%- for binding in func.input_bindings.direct_composites %}
                    {{ binding.declaration() }};
{%- endfor %}
{%- for writer in func.input_bindings.wire_writers %}
                    try (WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }})) {
                        {{ writer.encode_expr }};
{%- endfor %}
                        return Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- for _writer in func.input_bindings.wire_writers %}
                    }
{%- endfor %}
                },
{%- endif %}
                (future, contHandle) -> Native.{{ ac.poll }}(future, contHandle),
{%- if ac.complete_return_plan.is_void() %}
                future -> Native.{{ ac.complete }}(future),
{%- elif ac.complete_return_plan.is_direct() %}
                future -> Native.{{ ac.complete }}(future),
{%- elif ac.complete_return_plan.is_decode() %}
                future -> {
                    byte[] _buf = Native.{{ ac.complete }}(future);
                    if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
                    WireReader reader = new WireReader(_buf);
                    return {{ ac.complete_return_plan.decode_expr() }};
                },
{%- elif ac.complete_return_plan.is_c_style_enum() %}
                future -> {{ ac.complete_return_plan.c_style_enum_class() }}.fromValue(Native.{{ ac.complete }}(future)),
{%- elif ac.complete_return_plan.is_handle() %}
                future -> {
                    long _handle = Native.{{ ac.complete }}(future);
{%- if ac.complete_return_plan.handle_nullable() %}
                    if (_handle == 0L) return null;
{%- endif %}
                    return new {{ ac.complete_return_plan.handle_class() }}(_handle);
                },
{%- elif ac.complete_return_plan.is_callback() %}
                future -> {
                    long _handle = Native.{{ ac.complete }}(future);
{%- if ac.complete_return_plan.callback_nullable() %}
                    if (_handle == 0L) return null;
{%- endif %}
                    return {{ ac.complete_return_plan.callback_bridge_class() }}.wrap(_handle);
                },
{%- elif ac.complete_return_plan.is_result() %}
                future -> {
                    byte[] _buf = Native.{{ ac.complete }}(future);
                    if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
                    WireReader reader = new WireReader(_buf);
                    if (reader.readI8() != 0) {
{%- if ac.complete_return_plan.result_err_throws_directly() %}
                        throw {{ ac.complete_return_plan.result_err_decode() }};
{%- elif ac.complete_return_plan.result_has_typed_exception() %}
                        throw new {{ ac.complete_return_plan.result_err_exception_class() }}({{ ac.complete_return_plan.result_err_decode() }});
{%- elif ac.complete_return_plan.result_err_is_string() %}
                        throw new RuntimeException({{ ac.complete_return_plan.result_err_decode() }});
{%- else %}
                        throw new RuntimeException(String.valueOf({{ ac.complete_return_plan.result_err_decode() }}));
{%- endif %}
                    }
{%- if func.return_type == "void" %}
                    return null;
{%- else %}
                    return {{ ac.complete_return_plan.result_ok_decode() }};
{%- endif %}
                },
{%- endif %}
                future -> Native.{{ ac.free }}(future),
                future -> Native.{{ ac.cancel }}(future)
        );
    }
{%- when None %}
{%- endmatch %}
{%- else %}

{{ self::javadoc_block(func.doc, "    ") }}    public static {{ func.return_type }} {{ func.name }}({% for param in func.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for binding in func.input_bindings.direct_composites %}
        {{ binding.declaration() }};
{%- endfor %}
{%- if func.input_bindings.wire_writers.is_empty() %}
{%- if func.return_plan.is_void() %}
        Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif func.return_plan.is_direct() %}
        return Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif func.return_plan.is_c_style_enum() %}
        return {{ func.return_plan.c_style_enum_class() }}.fromValue(Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif func.return_plan.is_callback() %}
        long _handle = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if func.return_plan.callback_nullable() %}
        if (_handle == 0L) return null;
{%- endif %}
        return {{ func.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif func.return_plan.is_decode() %}
        byte[] _buf = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
        if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
        WireReader reader = new WireReader(_buf);
        return {{ func.return_plan.decode_expr() }};
{%- elif func.return_plan.is_result() %}
        byte[] _buf = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
        if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
        WireReader reader = new WireReader(_buf);
        if (reader.readI8() != 0) {
{%- if func.return_plan.result_err_throws_directly() %}
            throw {{ func.return_plan.result_err_decode() }};
{%- elif func.return_plan.result_has_typed_exception() %}
            throw new {{ func.return_plan.result_err_exception_class() }}({{ func.return_plan.result_err_decode() }});
{%- elif func.return_plan.result_err_is_string() %}
            throw new RuntimeException({{ func.return_plan.result_err_decode() }});
{%- else %}
            throw new RuntimeException(String.valueOf({{ func.return_plan.result_err_decode() }}));
{%- endif %}
        }
{%- if func.return_type == "void" %}
{%- else %}
        return {{ func.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
{%- else %}
        try (
{%- for writer in func.input_bindings.wire_writers %}
            WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }}){% if !loop.last %};{% endif %}
{%- endfor %}
        ) {
{%- for writer in func.input_bindings.wire_writers %}
            {{ writer.encode_expr }};
{%- endfor %}
{%- if func.return_plan.is_void() %}
            Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif func.return_plan.is_direct() %}
            return Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif func.return_plan.is_c_style_enum() %}
            return {{ func.return_plan.c_style_enum_class() }}.fromValue(Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif func.return_plan.is_callback() %}
            long _handle = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if func.return_plan.callback_nullable() %}
            if (_handle == 0L) return null;
{%- endif %}
            return {{ func.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif func.return_plan.is_decode() %}
            byte[] _buf = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
            if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
            WireReader reader = new WireReader(_buf);
            return {{ func.return_plan.decode_expr() }};
{%- elif func.return_plan.is_result() %}
            byte[] _buf = Native.{{ func.ffi_name }}({% for param in func.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
            if (_buf == null) throw new RuntimeException("FFI call returned null buffer");
            WireReader reader = new WireReader(_buf);
            if (reader.readI8() != 0) {
{%- if func.return_plan.result_err_throws_directly() %}
                throw {{ func.return_plan.result_err_decode() }};
{%- elif func.return_plan.result_has_typed_exception() %}
                throw new {{ func.return_plan.result_err_exception_class() }}({{ func.return_plan.result_err_decode() }});
{%- elif func.return_plan.result_err_is_string() %}
                throw new RuntimeException({{ func.return_plan.result_err_decode() }});
{%- else %}
                throw new RuntimeException(String.valueOf({{ func.return_plan.result_err_decode() }}));
{%- endif %}
            }
{%- if func.return_type == "void" %}
{%- else %}
            return {{ func.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
        }
{%- endif %}
    }
{%- endif %}
{%- endfor %}
}
