Skip to content

Commit 9756299

Browse files
committed
added annotation support into Java6 converter
1 parent abcf0ed commit 9756299

2 files changed

Lines changed: 158 additions & 52 deletions

File tree

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJava6Converter.java

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.igormaznitsa.jbbp.compiler.varlen.JBBPIntegerValueEvaluator;
2323
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
2424
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
25+
import com.igormaznitsa.jbbp.mapper.BinType;
2526
import com.igormaznitsa.jbbp.utils.JavaSrcTextBuffer;
2627

2728
import java.util.ArrayList;
@@ -259,6 +260,11 @@ public void visitEnd() {
259260
buffer.println("import java.io.IOException;");
260261
buffer.println("import java.util.*;");
261262

263+
if (this.builder.addBinAnnotations) {
264+
buffer.println("import com.igormaznitsa.jbbp.mapper.Bin;");
265+
buffer.println("import com.igormaznitsa.jbbp.mapper.BinType;");
266+
}
267+
262268
buffer.println();
263269

264270
this.specialSection.println();
@@ -352,7 +358,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
352358
if (nullableArraySize == null) {
353359
structType = structBaseTypeName;
354360
if (this.builder.generateFields) {
355-
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
361+
printField(nullableNameFieldInfo, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
356362
}
357363

358364
processSkipRemainingFlag();
@@ -365,7 +371,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
365371
} else {
366372
structType = structBaseTypeName + " []";
367373
if (this.builder.generateFields) {
368-
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
374+
printField(nullableNameFieldInfo, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
369375
}
370376
processSkipRemainingFlag();
371377
processSkipRemainingFlagForWriting("this." + structName);
@@ -444,7 +450,15 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
444450

445451
final String textFieldType = type.asJavaSingleFieldType();
446452
if (this.builder.generateFields) {
447-
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
453+
if (this.builder.addBinAnnotations) {
454+
final String name = nameFieldInfo.getFieldName();
455+
if (name == null) {
456+
getCurrentStruct().getFields().printf("@Bin");
457+
} else {
458+
getCurrentStruct().getFields().printf("@Bin(name=\"%s\")", nameFieldInfo.getFieldName());
459+
}
460+
}
461+
printField(nameFieldInfo, getCurrentStruct().getFields(), FieldType.VAL, fieldModifier, textFieldType, fieldName);
448462
}
449463

450464
final String valIn = evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, expression, this.flagSet, false);
@@ -458,8 +472,56 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
458472
}
459473
}
460474

475+
private void printField(final JBBPNamedFieldInfo nullableFieldInfo,
476+
final JavaSrcTextBuffer buffer,
477+
final FieldType fieldType,
478+
final String modifier,
479+
final String type,
480+
final String name) {
481+
if (this.builder.addBinAnnotations) {
482+
final String binName = nullableFieldInfo == null ? null : nullableFieldInfo.getFieldName();
483+
if (binName == null) {
484+
buffer.printf("@Bin%n");
485+
} else {
486+
buffer.printf("@Bin(name=\"%s\")%n", binName);
487+
}
488+
}
489+
buffer.printf("%s %s %s;%n", modifier, type, name);
490+
}
491+
492+
private void printBitField(final boolean array,
493+
final JBBPNamedFieldInfo nullableFieldInfo,
494+
final String sizeOfFieldOut,
495+
final JavaSrcTextBuffer buffer,
496+
final String modifier,
497+
final String type,
498+
final String name) {
499+
if (this.builder.addBinAnnotations) {
500+
final String binName = nullableFieldInfo == null ? null : nullableFieldInfo.getFieldName();
501+
if (binName == null) {
502+
buffer.printf("@Bin(type=BinType.%s,outBitNumber=%s)%n",
503+
(array ? BinType.BIT_ARRAY : BinType.BIT).name(),
504+
sizeOfFieldOut);
505+
} else {
506+
buffer.printf("@Bin(name=\"%s\",type=BinType.%s,outBitNumber=%s)%n",
507+
binName,
508+
(array ? BinType.BIT_ARRAY : BinType.BIT).name(),
509+
sizeOfFieldOut);
510+
}
511+
}
512+
buffer.printf("%s %s %s;%n", modifier, type, name);
513+
}
514+
461515
@Override
462-
public void visitPrimitiveField(final int offsetInCompiledBlock, final int primitiveType, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPByteOrder byteOrder, final boolean readWholeStreamAsArray, final boolean altFieldType, final JBBPIntegerValueEvaluator nullableArraySize) {
516+
public void visitPrimitiveField(
517+
final int offsetInCompiledBlock,
518+
final int primitiveType,
519+
final JBBPNamedFieldInfo nullableNameFieldInfo,
520+
final JBBPByteOrder byteOrder,
521+
final boolean readWholeStreamAsArray,
522+
final boolean altFieldType,
523+
final JBBPIntegerValueEvaluator nullableArraySize
524+
) {
463525
final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName() : prepFldName(nullableNameFieldInfo.getFieldName());
464526
FieldType type = FieldType.findForCode(primitiveType);
465527

@@ -492,14 +554,21 @@ public void visitPrimitiveField(final int offsetInCompiledBlock, final int primi
492554
if (nullableArraySize == null) {
493555
textFieldType = type.asJavaSingleFieldType();
494556
if (this.builder.generateFields) {
495-
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
557+
printField(
558+
nullableNameFieldInfo,
559+
getCurrentStruct().getFields(),
560+
type,
561+
fieldModifier,
562+
textFieldType,
563+
fieldName
564+
);
496565
}
497566
getCurrentStruct().getReadFunc().println(String.format("this.%s = %s;", fieldName, type.makeReaderForSingleField(NAME_INPUT_STREAM, byteOrder)));
498567
getCurrentStruct().getWriteFunc().print(type.makeWriterForSingleField(NAME_OUTPUT_STREAM, "this." + fieldName, byteOrder)).println(";");
499568
} else {
500569
textFieldType = type.asJavaArrayFieldType() + " []";
501570
if (this.builder.generateFields) {
502-
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
571+
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), type, fieldModifier, textFieldType, fieldName);
503572
}
504573
getCurrentStruct().getReadFunc().printf("this.%s = %s;%n", fieldName, type.makeReaderForArray(NAME_INPUT_STREAM, arraySizeIn, byteOrder));
505574
if (readWholeStreamAsArray) {
@@ -591,7 +660,15 @@ public void visitBitField(final int offsetInCompiledBlock, final JBBPNamedFieldI
591660

592661
final String fieldType = nullableArraySize == null ? "byte" : "byte []";
593662
if (this.builder.generateFields) {
594-
getCurrentStruct().getFields().indent().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
663+
printBitField(
664+
nullableArraySize != null,
665+
nullableNameFieldInfo,
666+
sizeOfFieldOut,
667+
getCurrentStruct().getFields().indent(),
668+
fieldModifier,
669+
fieldType,
670+
fieldName
671+
);
595672
}
596673

597674
if (nullableNameFieldInfo != null && this.builder.addGettersSetters) {
@@ -698,7 +775,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
698775
if (readWholeStreamIntoArray || nullableArraySizeEvaluator != null) {
699776
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>";
700777
if (this.builder.generateFields) {
701-
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
778+
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
702779
}
703780

704781
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
@@ -725,7 +802,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
725802
} else {
726803
fieldType = "JBBPAbstractField";
727804
if (this.builder.generateFields) {
728-
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
805+
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
729806
}
730807

731808
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
@@ -1082,7 +1159,15 @@ public static final class Builder {
10821159
/**
10831160
* Imternal classes must not be static.
10841161
*/
1085-
public boolean internalClassesNotStatic;
1162+
private boolean internalClassesNotStatic;
1163+
1164+
/**
1165+
* Flag to add Bin annotations to fields.
1166+
*
1167+
* @since 2.0.0
1168+
*/
1169+
private boolean addBinAnnotations;
1170+
10861171
/**
10871172
* The Package name for the result class.
10881173
*/
@@ -1216,6 +1301,17 @@ public Builder setParserFlags(final int value) {
12161301
return this;
12171302
}
12181303

1304+
/**
1305+
* Turn on adding of Bin annotations to generated fields.
1306+
*
1307+
* @return the builder instance, must not be null
1308+
*/
1309+
public Builder addBinAnnotations() {
1310+
assertNonLocked();
1311+
this.addBinAnnotations = true;
1312+
return this;
1313+
}
1314+
12191315
/**
12201316
* Set the package for the generated class.
12211317
*

0 commit comments

Comments
 (0)