zerodds-idl-java 1.0.0-rc.1

OMG IDL4 → Java 17 Code-Generator (idl4-java-1.0 + DDS-Java-PSM) für ZeroDDS.
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
//! Cluster-E-Tests fuer C5.4-b — Bitset/Bitmask, Multi-Inheritance,
//! `@value`-Enum, Annotation-Bridge, TopicType-Marker.
//!
//! Markerbasierte Snapshots — siehe `tests/fixtures.rs` fuer das
//! Pattern.

#![allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::panic,
    clippy::print_stderr,
    clippy::print_stdout,
    clippy::field_reassign_with_default,
    clippy::manual_flatten,
    clippy::collapsible_if,
    clippy::empty_line_after_doc_comments,
    clippy::uninlined_format_args,
    clippy::drop_non_drop,
    missing_docs
)]

use zerodds_idl::config::ParserConfig;
use zerodds_idl_java::{JavaFile, JavaGenError, JavaGenOptions, generate_java_files};

fn genfiles(src: &str) -> Vec<JavaFile> {
    let ast = zerodds_idl::parse(src, &ParserConfig::default()).expect("parse");
    generate_java_files(&ast, &JavaGenOptions::default()).expect("gen")
}

fn gen_err(src: &str) -> JavaGenError {
    let ast = zerodds_idl::parse(src, &ParserConfig::default()).expect("parse");
    generate_java_files(&ast, &JavaGenOptions::default()).expect_err("expected gen error")
}

fn find<'a>(files: &'a [JavaFile], class: &str) -> &'a JavaFile {
    files
        .iter()
        .find(|f| f.class_name == class)
        .unwrap_or_else(|| {
            panic!(
                "class {class} not in {:?}",
                files.iter().map(|f| &f.class_name).collect::<Vec<_>>()
            )
        })
}

fn join(files: &[JavaFile]) -> String {
    files
        .iter()
        .map(|f| f.source.clone())
        .collect::<Vec<_>>()
        .join("\n")
}

// ============================================================================
// Bitmask
// ============================================================================

#[test]
fn bitmask_default_bit_bound_32() {
    let files = genfiles("bitmask Flags { READ, WRITE, EXEC };");
    let f = find(&files, "Flags");
    assert!(f.source.contains("public final class Flags"));
    assert!(f.source.contains("BIT_BOUND = 32"));
    assert!(f.source.contains("READ(0)"));
    assert!(f.source.contains("WRITE(1)"));
    assert!(f.source.contains("EXEC(2);"));
    assert!(f.source.contains("java.util.EnumSet<Flag>"));
    assert!(f.source.contains("public boolean isSet(Flag f)"));
}

#[test]
fn bitmask_bit_bound_8_emits_constant() {
    let files = genfiles("@bit_bound(8) bitmask M { A };");
    let f = find(&files, "M");
    assert!(f.source.contains("BIT_BOUND = 8"));
}

#[test]
fn bitmask_bit_bound_16_emits_constant() {
    let files = genfiles("@bit_bound(16) bitmask M { A };");
    let f = find(&files, "M");
    assert!(f.source.contains("BIT_BOUND = 16"));
}

#[test]
fn bitmask_bit_bound_64_emits_constant() {
    let files = genfiles("@bit_bound(64) bitmask M { A };");
    let f = find(&files, "M");
    assert!(f.source.contains("BIT_BOUND = 64"));
}

#[test]
fn bitmask_position_overrides_implicit_cursor() {
    let files = genfiles("@bit_bound(16) bitmask M { @position(3) A, B };");
    let f = find(&files, "M");
    // explicit `@position(3)` → A(3); next implicit B(4).
    assert!(f.source.contains("A(3)"));
    assert!(f.source.contains("B(4)"));
}

#[test]
fn bitmask_inner_enum_has_position_accessor() {
    let files = genfiles("bitmask M { A };");
    let f = find(&files, "M");
    assert!(f.source.contains("public int position()"));
}

// ============================================================================
// Bitset
// ============================================================================

#[test]
fn bitset_simple_two_fields() {
    let files = genfiles("bitset MyBits { bitfield<3> a; bitfield<5> b; };");
    let f = find(&files, "MyBits");
    assert!(f.source.contains("public final class MyBits"));
    assert!(f.source.contains("private long bits;"));
    assert!(f.source.contains("public int getA()"));
    assert!(f.source.contains("public int getB()"));
    assert!(f.source.contains("public void setA(int value)"));
    assert!(f.source.contains("public void setB(int value)"));
}

#[test]
fn bitset_a_uses_mask_0x7_offset_0() {
    let files = genfiles("bitset MyBits { bitfield<3> a; };");
    let f = find(&files, "MyBits");
    assert!(f.source.contains("0x7L"));
    assert!(f.source.contains(">>> 0)"));
}

#[test]
fn bitset_b_uses_mask_0x1f_offset_3() {
    let files = genfiles("bitset MyBits { bitfield<3> a; bitfield<5> b; };");
    let f = find(&files, "MyBits");
    assert!(f.source.contains("0x1FL"));
    // setB shifts by 3
    assert!(f.source.contains("<< 3)"));
}

