Skip to content

Commit 16024d9

Browse files
committed
improved Java6 converter
1 parent 9756299 commit 16024d9

1 file changed

Lines changed: 63 additions & 27 deletions

File tree

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

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
358358
if (nullableArraySize == null) {
359359
structType = structBaseTypeName;
360360
if (this.builder.generateFields) {
361-
printField(nullableNameFieldInfo, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
361+
printField(nullableNameFieldInfo, false, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
362362
}
363363

364364
processSkipRemainingFlag();
@@ -371,7 +371,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
371371
} else {
372372
structType = structBaseTypeName + " []";
373373
if (this.builder.generateFields) {
374-
printField(nullableNameFieldInfo, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
374+
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
375375
}
376376
processSkipRemainingFlag();
377377
processSkipRemainingFlagForWriting("this." + structName);
@@ -458,7 +458,7 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
458458
getCurrentStruct().getFields().printf("@Bin(name=\"%s\")", nameFieldInfo.getFieldName());
459459
}
460460
}
461-
printField(nameFieldInfo, getCurrentStruct().getFields(), FieldType.VAL, fieldModifier, textFieldType, fieldName);
461+
printField(nameFieldInfo, false, getCurrentStruct().getFields(), FieldType.VAL, fieldModifier, textFieldType, fieldName);
462462
}
463463

464464
final String valIn = evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, expression, this.flagSet, false);
@@ -473,17 +473,29 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
473473
}
474474

