Skip to content

Commit 0566028

Browse files
committed
added NewLineComment
1 parent 71496fc commit 0566028

File tree

2 files changed

+104
-22
lines changed

2 files changed

+104
-22
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPDslBuilder.java

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ public static JBBPDslBuilder Begin() {
5454
}
5555

5656
protected void addItem(final Item item) {
57-
if (item.name == null || item.name.length() == 0) {
57+
if (item instanceof ItemComment || item.name == null || item.name.length() == 0) {
5858
this.items.add(item);
5959
} else {
6060
int structCounter = 0;
6161
for (int i = this.items.size() - 1; i >= 0; i--) {
6262
final Item itm = this.items.get(i);
6363

64+
if (itm instanceof ItemComment) {
65+
continue;
66+
}
67+
6468
if (itm.type == BinType.STRUCT || itm.type == BinType.STRUCT_ARRAY) {
6569
if (structCounter == 0) {
6670
break;
@@ -1289,13 +1293,25 @@ public JBBPDslBuilder Double(final String name) {
12891293
}
12901294

12911295
/**
1292-
* Add comment.
1296+
* Add comment, in case that a field followed by the comment, the comment will be placed on the same line as field definition.
12931297
*
12941298
* @param text text of comment, can be null
12951299
* @return the builder instance, must not be null
12961300
*/
12971301
public JBBPDslBuilder Comment(final String text) {
1298-
this.addItem(new ItemComment(text == null ? "" : text));
1302+
this.addItem(new ItemComment(text == null ? "" : text, false));
1303+
return this;
1304+
}
1305+
1306+
/**
1307+
* Add comment which will be placed on new line.
1308+
*
1309+
* @param text text of comment, can be null
1310+
* @return the builder instance, must not be null
1311+
* @since 1.4.1
1312+
*/
1313+
public JBBPDslBuilder NewLineComment(final String text) {
1314+
this.addItem(new ItemComment(text == null ? "" : text, true));
12991315
return this;
13001316
}
13011317

@@ -1484,7 +1500,31 @@ public String End(final boolean format) {
14841500
} else if (item instanceof ItemSkip) {
14851501
doTabs(format, buffer, structCounter).append("skip").append(item.sizeExpression == null ? "" : ':' + item.makeExpressionForExtraField(item.sizeExpression)).append(';');
14861502
} else if (item instanceof ItemComment) {
1487-
doTabs(format, buffer, structCounter).append("// ").append(item.name.replace("\n", " "));
1503+
final ItemComment comment = (ItemComment) item;
1504+
final String commentText = comment.getComment().replace("\n", " ");
1505+
if (comment.isNewLine()) {
1506+
if (buffer.length() > 0) {
1507+
final int lastNewLine = buffer.lastIndexOf("\n");
1508+
if (lastNewLine < 0 || buffer.substring(lastNewLine + 1).length() != 0) {
1509+
buffer.append('\n');
1510+
}
1511+
}
1512+
doTabs(format, buffer, structCounter).append("// ").append(commentText);
1513+
} else {
1514+
final String current = buffer.toString();
1515+
if (current.endsWith(";\n") || current.endsWith("}\n")) {
1516+
buffer.setLength(buffer.length() - 1);
1517+
}
1518+
final int lastCommentIndex = buffer.lastIndexOf("//");
1519+
if (lastCommentIndex < 0) {
1520+
buffer.append("// ");
1521+
} else if (buffer.lastIndexOf("\n") > lastCommentIndex) {
1522+
buffer.append("// ");
1523+
} else {
1524+
buffer.append(' ');
1525+
}
1526+
buffer.append(commentText);
1527+
}
14881528
} else {
14891529
throw new IllegalArgumentException("Unexpected item : " + item.getClass().getName());
14901530
}
@@ -1792,6 +1832,10 @@ class Pair {
17921832
}
17931833
}
17941834
}
1835+
final String comment = field.getComment();
1836+
if (comment.length() != 0) {
1837+
this.Comment(comment);
1838+
}
17951839
}
17961840
}
17971841

@@ -1820,7 +1864,7 @@ protected static class Item {
18201864

18211865
Item(final BinType type, final String name, final JBBPByteOrder byteOrder) {
18221866
this.type = type;
1823-
this.name = name == null ? name : assertNameIfNotNull(assertTextNotNullAndTrimmedNotEmpty(name)).trim();
1867+
this.name = name == null ? null : assertNameIfNotNull(assertTextNotNullAndTrimmedNotEmpty(name)).trim();
18241868
this.byteOrder = byteOrder;
18251869
}
18261870

@@ -1900,6 +1944,9 @@ protected String makeExpressionForExtraField(final String expression) {
19001944
}
19011945
}
19021946

1947+
/**
1948+
* Internal auxiliary class to keep found annotated fields.
1949+
*/
19031950
protected static class BinField implements Comparable<BinField> {
19041951

19051952
Bin bin;
@@ -1922,6 +1969,18 @@ protected static class BinField implements Comparable<BinField> {
19221969
this.binCustom = null;
19231970
}
19241971

1972+
String getComment() {
1973+
String result = "";
1974+
if (this.fieldLocalAnnotation) {
1975+
if (this.binCustom != null) {
1976+
result = this.binCustom.comment();
1977+
} else if (this.bin != null) {
1978+
result = this.bin.comment();
1979+
}
1980+
}
1981+
return result;
1982+
}
1983+
19251984
boolean isArrayField() {
19261985
return this.field != null && this.field.getType().isArray();
19271986
}
@@ -2016,8 +2075,8 @@ protected static class BinFieldContainer extends BinField {
20162075

20172076
static BinFieldContainer END_STRUCT = new BinFieldContainer(null, null);
20182077

2019-
BinFieldContainer(final Class<?> klazz, final Bin bin, final boolean binSet, final Field field) {
2020-
super(bin, binSet, field);
2078+
BinFieldContainer(final Class<?> klazz, final Bin bin, final boolean fieldLocalAnnotation, final Field field) {
2079+
super(bin, fieldLocalAnnotation, field);
20212080
this.klazz = klazz;
20222081
}
20232082

@@ -2026,8 +2085,8 @@ protected static class BinFieldContainer extends BinField {
20262085
this.klazz = klazz;
20272086
}
20282087

2029-
BinFieldContainer(final Class<?> klazz, final DslBinCustom binCustom, final boolean binCustomSet, final Field field) {
2030-
super(binCustom, binCustomSet, field);
2088+
BinFieldContainer(final Class<?> klazz, final DslBinCustom binCustom, final boolean fieldLocalAnnotation, final Field field) {
2089+
super(binCustom, fieldLocalAnnotation, field);
20312090
this.klazz = klazz;
20322091
}
20332092

@@ -2043,12 +2102,12 @@ void addContaner(final BinFieldContainer container) {
20432102
this.fields.add(container);
20442103
}
20452104

2046-
void addBinField(final Bin bin, final boolean binSet, final Field field) {
2047-
this.fields.add(new BinField(bin, binSet, field));
2105+
void addBinField(final Bin bin, final boolean fieldLocalAnnotation, final Field field) {
2106+
this.fields.add(new BinField(bin, fieldLocalAnnotation, field));
20482107
}
20492108

2050-
void addBinCustomField(final DslBinCustom bin, final boolean binCustomSet, final Field field) {
2051-
this.fields.add(new BinField(bin, binCustomSet, field));
2109+
void addBinCustomField(final DslBinCustom bin, final boolean fieldLocalAnnotation, final Field field) {
2110+
this.fields.add(new BinField(bin, fieldLocalAnnotation, field));
20522111
}
20532112

20542113
JBBPByteOrder getByteOrder(final BinField field) {
@@ -2073,8 +2132,21 @@ JBBPBitNumber getBitNumber(final BinField field) {
20732132
}
20742133

20752134
protected static class ItemComment extends Item {
2076-
ItemComment(final String text) {
2077-
super(BinType.UNDEFINED, text, JBBPByteOrder.BIG_ENDIAN);
2135+
private final boolean newLine;
2136+
private final String comment;
2137+
2138+
ItemComment(final String text, final boolean newLine) {
2139+
super(BinType.UNDEFINED, "_ignored_because_comment_", JBBPByteOrder.BIG_ENDIAN);
2140+
this.comment = text;
2141+
this.newLine = newLine;
2142+
}
2143+
2144+
String getComment() {
2145+
return this.comment == null ? "" : this.comment;
2146+
}
2147+
2148+
boolean isNewLine() {
2149+
return this.newLine;
20782150
}
20792151
}
20802152

jbbp/src/test/java/com/igormaznitsa/jbbp/utils/JBBPDslBuilderTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public void testType_Var() {
143143
assertEquals("var:(a+b) huzzaa;", Begin().Var("huzzaa", "a+b").End());
144144
}
145145

146+
@Test
147+
public void testComment() {
148+
assertEquals("// Test\n", Begin().Comment("Test").End());
149+
assertEquals("// //\n// Test\n", Begin().Comment("//").Comment("Test").End());
150+
assertEquals("// Test\n// Test2\n", Begin().Comment("Test").Comment("Test2").End());
151+
assertEquals("int a;// Test\n// Test2\n", Begin().Int("a").Comment("Test").Comment("Test2").End());
152+
assertEquals("int a;\n// Test\n// Test2\n", Begin().Int("a").NewLineComment("Test").NewLineComment("Test2").End());
153+
assertEquals("int a;hello{// hello\n}// end hello\n", Begin().Int("a").Struct("hello").Comment("hello").CloseStruct().Comment("end hello").End());
154+
}
155+
146156
@Test
147157
public void testType_VarArray() {
148158
assertEquals("var[1234];", Begin().VarArray(1234).End());
@@ -539,7 +549,7 @@ class BreakJBBPDslBuilder {
539549

540550
final String dsl = Begin().AnnotatedClass(BreakJBBPDslBuilder.class).End();
541551

542-
assertEquals("BreakJBBPDslBuilder{bit:8[4] reserved;}", dsl);
552+
assertEquals("BreakJBBPDslBuilder{bit:8[4] reserved;// Reserved\n}", dsl);
543553

544554
JBBPFieldStruct struct = JBBPParser.prepare(dsl).parse(new byte[] {1, 2, 3, 4});
545555
assertArrayEquals(new byte[] {1, 2, 3, 4}, struct.findFieldForType(JBBPFieldStruct.class).findFieldForType(JBBPFieldArrayBit.class).getArray());
@@ -584,7 +594,7 @@ class Internal {
584594
@Test
585595
public void testAnotatedClass_AnnottatedAllTypes() {
586596
class Test {
587-
@Bin(outOrder = 1, type = BinType.BIT, outBitNumber = JBBPBitNumber.BITS_4)
597+
@Bin(outOrder = 1, type = BinType.BIT, outBitNumber = JBBPBitNumber.BITS_4, comment = "bit field")
588598
byte a;
589599
@Bin(outOrder = 2, outBitNumber = JBBPBitNumber.BITS_2, extra = "123")
590600
byte[] a1;
@@ -625,7 +635,7 @@ class Test {
625635
@Bin(outOrder = 20, extra = "a+b")
626636
String[] l1;
627637

628-
@DslBinCustom(type = "int9", extraExpression = "a+b", arraySizeExpression = "c*d")
638+
@DslBinCustom(type = "int9", extraExpression = "a+b", arraySizeExpression = "c*d", byteOrder = JBBPByteOrder.LITTLE_ENDIAN, comment = "some comment")
629639
int[] cus;
630640

631641
class Some {
@@ -651,8 +661,8 @@ class Internal {
651661
}
652662

653663
assertEquals("Test{\n" +
654-
"\tint9:(a+b)[c*d] cus;\n" +
655-
"\tbit:4 a;\n" +
664+
"\t<int9:(a+b)[c*d] cus;// some comment\n" +
665+
"\tbit:4 a;// bit field\n" +
656666
"\tbyte[123] a1;\n" +
657667
"\tbool b;\n" +
658668
"\tbool[456] b1;\n" +
@@ -688,8 +698,8 @@ class Internal {
688698
"\t}\n" +
689699
"}\n", Begin().AnnotatedClass(Test.class).End(true));
690700

691-
assertEquals("int9:(a+b)[c*d] cus;\n" +
692-
"bit:4 a;\n" +
701+
assertEquals("<int9:(a+b)[c*d] cus;// some comment\n" +
702+
"bit:4 a;// bit field\n" +
693703
"byte[123] a1;\n" +
694704
"bool b;\n" +
695705
"bool[456] b1;\n" +

0 commit comments

Comments
 (0)