#[test]
fn bitset_cumulative_64bit_filled_is_ok() {
    let files = genfiles("bitset Full { bitfield<32> a; bitfield<32> b; };");
    let f = find(&files, "Full");
    assert!(f.source.contains("BIT_WIDTH = 64"));
}

#[test]
fn bitset_over_64_returns_error() {
    let err = gen_err("bitset Big { bitfield<40> a; bitfield<30> b; };");
    match err {
        JavaGenError::UnsupportedConstruct { construct, .. } => {
            assert!(construct.contains("64"), "msg: {construct}");
        }
        other => panic!("expected UnsupportedConstruct, got {other:?}"),
    }
}

#[test]
fn bitset_64bit_field_returns_long_typed_accessor() {
    let files = genfiles("bitset Wide { bitfield<64> a; };");
    let f = find(&files, "Wide");
    assert!(f.source.contains("public long getA()"));
    assert!(f.source.contains("public void setA(long value)"));
}

#[test]
fn bitset_anonymous_padding_is_skipped_but_offsets_advance() {
    let files = genfiles("bitset P { bitfield<2> a; bitfield<3>; bitfield<4> b; };");
    let f = find(&files, "P");
    // a@0, padding@2 (3 wide), b@5
    assert!(f.source.contains("padding: width=3 offset=2"));
    assert!(f.source.contains("offset=5"));
    assert!(f.source.contains("public int getA()"));
    assert!(f.source.contains("public int getB()"));
}

// ============================================================================
// `@value`-Enum
// ============================================================================

#[test]
fn enum_value_overrides_emit_explicit_int() {
    let files = genfiles("enum Color { @value(0) RED, @value(2) GREEN, @value(4) BLUE };");
    let f = find(&files, "Color");
    assert!(f.source.contains("RED(0),"));
    assert!(f.source.contains("GREEN(2),"));
    assert!(f.source.contains("BLUE(4);"));
}

#[test]
fn enum_value_partial_overrides_continue_from_override() {
    // After @value(10), the next implicit ordinal is 11.
    let files = genfiles("enum X { A, @value(10) B, C };");
    let f = find(&files, "X");
    assert!(f.source.contains("A(0),"));
    assert!(f.source.contains("B(10),"));
    assert!(f.source.contains("C(11);"));
}

#[test]
fn enum_value_non_ascending_is_allowed() {
    // @value-Spec gives us full freedom — non-monotonic is legal.
    let files = genfiles("enum X { @value(5) A, @value(2) B };");
    let f = find(&files, "X");
    assert!(f.source.contains("A(5),"));
    assert!(f.source.contains("B(2);"));
}

#[test]
fn enum_without_value_keeps_sequential_ordinals() {
    let files = genfiles("enum Color { RED, GREEN, BLUE };");
    let f = find(&files, "Color");
    assert!(f.source.contains("RED(0),"));
    assert!(f.source.contains("GREEN(1),"));
    assert!(f.source.contains("BLUE(2);"));
}

// ============================================================================
// Multi-Inheritance via Interface-Pattern
// ============================================================================

#[test]
fn base_emits_companion_interface_descendant_does_not() {
    let files = genfiles("struct A { long x; }; struct B : A { long y; };");
    // A is a base of B → companion `AInterface.java` IS emitted.
    let names: Vec<&str> = files.iter().map(|f| f.class_name.as_str()).collect();
    assert!(names.contains(&"A"));
    assert!(names.contains(&"B"));
    assert!(names.contains(&"AInterface"));
    // B has no descendant → no `BInterface`.
    assert!(!names.contains(&"BInterface"));
}

#[test]
fn two_level_chain_emits_grandparent_interface_clause() {
    // A : B, B : C → A extends B implements CInterface, ...
    let files =
        genfiles("struct C { long c; }; struct B : C { long b; }; struct A : B { long a; };");
    let a = find(&files, "A");
    assert!(a.source.contains("public class A extends B"));
    assert!(a.source.contains("CInterface"));
    // B is base of A → companion BInterface.
    assert!(files.iter().any(|f| f.class_name == "BInterface"));
    // C is base of B → companion CInterface.
    assert!(files.iter().any(|f| f.class_name == "CInterface"));
}

#[test]
fn three_level_chain_emits_two_grandparent_interfaces() {
    // A : B : C : D — A extends B implements CInterface, DInterface
    let files = genfiles(
        "struct D { long d; }; struct C : D { long c; }; \
         struct B : C { long b; }; struct A : B { long a; };",
    );
    let a = find(&files, "A");
    assert!(a.source.contains("public class A extends B"));
    assert!(a.source.contains("CInterface"));
    assert!(a.source.contains("DInterface"));
}

#[test]
fn companion_interface_lists_getter_signatures() {
    let files = genfiles("struct Parent { long x; string s; }; struct Child : Parent { long y; };");
    let pi = find(&files, "ParentInterface");
    assert!(pi.source.contains("public interface ParentInterface"));
    assert!(pi.source.contains("int getX();"));
    assert!(pi.source.contains("String getS();"));
}