475475
private void printField(final JBBPNamedFieldInfo nullableFieldInfo,
476+
final boolean array,
476477
final JavaSrcTextBuffer buffer,
477-
final FieldType fieldType,
478+
final FieldType nullableFieldType,
478479
final String modifier,
479480
final String type,
480481
final String name) {
481482
if (this.builder.addBinAnnotations) {
482483
final String binName = nullableFieldInfo == null ? null : nullableFieldInfo.getFieldName();
483-
if (binName == null) {
484-
buffer.printf("@Bin%n");
484+
485+
if (nullableFieldType == null
486+
|| nullableFieldType.getBinType() == BinType.UNDEFINED
487+
|| nullableFieldType.getBinTypeArray() == BinType.UNDEFINED) {
488+
if (binName == null) {
489+
buffer.printf("@Bin%n");
490+
} else {
491+
buffer.printf("@Bin(name=\"%s\")%n", binName);
492+
}
485493
} else {
486-
buffer.printf("@Bin(name=\"%s\")%n", binName);
494+
if (binName == null) {
495+
buffer.printf("@Bin(type=BinType.%s)%n", array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType());
496+
} else {
497+
buffer.printf("@Bin(name=\"%s\",type=BinType.%s)%n", binName, array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType());
498+
}
487499
}
488500
}
489501
buffer.printf("%s %s %s;%n", modifier, type, name);
@@ -556,6 +568,7 @@ public void visitPrimitiveField(
556568
if (this.builder.generateFields) {
557569
printField(
558570
nullableNameFieldInfo,
571+
false,
559572
getCurrentStruct().getFields(),
560573
type,
561574
fieldModifier,
@@ -568,7 +581,7 @@ public void visitPrimitiveField(
568581
} else {
569582
textFieldType = type.asJavaArrayFieldType() + " []";
570583
if (this.builder.generateFields) {
571-
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), type, fieldModifier, textFieldType, fieldName);
584+
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields(), type, fieldModifier, textFieldType, fieldName);
572585
}
573586
getCurrentStruct().getReadFunc().printf("this.%s = %s;%n", fieldName, type.makeReaderForArray(NAME_INPUT_STREAM, arraySizeIn, byteOrder));
574587
if (readWholeStreamAsArray) {
@@ -775,7 +788,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
775788
if (readWholeStreamIntoArray || nullableArraySizeEvaluator != null) {
776789
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>";
777790
if (this.builder.generateFields) {
778-
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
791+
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
779792
}
780793

781794
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
@@ -802,7 +815,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
802815
} else {
803816
fieldType = "JBBPAbstractField";
804817
if (this.builder.generateFields) {
805-
printField(nullableNameFieldInfo, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
818+
printField(nullableNameFieldInfo, false, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
806819
}
807820

808821
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
@@ -1046,22 +1059,24 @@ public void visitActionItem(final int offsetInCompiledBlock, final int actionTyp
10461059
}
10471060

10481061
private enum FieldType {
1049-
BOOL(CODE_BOOL, "boolean", "boolean", "%s.readBoolean()", "%s.readBoolArray(%s)", "%s.write(%s ? 1 : 0)", "for(int I=0;I<%3$s;I++){%1$s.write(%2$s[I] ? 1 : 0);}", "for(int I=0;I<%2$s.length;I++){%1$s.write(%2$s[I] ? 1 : 0);}"),
1050-
BYTE(CODE_BYTE, "byte", "byte", "(byte)%s.readByte()", "%s.readByteArray(%s, %s)", "%s.write(%s)", "%1$s.writeBytes(%2$s, %3$s, %4$s)", "%1$s.writeBytes(%2$s, %2$s.length, %3$s)"),
1051-
UBYTE(CODE_UBYTE, "char", "byte", "(char)(%s.readByte() & 0xFF)", "%s.readByteArray(%s, %s)", "%s.write(%s)", "%1$s.writeBytes(%2$s, %3$s, %4$s)", "%1$s.writeBytes(%2$s, %2$s.length, %3$s)"),
1052-
SHORT(CODE_SHORT, "short", "short", "(short)%s.readUnsignedShort(%s)", "%s.readShortArray(%s,%s)", "%s.writeShort(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeShort(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeShort(%2$s[I],%3$s);}"),
1053-
USHORT(CODE_USHORT, "char", "char", "(char)%s.readUnsignedShort(%s)", "%s.readUShortArray(%s,%s)", "%s.writeShort(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeShort(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeShort(%2$s[I],%3$s);}"),
1054-
INT(CODE_INT, "int", "int", "%s.readInt(%s)", "%s.readIntArray(%s,%s)", "%s.writeInt(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeInt(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeInt(%2$s[I],%3$s);}"),
1055-
LONG(CODE_LONG, "long", "long", "%s.readLong(%s)", "%s.readLongArray(%s,%s)", "%s.writeLong(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeLong(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeLong(%2$s[I],%3$s);}"),
1056-
CUSTOM(-1, "", "", "", "", "", "", ""),
1057-
VAR(-2, "", "", "", "", "", "", ""),
1058-
VAL(-3, "int", "", "", "", "", "", ""),
1059-
BIT(-4, "", "", "", "", "", "", ""),
1060-
FLOAT(-5, "float", "float", "%s.readFloat(%s)", "%s.readFloatArray(%s,%s)", "%s.writeFloat(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeFloat(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeFloat(%2$s[I],%3$s);}"),
1061-
DOUBLE(-6, "double", "double", "%s.readDouble(%s)", "%s.readDoubleArray(%s,%s)", "%s.writeDouble(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeDouble(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeDouble(%2$s[I],%3$s);}"),
1062-
STRING(-7, "String", "String", "%s.readString(%s)", "%s.readStringArray(%s,%s)", "%s.writeString(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeString(%2$s[I],%4$s);}", "%1$s.writeStringArray(%2$s,%3$s)"),
1063-
UNKNOWN(Integer.MIN_VALUE, "", "", "", "", "", "", "");
1064-
1062+
BOOL(BinType.BOOL, BinType.BOOL_ARRAY, CODE_BOOL, "boolean", "boolean", "%s.readBoolean()", "%s.readBoolArray(%s)", "%s.write(%s ? 1 : 0)", "for(int I=0;I<%3$s;I++){%1$s.write(%2$s[I] ? 1 : 0);}", "for(int I=0;I<%2$s.length;I++){%1$s.write(%2$s[I] ? 1 : 0);}"),
1063+
BYTE(BinType.BYTE, BinType.BYTE_ARRAY, CODE_BYTE, "byte", "byte", "(byte)%s.readByte()", "%s.readByteArray(%s, %s)", "%s.write(%s)", "%1$s.writeBytes(%2$s, %3$s, %4$s)", "%1$s.writeBytes(%2$s, %2$s.length, %3$s)"),
1064+
UBYTE(BinType.UBYTE, BinType.UBYTE_ARRAY, CODE_UBYTE, "char", "byte", "(char)(%s.readByte() & 0xFF)", "%s.readByteArray(%s, %s)", "%s.write(%s)", "%1$s.writeBytes(%2$s, %3$s, %4$s)", "%1$s.writeBytes(%2$s, %2$s.length, %3$s)"),
1065+
SHORT(BinType.SHORT, BinType.SHORT_ARRAY, CODE_SHORT, "short", "short", "(short)%s.readUnsignedShort(%s)", "%s.readShortArray(%s,%s)", "%s.writeShort(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeShort(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeShort(%2$s[I],%3$s);}"),
1066+
USHORT(BinType.USHORT, BinType.USHORT_ARRAY, CODE_USHORT, "char", "char", "(char)%s.readUnsignedShort(%s)", "%s.readUShortArray(%s,%s)", "%s.writeShort(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeShort(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeShort(%2$s[I],%3$s);}"),
1067+
INT(BinType.INT, BinType.INT_ARRAY, CODE_INT, "int", "int", "%s.readInt(%s)", "%s.readIntArray(%s,%s)", "%s.writeInt(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeInt(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeInt(%2$s[I],%3$s);}"),
1068+
LONG(BinType.LONG, BinType.LONG_ARRAY, CODE_LONG, "long", "long", "%s.readLong(%s)", "%s.readLongArray(%s,%s)", "%s.writeLong(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeLong(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeLong(%2$s[I],%3$s);}"),
1069+
CUSTOM(BinType.UNDEFINED, BinType.UNDEFINED, -1, "", "", "", "", "", "", ""),
1070+
VAR(BinType.UNDEFINED, BinType.UNDEFINED, -2, "", "", "", "", "", "", ""),
1071+
VAL(BinType.UNDEFINED, BinType.UNDEFINED, -3, "int", "", "", "", "", "", ""),
1072+
BIT(BinType.BIT, BinType.BIT_ARRAY, -4, "", "", "", "", "", "", ""),
1073+
FLOAT(BinType.FLOAT, BinType.FLOAT_ARRAY, -5, "float", "float", "%s.readFloat(%s)", "%s.readFloatArray(%s,%s)", "%s.writeFloat(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeFloat(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeFloat(%2$s[I],%3$s);}"),
1074+
DOUBLE(BinType.DOUBLE, BinType.DOUBLE_ARRAY, -6, "double", "double", "%s.readDouble(%s)", "%s.readDoubleArray(%s,%s)", "%s.writeDouble(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeDouble(%2$s[I],%4$s);}", "for(int I=0;I<%2$s.length;I++){%1$s.writeDouble(%2$s[I],%3$s);}"),
1075+
STRING(BinType.UNDEFINED, BinType.UNDEFINED, -7, "String", "String", "%s.readString(%s)", "%s.readStringArray(%s,%s)", "%s.writeString(%s,%s)", "for(int I=0;I<%3$s;I++){%1$s.writeString(%2$s[I],%4$s);}", "%1$s.writeStringArray(%2$s,%3$s)"),
1076+
UNKNOWN(BinType.UNDEFINED, BinType.UNDEFINED, Integer.MIN_VALUE, "", "", "", "", "", "", "");
1077+
1078+
private final BinType binType;
1079+
private final BinType binTypeArray;
10651080
private final int code;
10661081
private final String javaSingleType;
10671082
private final String javaArrayType;
@@ -1071,7 +1086,20 @@ private enum FieldType {
10711086
private final String methodWriteArray;
10721087
private final String methodWriteArrayWithUnknownSize;
10731088

1074-
FieldType(final int code, final String javaSingleType, final String javaArrayType, final String readOne, final String readArray, final String writeOne, final String writeArray, final String writeArrayWithUnknownSize) {
1089+
FieldType(
1090+
final BinType binType,
1091+
final BinType binTypeArray,
1092+
final int code,
1093+
final String javaSingleType,
1094+
final String javaArrayType,
1095+
final String readOne,
1096+
final String readArray,
1097+
final String writeOne,
1098+
final String writeArray,
1099+
final String writeArrayWithUnknownSize
1100+
) {
1101+
this.binTypeArray = binTypeArray;
1102+
this.binType = binType;
10751103
this.code = code;
10761104
this.methodWriteArrayWithUnknownSize = writeArrayWithUnknownSize;
10771105
this.javaSingleType = javaSingleType;
@@ -1131,6 +1159,14 @@ String makeWriterForArrayWithUnknownSize(final String streamName, final String f
11311159
assertNotUnknown();
11321160
return String.format(this.methodWriteArrayWithUnknownSize, streamName, fieldName, "JBBPByteOrder." + byteOrder.name());
11331161
}
1162+
1163+
BinType getBinType() {
1164+
return this.binType;
1165+
}
1166+
1167+
BinType getBinTypeArray() {
1168+
return this.binTypeArray;
1169+
}
11341170
}
11351171

11361172
/**

0 commit comments

Comments
 (0)