boltffi_bindgen 0.24.0

Code generation library for BoltFFI - generates Swift, Kotlin, and TypeScript bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package {{ package_name }};

import java.util.concurrent.atomic.AtomicBoolean;
{%- if class.has_async_methods() && async_mode.is_completable_future() %}
import java.util.concurrent.CompletableFuture;
{%- endif %}

{{ self::javadoc_block(class.doc, "") }}public final class {{ class.class_name }} implements AutoCloseable {
    private final long handle;
    private final AtomicBoolean closed = new AtomicBoolean(false);

    private {{ class.class_name }}(long handle) {
        this.handle = handle;
    }
{%- for ctor in class.constructors %}{% if !ctor.is_factory() %}

{{ self::javadoc_block(ctor.doc, "    ") }}    public {{ class.class_name }}({% for param in ctor.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        this({{ class.class_name }}.createHandle{{ loop.index0 }}({% for param in ctor.params %}{{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}));
    }

    private static long createHandle{{ loop.index0 }}({% for param in ctor.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for binding in ctor.input_bindings.direct_composites %}
        {{ binding.declaration() }};
{%- endfor %}
{%- if ctor.input_bindings.wire_writers.is_empty() %}
{%- if ctor.is_fallible %}
        long _handle = Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
        if (_handle == 0L) throw new RuntimeException("Constructor failed");
        return _handle;
{%- else %}
        return Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- endif %}
{%- else %}
        try (
{%- for writer in ctor.input_bindings.wire_writers %}
            WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }}){% if !loop.last %};{% endif %}
{%- endfor %}
        ) {
{%- for writer in ctor.input_bindings.wire_writers %}
            {{ writer.encode_expr }};
{%- endfor %}
{%- if ctor.is_fallible %}
            long _handle = Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
            if (_handle == 0L) throw new RuntimeException("Constructor failed");
            return _handle;
{%- else %}
            return Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- endif %}
        }
{%- endif %}
    }
{%- endif %}{% endfor %}

    long rawHandle() {
        return handle;
    }

    @Override
    public void close() {
        if (!closed.compareAndSet(false, true)) return;
        Native.{{ class.ffi_free }}(handle);
    }
{%- if class.has_factory_constructors() || class.has_static_methods() || class.has_async_methods() %}
{%- for ctor in class.constructors %}{% if ctor.is_factory() %}