// ============================================================================
// TopicType-Marker
// ============================================================================

#[test]
fn top_level_struct_implements_topic_type() {
    let files = genfiles("struct S { long x; };");
    let f = find(&files, "S");
    assert!(
        f.source
            .contains("implements org.omg.dds.topic.TopicType<S>")
    );
}

#[test]
fn nested_struct_does_not_implement_topic_type() {
    let files = genfiles("@nested struct Inner { long x; };");
    let f = find(&files, "Inner");
    assert!(!f.source.contains("TopicType"));
    // and carries the @Nested type-annotation.
    assert!(f.source.contains("@org.zerodds.types.Nested"));
}

#[test]
fn struct_with_base_inherits_topic_type_from_parent() {
    // TS-3-Finding 4 (gefixt 2026-05-01): bei Inheritance wird
    // `TopicType<T>` nur am Wurzel-Type emittiert. Java verbietet
    // `Child extends Base<Base> implements TopicType<Child>` weil
    // beide TopicType-Generic-Args kollidieren. Spec DDS-Java-PSM
    // betrachtet TopicType als Marker-Interface — Sub-Classes erben
    // den Marker via `extends Parent` und sind weiterhin
    // `instanceof TopicType<Parent>`.
    let files = genfiles("struct Parent { long x; }; struct Child : Parent { long y; };");
    let parent = find(&files, "Parent");
    let child = find(&files, "Child");
    assert!(
        parent.source.contains("TopicType<Parent>"),
        "Parent must implement TopicType (Wurzel der Kette)"
    );
    assert!(child.source.contains("extends Parent"));
    assert!(
        !child.source.contains("TopicType"),
        "Child must NOT re-implement TopicType — inherits via extends Parent"
    );
}

// ============================================================================
// Annotation-Bridge
// ============================================================================

#[test]
fn key_annotation_emits_java_annotation() {
    let files = genfiles("struct S { @key long id; long val; };");
    let f = find(&files, "S");
    assert!(f.source.contains("@org.zerodds.types.Key"));
}

#[test]
fn id_annotation_includes_value() {
    let files = genfiles("struct S { @id(7) long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("@org.zerodds.types.Id(7)"));
}

#[test]
fn must_understand_annotation_emits() {
    let files = genfiles("struct S { @must_understand long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("@org.zerodds.types.MustUnderstand"));
}

#[test]
fn external_annotation_emits() {
    let files = genfiles("struct S { @external long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("@org.zerodds.types.External"));
}

#[test]
fn optional_annotation_emits_marker_in_addition_to_optional_type() {
    let files = genfiles("struct S { @optional long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("@org.zerodds.types.Optional"));
    assert!(f.source.contains("java.util.Optional"));
}

#[test]
fn extensibility_final_emits_type_annotation() {
    let files = genfiles("@final struct S { long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains(
        "@org.zerodds.types.Extensibility(\
        org.zerodds.types.Extensibility.Kind.FINAL)"
    ));
}

#[test]
fn extensibility_appendable_explicit_form() {
    let files = genfiles("@extensibility(APPENDABLE) struct S { long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("Extensibility.Kind.APPENDABLE"));
}

#[test]
fn extensibility_mutable_emits_type_annotation() {
    let files = genfiles("@mutable struct S { long x; };");
    let f = find(&files, "S");
    assert!(f.source.contains("Extensibility.Kind.MUTABLE"));
}

#[test]
fn nested_annotation_on_enum_is_emitted() {
    let files = genfiles("@nested enum E { A };");
    let f = find(&files, "E");
    assert!(f.source.contains("@org.zerodds.types.Nested"));
}

#[test]
fn key_id_and_optional_appear_in_deterministic_order() {
    let files = genfiles("struct S { @optional @id(3) @key long x; };");
    let f = find(&files, "S");
    let blob = &f.source;
    let key_pos = blob.find("@org.zerodds.types.Key").expect("Key");
    let id_pos = blob.find("@org.zerodds.types.Id(3)").expect("Id");
    let opt_pos = blob.find("@org.zerodds.types.Optional").expect("Optional");
    assert!(key_pos < id_pos, "Key before Id");
    assert!(id_pos < opt_pos, "Id before Optional");
}

#[test]
fn multiple_annotations_combined_test() {
    // Big mixed scenario hits the full bridge.
    let files = genfiles(
        "@mutable struct S { \
           @key @id(1) long topic_key; \
           @optional @must_understand @external long maybe; \
         };",
    );
    let blob = join(&files);
    assert!(blob.contains("Extensibility.Kind.MUTABLE"));
    assert!(blob.contains("@org.zerodds.types.Key"));
    assert!(blob.contains("@org.zerodds.types.Id(1)"));
    assert!(blob.contains("@org.zerodds.types.Optional"));
    assert!(blob.contains("@org.zerodds.types.MustUnderstand"));
    assert!(blob.contains("@org.zerodds.types.External"));
}