Skip to content

Commit fbd3799

Browse files
committed
[Java] Handle primitive arrays correctly in the generated DTO code.
1 parent cb4eb1f commit fbd3799

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaDtoGenerator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,18 +1039,23 @@ private void generateArrayEncodeWith(
10391039

10401040
if (primitiveType == PrimitiveType.CHAR)
10411041
{
1042-
sb.append(indent).append("encoder.").append(toLowerFirstChar(propertyName)).append("(")
1042+
sb.append(indent).append("encoder.").append(formattedPropertyName).append("(")
10431043
.append("dto.").append(formattedPropertyName).append("());\n");
10441044
}
10451045
else
10461046
{
10471047
final String javaTypeName = javaTypeName(primitiveType);
10481048
sb.append(indent).append(javaTypeName).append("[] ").append(formattedPropertyName).append(" = ")
10491049
.append("dto.").append(formattedPropertyName).append("();\n")
1050-
.append(indent).append("for (int i = 0; i < ").append(formattedPropertyName).append(".length; i++)\n")
1050+
.append(indent).append("if (null != ").append(formattedPropertyName).append(")\n")
10511051
.append(indent).append("{\n")
1052-
.append(indent).append(INDENT).append("encoder.").append(formattedPropertyName).append("(")
1052+
.append(indent).append(indent).append("for (int i = 0; i < ").append(formattedPropertyName)
1053+
.append(".length; i++)\n")
1054+
.append(indent).append(indent).append("{\n")
1055+
.append(indent).append(indent).append(INDENT).append("encoder.").append(formattedPropertyName)
1056+
.append("(")
10531057
.append("i, ").append(formattedPropertyName).append("[i]);\n")
1058+
.append(indent).append(indent).append("}\n")
10541059
.append(indent).append("}\n");
10551060
}
10561061
}

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/DtoTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void dtoWithKeywords()
8282
dto.abstract$((byte)-81);
8383
dto.false$((byte)0);
8484
dto.falsE((byte)1);
85+
dto.import$("char nine");
86+
dto.void$(new int[] {1, 2, 3});
8587
final ExpandableArrayBuffer input = new ExpandableArrayBuffer();
8688
final KeywordsEncoder encoder = new KeywordsEncoder();
8789
encoder.wrap(input, 0);

sbe-tool/src/test/resources/dto-test-schema.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<type name="someNumbers" primitiveType="uint32" length="5"/>
3333
<type name="Percentage" primitiveType="int8" minValue="0" maxValue="100"/>
3434
<type name="TwoBytes" primitiveType="int8" length="2" sinceVersion="2"/>
35+
<type name="CharArray9" primitiveType="char" length="9" />
36+
<type name="intArray" primitiveType="int32" length="5" />
3537
<composite name="Booster">
3638
<enum name="BoostType" encodingType="char">
3739
<validValue name="TURBO">T</validValue>
@@ -133,7 +135,7 @@
133135
<field name="goto" id="22" type="int8"/>
134136
<field name="if" id="23" type="int8"/>
135137
<field name="implements" id="24" type="int8"/>
136-
<field name="import" id="25" type="int8"/>
138+
<field name="Import" id="25" type="CharArray9"/>
137139
<field name="instanceof" id="26" type="int8"/>
138140
<field name="int" id="27" type="int8"/>
139141
<field name="interface" id="28" type="int8"/>
@@ -147,7 +149,7 @@
147149
<field name="return" id="36" type="int8"/>
148150
<field name="short" id="37" type="int8"/>
149151
<field name="static" id="38" type="int8"/>
150-
<field name="strictfp" id="39" type="int8"/>
152+
<field name="strictfp" id="39" type="CharArray9"/>
151153
<field name="super" id="40" type="int8"/>
152154
<field name="switch" id="41" type="int8"/>
153155
<field name="synchronized" id="42" type="int8"/>
@@ -156,7 +158,7 @@
156158
<field name="throws" id="45" type="int8"/>
157159
<field name="transient" id="46" type="int8"/>
158160
<field name="try" id="47" type="int8"/>
159-
<field name="void" id="48" type="int8"/>
161+
<field name="void" id="48" type="intArray"/>
160162
<field name="volatile" id="49" type="int8"/>
161163
<field name="while" id="50" type="int8"/>
162164
<field name="null" id="51" type="int8"/>

0 commit comments

Comments
 (0)