{{ self::javadoc_block(ctor.doc, "    ") }}    public static {{ class.class_name }} {{ ctor.name }}({% for param in ctor.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for binding in ctor.input_bindings.direct_composites %}
        {{ binding.declaration() }};
{%- endfor %}
{%- if ctor.input_bindings.wire_writers.is_empty() %}
{%- if ctor.is_fallible %}
        long _handle = Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
        if (_handle == 0L) throw new RuntimeException("Factory constructor failed");
        return new {{ class.class_name }}(_handle);
{%- else %}
        return new {{ class.class_name }}(Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- endif %}
{%- else %}
        try (
{%- for writer in ctor.input_bindings.wire_writers %}
            WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }}){% if !loop.last %};{% endif %}
{%- endfor %}
        ) {
{%- for writer in ctor.input_bindings.wire_writers %}
            {{ writer.encode_expr }};
{%- endfor %}
{%- if ctor.is_fallible %}
            long _handle = Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
            if (_handle == 0L) throw new RuntimeException("Factory constructor failed");
            return new {{ class.class_name }}(_handle);
{%- else %}
            return new {{ class.class_name }}(Native.{{ ctor.ffi_name }}({% for param in ctor.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- endif %}
        }
{%- endif %}
    }
{%- endif %}{% endfor %}
{%- for method in class.methods %}{% if method.is_static && !method.is_async() %}

{{ self::javadoc_block(method.doc, "    ") }}    public static {{ method.return_type }} {{ method.name }}({% for param in method.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for binding in method.input_bindings.direct_composites %}
        {{ binding.declaration() }};
{%- endfor %}
{%- if method.input_bindings.wire_writers.is_empty() %}
{%- if method.return_plan.is_void() %}
        Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_direct() %}
        return Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_c_style_enum() %}
        return {{ method.return_plan.c_style_enum_class() }}.fromValue(Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif method.return_plan.is_handle() %}
        long _handle = Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.handle_nullable() %}
        if (_handle == 0L) return null;
{%- endif %}
        return new {{ method.return_plan.handle_class() }}(_handle);
{%- elif method.return_plan.is_callback() %}
        long _handle = Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.callback_nullable() %}
        if (_handle == 0L) return null;
{%- endif %}
        return {{ method.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif method.return_plan.is_decode() %}
        byte[] _buf = Native.{{ method.ffi_name }}({% for param in method.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 {{ method.return_plan.decode_expr() }};
{%- elif method.return_plan.is_result() %}
        byte[] _buf = Native.{{ method.ffi_name }}({% for param in method.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 method.return_plan.result_err_throws_directly() %}
            throw {{ method.return_plan.result_err_decode() }};
{%- elif method.return_plan.result_has_typed_exception() %}
            throw new {{ method.return_plan.result_err_exception_class() }}({{ method.return_plan.result_err_decode() }});
{%- elif method.return_plan.result_err_is_string() %}
            throw new RuntimeException({{ method.return_plan.result_err_decode() }});
{%- else %}
            throw new RuntimeException(String.valueOf({{ method.return_plan.result_err_decode() }}));
{%- endif %}
        }
{%- if method.return_type == "void" %}
{%- else %}
        return {{ method.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
{%- else %}
        try (
{%- for writer in method.input_bindings.wire_writers %}
            WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }}){% if !loop.last %};{% endif %}
{%- endfor %}
        ) {
{%- for writer in method.input_bindings.wire_writers %}
            {{ writer.encode_expr }};
{%- endfor %}
{%- if method.return_plan.is_void() %}
            Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_direct() %}
            return Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_c_style_enum() %}
            return {{ method.return_plan.c_style_enum_class() }}.fromValue(Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif method.return_plan.is_handle() %}
            long _handle = Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.handle_nullable() %}
            if (_handle == 0L) return null;
{%- endif %}
            return new {{ method.return_plan.handle_class() }}(_handle);
{%- elif method.return_plan.is_callback() %}
            long _handle = Native.{{ method.ffi_name }}({% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.callback_nullable() %}
            if (_handle == 0L) return null;
{%- endif %}
            return {{ method.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif method.return_plan.is_decode() %}
            byte[] _buf = Native.{{ method.ffi_name }}({% for param in method.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 {{ method.return_plan.decode_expr() }};
{%- elif method.return_plan.is_result() %}
            byte[] _buf = Native.{{ method.ffi_name }}({% for param in method.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 method.return_plan.result_err_throws_directly() %}
                throw {{ method.return_plan.result_err_decode() }};
{%- elif method.return_plan.result_has_typed_exception() %}
                throw new {{ method.return_plan.result_err_exception_class() }}({{ method.return_plan.result_err_decode() }});
{%- elif method.return_plan.result_err_is_string() %}
                throw new RuntimeException({{ method.return_plan.result_err_decode() }});
{%- else %}
                throw new RuntimeException(String.valueOf({{ method.return_plan.result_err_decode() }}));
{%- endif %}
            }
{%- if method.return_type == "void" %}
{%- else %}
            return {{ method.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
        }
{%- endif %}
    }
{%- endif %}{% endfor %}
{%- for method in class.methods %}{% if method.is_async() %}
{%- let ac = method.async_call.as_ref().unwrap() %}
{% if async_mode.is_virtual_thread() %}
{%- if ac.complete_return_plan.is_void() %}
{{ self::javadoc_block(method.doc, "    ") }}    public{% if method.is_static %} static{% endif %} void {{ method.name }}({% for param in method.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        BoltFFIAsync.callAsyncVoid(
{%- else %}
{{ self::javadoc_block(method.doc, "    ") }}    public{% if method.is_static %} static{% endif %} {{ method.return_type }} {{ method.name }}({% for param in method.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(method.doc, "    ") }}    public{% if method.is_static %} static{% endif %} CompletableFuture<Void> {{ method.name }}({% for param in method.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        return BoltFFIAsync.callAsyncVoid(
{%- else %}
{{ self::javadoc_block(method.doc, "    ") }}    public{% if method.is_static %} static{% endif %} CompletableFuture<{{ method.boxed_return_type() }}> {{ method.name }}({% for param in method.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
        return BoltFFIAsync.callAsync(
{%- endif %}
{%- endif %}
{%- if method.input_bindings.is_empty() %}
                () -> Native.{{ method.ffi_name }}({% if !method.is_static %}handle{% if !method.params.is_empty() %}, {% endif %}{% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}),
{%- else %}
                () -> {
{%- for binding in method.input_bindings.direct_composites %}
                    {{ binding.declaration() }};
{%- endfor %}
{%- for writer in method.input_bindings.wire_writers %}
                    try (WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }})) {
                        {{ writer.encode_expr }};
{%- endfor %}
                        return Native.{{ method.ffi_name }}({% if !method.is_static %}handle{% if !method.params.is_empty() %}, {% endif %}{% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- for _writer in method.input_bindings.wire_writers %}
                    }
{%- endfor %}
                },
{%- endif %}
                (future, contHandle) -> Native.{{ ac.poll }}({% if !method.is_static %}handle, {% endif %}future, contHandle),
{%- if ac.complete_return_plan.is_void() %}
                future -> Native.{{ ac.complete }}({% if !method.is_static %}handle, {% endif %}future),
{%- elif ac.complete_return_plan.is_direct() %}
                future -> Native.{{ ac.complete }}({% if !method.is_static %}handle, {% endif %}future),
{%- elif ac.complete_return_plan.is_decode() %}
                future -> {
                    byte[] _buf = Native.{{ ac.complete }}({% if !method.is_static %}handle, {% endif %}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 }}({% if !method.is_static %}handle, {% endif %}future)),
{%- elif ac.complete_return_plan.is_handle() %}
                future -> {
                    long _handle = Native.{{ ac.complete }}({% if !method.is_static %}handle, {% endif %}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 }}({% if !method.is_static %}handle, {% endif %}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 }}({% if !method.is_static %}handle, {% endif %}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 method.return_type == "void" %}
                    return null;
{%- else %}
                    return {{ ac.complete_return_plan.result_ok_decode() }};
{%- endif %}
                },
{%- endif %}
                future -> Native.{{ ac.free }}({% if !method.is_static %}handle, {% endif %}future),
                future -> Native.{{ ac.cancel }}({% if !method.is_static %}handle, {% endif %}future)
        );
    }
{%- endif %}{% endfor %}
{%- endif %}
{%- for method in class.methods %}{% if !method.is_static && !method.is_async() %}

{{ self::javadoc_block(method.doc, "    ") }}    public {{ method.return_type }} {{ method.name }}({% for param in method.params %}{{ param.java_type }} {{ param.name }}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for binding in method.input_bindings.direct_composites %}
        {{ binding.declaration() }};
{%- endfor %}
{%- if method.input_bindings.wire_writers.is_empty() %}
{%- if method.return_plan.is_void() %}
        Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_direct() %}
        return Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_c_style_enum() %}
        return {{ method.return_plan.c_style_enum_class() }}.fromValue(Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif method.return_plan.is_handle() %}
        long _handle = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.handle_nullable() %}
        if (_handle == 0L) return null;
{%- endif %}
        return new {{ method.return_plan.handle_class() }}(_handle);
{%- elif method.return_plan.is_callback() %}
        long _handle = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.callback_nullable() %}
        if (_handle == 0L) return null;
{%- endif %}
        return {{ method.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif method.return_plan.is_decode() %}
        byte[] _buf = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.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 {{ method.return_plan.decode_expr() }};
{%- elif method.return_plan.is_result() %}
        byte[] _buf = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.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 method.return_plan.result_err_throws_directly() %}
            throw {{ method.return_plan.result_err_decode() }};
{%- elif method.return_plan.result_has_typed_exception() %}
            throw new {{ method.return_plan.result_err_exception_class() }}({{ method.return_plan.result_err_decode() }});
{%- elif method.return_plan.result_err_is_string() %}
            throw new RuntimeException({{ method.return_plan.result_err_decode() }});
{%- else %}
            throw new RuntimeException(String.valueOf({{ method.return_plan.result_err_decode() }}));
{%- endif %}
        }
{%- if method.return_type == "void" %}
{%- else %}
        return {{ method.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
{%- else %}
        try (
{%- for writer in method.input_bindings.wire_writers %}
            WireWriter {{ writer.binding_name }} = new WireWriter({{ writer.size_expr }}){% if !loop.last %};{% endif %}
{%- endfor %}
        ) {
{%- for writer in method.input_bindings.wire_writers %}
            {{ writer.encode_expr }};
{%- endfor %}
{%- if method.return_plan.is_void() %}
            Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_direct() %}
            return Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- elif method.return_plan.is_c_style_enum() %}
            return {{ method.return_plan.c_style_enum_class() }}.fromValue(Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %}));
{%- elif method.return_plan.is_handle() %}
            long _handle = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.handle_nullable() %}
            if (_handle == 0L) return null;
{%- endif %}
            return new {{ method.return_plan.handle_class() }}(_handle);
{%- elif method.return_plan.is_callback() %}
            long _handle = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.params %}{{ param.native_expr }}{% if !loop.last %}, {% endif %}{% endfor %});
{%- if method.return_plan.callback_nullable() %}
            if (_handle == 0L) return null;
{%- endif %}
            return {{ method.return_plan.callback_bridge_class() }}.wrap(_handle);
{%- elif method.return_plan.is_decode() %}
            byte[] _buf = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.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 {{ method.return_plan.decode_expr() }};
{%- elif method.return_plan.is_result() %}
            byte[] _buf = Native.{{ method.ffi_name }}(handle{% if !method.params.is_empty() %}, {% endif %}{% for param in method.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 method.return_plan.result_err_throws_directly() %}
                throw {{ method.return_plan.result_err_decode() }};
{%- elif method.return_plan.result_has_typed_exception() %}
                throw new {{ method.return_plan.result_err_exception_class() }}({{ method.return_plan.result_err_decode() }});
{%- elif method.return_plan.result_err_is_string() %}
                throw new RuntimeException({{ method.return_plan.result_err_decode() }});
{%- else %}
                throw new RuntimeException(String.valueOf({{ method.return_plan.result_err_decode() }}));
{%- endif %}
            }
{%- if method.return_type == "void" %}
{%- else %}
            return {{ method.return_plan.result_ok_decode() }};
{%- endif %}
{%- endif %}
        }
{%- endif %}
    }
{%- endif %}{% endfor %}
{%- for stream in class.streams %}
{%- match stream.mode %}
{%- when crate::render::java::plan::JavaStreamMode::Async %}

