@@ -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
0 commit comments