Skip to content

Commit 46c02cd

Browse files
committed
refactoring
1 parent 7ba139e commit 46c02cd

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public final CompiledBlockVisitor visit() {
135135
final JBBPIntegerValueEvaluator evaluator = extraFieldNumAsExpr ? extraFieldValueEvaluator : new IntConstValueEvaluator(JBBPUtils.unpackInt(compiledData, positionAtCompiledBlock));
136136
if (altFileType) {
137137
if (theCode == JBBPCompiler.CODE_SKIP) {
138-
visitValField(theOffset, name, evaluator);
138+
visitValField(theOffset, byteOrder, name, evaluator);
139139
} else {
140140
throw new Error("Unexpected code:" + theCode);
141141
}
@@ -147,7 +147,7 @@ public final CompiledBlockVisitor visit() {
147147

148148
case JBBPCompiler.CODE_BIT: {
149149
final JBBPIntegerValueEvaluator numberOfBits = extraFieldNumAsExpr ? extraFieldValueEvaluator : new IntConstValueEvaluator(JBBPUtils.unpackInt(compiledData, positionAtCompiledBlock));
150-
visitBitField(theOffset, name, numberOfBits, arraySizeEvaluator);
150+
visitBitField(theOffset, byteOrder, name, numberOfBits, arraySizeEvaluator);
151151
}
152152
break;
153153

@@ -163,7 +163,7 @@ public final CompiledBlockVisitor visit() {
163163
break;
164164

165165
case JBBPCompiler.CODE_STRUCT_START: {
166-
visitStructureStart(theOffset, name, arraySizeEvaluator);
166+
visitStructureStart(theOffset, byteOrder, name, arraySizeEvaluator);
167167
}
168168
break;
169169

@@ -212,11 +212,12 @@ public void visitActionItem(int offsetInCompiledBlock, int actionType, JBBPInteg
212212
* Visit field contains virtual field with VAL type.
213213
*
214214
* @param offsetInCompiledBlock offset in the compiled block
215+
* @param byteOrder byteOrder
215216
* @param nameFieldInfo name of the field, must not be null
216217
* @param expression expression to calculate value
217218
* @since 1.4.0
218219
*/
219-
public void visitValField(int offsetInCompiledBlock, JBBPNamedFieldInfo nameFieldInfo, JBBPIntegerValueEvaluator expression) {
220+
public void visitValField(int offsetInCompiledBlock, JBBPByteOrder byteOrder, JBBPNamedFieldInfo nameFieldInfo, JBBPIntegerValueEvaluator expression) {
220221
}
221222

222223
/**
@@ -257,10 +258,10 @@ public void visitVarField(int offsetInCompiledBlock, JBBPNamedFieldInfo nullable
257258
public void visitCustomField(int offsetInCompiledBlock, JBBPFieldTypeParameterContainer notNullFieldType, JBBPNamedFieldInfo nullableNameFieldInfo, JBBPByteOrder byteOrder, boolean readWholeStream, JBBPIntegerValueEvaluator nullableArraySizeEvaluator, JBBPIntegerValueEvaluator extraDataValueEvaluator) {
258259
}
259260

260-
public void visitBitField(int offsetInCompiledBlock, JBBPNamedFieldInfo nullableNameFieldInfo, JBBPIntegerValueEvaluator notNullFieldSize, JBBPIntegerValueEvaluator nullableArraySize) {
261+
public void visitBitField(int offsetInCompiledBlock, JBBPByteOrder byteOrder, JBBPNamedFieldInfo nullableNameFieldInfo, JBBPIntegerValueEvaluator notNullFieldSize, JBBPIntegerValueEvaluator nullableArraySize) {
261262
}
262263

263-
public void visitStructureStart(int offsetInCompiledBlock, JBBPNamedFieldInfo nullableNameFieldInfo, JBBPIntegerValueEvaluator nullableArraySize) {
264+
public void visitStructureStart(int offsetInCompiledBlock, JBBPByteOrder byteOrder, JBBPNamedFieldInfo nullableNameFieldInfo, JBBPIntegerValueEvaluator nullableArraySize) {
264265
}
265266

266267
public void visitStructureEnd(int offsetInCompiledBlock, JBBPNamedFieldInfo nullableNameFieldInfo) {

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void visitEnd() {
356356
}
357357

358358
@Override
359-
public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPIntegerValueEvaluator nullableArraySize) {
359+
public void visitStructureStart(final int offsetInCompiledBlock, final JBBPByteOrder byteOrder, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPIntegerValueEvaluator nullableArraySize) {
360360
final String structName = (nullableNameFieldInfo == null ? makeAnonymousStructName() : prepFldName(nullableNameFieldInfo.getFieldName())).toLowerCase(Locale.ENGLISH);
361361
final String structBaseTypeName = structName.toUpperCase(Locale.ENGLISH);
362362
final String arraySizeIn = nullableArraySize == null ? null : evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, nullableArraySize, this.flagSet, true);
@@ -379,7 +379,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
379379
if (nullableArraySize == null) {
380380
structType = structBaseTypeName;
381381
if (this.builder.generateFields) {
382-
printField(nullableNameFieldInfo, false, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
382+
printField(nullableNameFieldInfo, byteOrder, false, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
383383
}
384384

385385
processSkipRemainingFlag();
@@ -392,7 +392,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
392392
} else {
393393
structType = structBaseTypeName + " []";
394394
if (this.builder.generateFields) {
395-
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
395+
printField(nullableNameFieldInfo, byteOrder, true, getCurrentStruct().getFields().indent(), null, fieldModifier, structType, structName);
396396
}
397397
processSkipRemainingFlag();
398398
processSkipRemainingFlagForWriting("this." + structName);
@@ -477,7 +477,7 @@ public void visitStructureEnd(final int offsetInCompiledBlock, final JBBPNamedFi
477477
}
478478

479479
@Override
480-
public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nameFieldInfo, final JBBPIntegerValueEvaluator expression) {
480+
public void visitValField(final int offsetInCompiledBlock, final JBBPByteOrder byteOrder, final JBBPNamedFieldInfo nameFieldInfo, final JBBPIntegerValueEvaluator expression) {
481481
final String fieldName = prepFldName(nameFieldInfo.getFieldName());
482482
FieldType type = FieldType.VAL;
483483

@@ -496,7 +496,7 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
496496
getCurrentStruct().getFields().printf("@Bin(name=\"%s\")", nameFieldInfo.getFieldName());
497497
}
498498
}
499-
printField(nameFieldInfo, false, getCurrentStruct().getFields(), FieldType.VAL, fieldModifier, textFieldType, fieldName);
499+
printField(nameFieldInfo, byteOrder, false, getCurrentStruct().getFields(), FieldType.VAL, fieldModifier, textFieldType, fieldName);
500500
}
501501

502502
final String valIn = evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, expression, this.flagSet, false);
@@ -511,6 +511,7 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
511511
}
512512

513513
private void printField(final JBBPNamedFieldInfo nullableFieldInfo,
514+
final JBBPByteOrder byteOrder,
514515
final boolean array,
515516
final JavaSrcTextBuffer buffer,
516517
final FieldType nullableFieldType,
@@ -524,22 +525,23 @@ private void printField(final JBBPNamedFieldInfo nullableFieldInfo,
524525
|| nullableFieldType.getBinType() == BinType.UNDEFINED
525526
|| nullableFieldType.getBinTypeArray() == BinType.UNDEFINED) {
526527
if (binName == null) {
527-
buffer.printf("@Bin%n");
528+
buffer.printf("@Bin(outByteOrder=JBBPByteOrder.%s)%n", byteOrder.name());
528529
} else {
529-
buffer.printf("@Bin(name=\"%s\")%n", binName);
530+
buffer.printf("@Bin(name=\"%s\",outByteOrder=JBBPByteOrder.%s)%n", binName, byteOrder.name());
530531
}
531532
} else {
532533
if (binName == null) {
533-
buffer.printf("@Bin(type=BinType.%s)%n", array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType());
534+
buffer.printf("@Bin(type=BinType.%s,outByteOrder=JBBPByteOrder.%s)%n", array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType(), byteOrder.name());
534535
} else {
535-
buffer.printf("@Bin(name=\"%s\",type=BinType.%s)%n", binName, array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType());
536+
buffer.printf("@Bin(name=\"%s\",type=BinType.%s,outByteOrder=JBBPByteOrder.%s)%n", binName, array ? nullableFieldType.getBinTypeArray() : nullableFieldType.getBinType(), byteOrder.name());
536537
}
537538
}
538539
}
539540
buffer.printf("%s %s %s;%n", modifier, type, name);
540541
}
541542

542543
private void printBitField(final boolean array,
544+
final JBBPByteOrder byteOrder,
543545
final JBBPNamedFieldInfo nullableFieldInfo,
544546
final String sizeOfFieldOut,
545547
final JavaSrcTextBuffer buffer,
@@ -549,14 +551,14 @@ private void printBitField(final boolean array,
549551
if (this.builder.addBinAnnotations) {
550552
final String binName = nullableFieldInfo == null ? null : nullableFieldInfo.getFieldName();
551553
if (binName == null) {
552-
buffer.printf("@Bin(type=BinType.%s,outBitNumber=%s)%n",
554+
buffer.printf("@Bin(type=BinType.%s,outBitNumber=%s,outByteOrder=JBBPByteOrder.%s)%n",
553555
(array ? BinType.BIT_ARRAY : BinType.BIT).name(),
554-
sizeOfFieldOut);
556+
sizeOfFieldOut, byteOrder.name());
555557
} else {
556-
buffer.printf("@Bin(name=\"%s\",type=BinType.%s,outBitNumber=%s)%n",
558+
buffer.printf("@Bin(name=\"%s\",type=BinType.%s,outBitNumber=%s,outByteOrder=JBBPByteOrder.%s)%n",
557559
binName,
558560
(array ? BinType.BIT_ARRAY : BinType.BIT).name(),
559-
sizeOfFieldOut);
561+
sizeOfFieldOut, byteOrder.name());
560562
}
561563
}
562564
buffer.printf("%s %s %s;%n", modifier, type, name);
@@ -606,6 +608,7 @@ public void visitPrimitiveField(
606608
if (this.builder.generateFields) {
607609
printField(
608610
nullableNameFieldInfo,
611+
byteOrder,
609612
false,
610613
getCurrentStruct().getFields(),
611614
type,
@@ -619,7 +622,7 @@ public void visitPrimitiveField(
619622
} else {
620623
textFieldType = type.asJavaArrayFieldType() + " []";
621624
if (this.builder.generateFields) {
622-
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields(), type, fieldModifier, textFieldType, fieldName);
625+
printField(nullableNameFieldInfo, byteOrder, true, getCurrentStruct().getFields(), type, fieldModifier, textFieldType, fieldName);
623626
}
624627
getCurrentStruct().getReadFunc().printf("this.%s = %s;%n", fieldName, type.makeReaderForArray(NAME_INPUT_STREAM, arraySizeIn, byteOrder));
625628
if (readWholeStreamAsArray) {
@@ -667,7 +670,7 @@ private void registerMaker(final String rawFieldType, final String fieldName, fi
667670
}
668671

669672
@Override
670-
public void visitBitField(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPIntegerValueEvaluator notNullFieldSize, final JBBPIntegerValueEvaluator nullableArraySize) {
673+
public void visitBitField(final int offsetInCompiledBlock, final JBBPByteOrder byteOrder, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPIntegerValueEvaluator notNullFieldSize, final JBBPIntegerValueEvaluator nullableArraySize) {
671674
final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName() : prepFldName(nullableNameFieldInfo.getFieldName());
672675

673676
registerNamedField(nullableNameFieldInfo, FieldType.BIT);
@@ -713,6 +716,7 @@ public void visitBitField(final int offsetInCompiledBlock, final JBBPNamedFieldI
713716
if (this.builder.generateFields) {
714717
printBitField(
715718
nullableArraySize != null,
719+
byteOrder,
716720
nullableNameFieldInfo,
717721
sizeOfFieldOut,
718722
getCurrentStruct().getFields().indent(),
@@ -803,7 +807,14 @@ public void visitCustomField(final int offsetInCompiledBlock, final JBBPFieldTyp
803807
}
804808

805809
@Override
806-
public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nullableNameFieldInfo, final JBBPByteOrder byteOrder, final boolean readWholeStreamIntoArray, final JBBPIntegerValueEvaluator nullableArraySizeEvaluator, final JBBPIntegerValueEvaluator extraDataValueEvaluator) {
810+
public void visitVarField(
811+
final int offsetInCompiledBlock,
812+
final JBBPNamedFieldInfo nullableNameFieldInfo,
813+
final JBBPByteOrder byteOrder,
814+
final boolean readWholeStreamIntoArray,
815+
final JBBPIntegerValueEvaluator nullableArraySizeEvaluator,
816+
final JBBPIntegerValueEvaluator extraDataValueEvaluator
817+
) {
807818
this.flagSet.set(this.flagSet.get() | FLAG_DETECTED_VAR_FIELDS);
808819

809820
registerNamedField(nullableNameFieldInfo, FieldType.VAR);
@@ -826,7 +837,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
826837
if (readWholeStreamIntoArray || nullableArraySizeEvaluator != null) {
827838
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>";
828839
if (this.builder.generateFields) {
829-
printField(nullableNameFieldInfo, true, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
840+
printField(nullableNameFieldInfo, byteOrder, true, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
830841
}
831842

832843
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
@@ -853,7 +864,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
853864
} else {
854865
fieldType = "JBBPAbstractField";
855866
if (this.builder.generateFields) {
856-
printField(nullableNameFieldInfo, false, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
867+
printField(nullableNameFieldInfo, byteOrder, false, getCurrentStruct().getFields(), FieldType.VAR, fieldModifier, fieldType, fieldName);
857868
}
858869

859870
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",

jbbp/src/main/java/com/igormaznitsa/jbbp/io/AbstractMappedClassFieldObserver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
public abstract class AbstractMappedClassFieldObserver {
4545

4646
/**
47-
* Inside cache to keep outOrder of fields for classes for data output. It is
47+
* Internal cache to keep outOrder of fields for classes for data output. It is
4848
* lazy initialized field.
4949
*/
50-
private static volatile Map<Class<?>, Field[]> cachedClasses;
50+
private static Map<Class<?>, Field[]> cachedClasses;
5151

5252
/**
5353
* Inside auxiliary method to read object field value.

0 commit comments

Comments
 (0)