{{ self::javadoc_block(stream.doc, "    ") }}    public StreamSubscription<{{ stream.item_type }}> {{ stream.name }}(java.util.function.Consumer<{{ stream.item_type }}> callback) {
        long subscription = Native.{{ stream.subscribe }}(handle);
        if (subscription == 0L) return StreamSubscription.callback(() -> {});
        BoltFFIStreamContext context = new BoltFFIStreamContext(
            subscription, 16L,
            Native::{{ stream.pop_batch }},
            Native::{{ stream.poll }},
            Native::{{ stream.unsubscribe }},
            Native::{{ stream.free }},
            _bytes -> { for ({{ stream.item_type }} item : {{ stream.pop_batch_items_expr }}) callback.accept(item); },
            () -> {}
        );
        context.start();
        return StreamSubscription.callback(context::requestTermination);
    }
{%- when crate::render::java::plan::JavaStreamMode::Batch %}

{{ self::javadoc_block(stream.doc, "    ") }}    public StreamSubscription<{{ stream.item_type }}> {{ stream.name }}() {
        return StreamSubscription.batch(
            Native.{{ stream.subscribe }}(handle),
            Native::{{ stream.pop_batch }},
            Native::{{ stream.wait }},
            Native::{{ stream.unsubscribe }},
            Native::{{ stream.free }},
            _bytes -> {{ stream.pop_batch_items_expr }}
        );
    }
{%- when crate::render::java::plan::JavaStreamMode::Callback %}

{{ self::javadoc_block(stream.doc, "    ") }}    public StreamSubscription<{{ stream.item_type }}> {{ stream.name }}(java.util.function.Consumer<{{ stream.item_type }}> callback) {
        long subscription = Native.{{ stream.subscribe }}(handle);
        if (subscription == 0L) return StreamSubscription.callback(() -> {});
        BoltFFIStreamContext context = new BoltFFIStreamContext(
            subscription, 16L,
            Native::{{ stream.pop_batch }},
            Native::{{ stream.poll }},
            Native::{{ stream.unsubscribe }},
            Native::{{ stream.free }},
            _bytes -> { for ({{ stream.item_type }} item : {{ stream.pop_batch_items_expr }}) callback.accept(item); },
            () -> {}
        );
        context.start();
        return StreamSubscription.callback(context::requestTermination);
    }
{%- endmatch %}
{%- endfor %}
}