Skip to content

Commit 6cd91ba

Browse files
committed
added flag to disable generate field
1 parent 15977df commit 6cd91ba

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
275275
final String structType;
276276
if (nullableArraySize == null) {
277277
structType = structBaseTypeName;
278-
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
278+
if (this.builder.generateFields) {
279+
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
280+
}
279281
processSkipRemainingFlag();
280282
processSkipRemainingFlagForWriting("this." + structName);
281283
this.getCurrentStruct().getReadFunc().indent()
@@ -284,7 +286,9 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
284286
this.getCurrentStruct().getWriteFunc().indent().print(structName).println(".write(Out);");
285287
} else {
286288
structType = structBaseTypeName + " []";
287-
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
289+
if (this.builder.generateFields) {
290+
this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println();
291+
}
288292
processSkipRemainingFlag();
289293
processSkipRemainingFlagForWriting("this." + structName);
290294
if ("-1".equals(arraySizeIn)) {
@@ -357,12 +361,16 @@ public void visitPrimitiveField(final int offsetInCompiledBlock, final int primi
357361

358362
if (nullableArraySize == null) {
359363
textFieldType = type.asJavaSingleFieldType();
360-
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
364+
if (this.builder.generateFields) {
365+
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
366+
}
361367
getCurrentStruct().getReadFunc().println(String.format("this.%s = %s;", fieldName, type.makeReaderForSingleField(NAME_INPUT_STREAM, byteOrder)));
362368
getCurrentStruct().getWriteFunc().print(type.makeWriterForSingleField(NAME_OUTPUT_STREAM, "this." + fieldName, byteOrder)).println(";");
363369
} else {
364370
textFieldType = type.asJavaArrayFieldType() + " []";
365-
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
371+
if (this.builder.generateFields) {
372+
getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName);
373+
}
366374
getCurrentStruct().getReadFunc().printf("this.%s = %s;%n", fieldName, type.makeReaderForArray(NAME_INPUT_STREAM, arraySizeIn, byteOrder));
367375
if (readWholeStreamAsArray) {
368376
getCurrentStruct().getWriteFunc().print(type.makeWriterForArrayWithUnknownSize(NAME_OUTPUT_STREAM, "this." + fieldName, byteOrder)).println(";");
@@ -432,7 +440,9 @@ public void visitBitField(final int offsetInCompiledBlock, final JBBPNamedFieldI
432440
}
433441

434442
final String fieldType = nullableArraySize == null ? "byte" : "byte []";
435-
getCurrentStruct().getFields().indent().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
443+
if (this.builder.generateFields) {
444+
getCurrentStruct().getFields().indent().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
445+
}
436446

437447
if (nullableNameFieldInfo != null && this.builder.addGettersSetters) {
438448
registerGetterSetter(fieldType, fieldName, true);
@@ -468,7 +478,9 @@ public void visitCustomField(final int offsetInCompiledBlock, final JBBPFieldTyp
468478
final String specialFieldName_fieldNameInfo = specialFieldName + "FieldInfo";
469479
final String specialFieldName_typeParameterContainer = specialFieldName + "TypeParameter";
470480

471-
this.getCurrentStruct().getFields().printf("%s JBBPAbstractField %s;%n", fieldModifier, fieldName);
481+
if (this.builder.generateFields) {
482+
this.getCurrentStruct().getFields().printf("%s JBBPAbstractField %s;%n", fieldModifier, fieldName);
483+
}
472484

473485
if (nullableNameFieldInfo != null) {
474486
this.specialSection.printf("private static final JBBPNamedFieldInfo %s = %s;%n",
@@ -535,7 +547,9 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
535547
final String fieldType;
536548
if (readWholeStreamIntoArray || nullableArraySizeEvaluator != null) {
537549
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>";
538-
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
550+
if (this.builder.generateFields) {
551+
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
552+
}
539553

540554
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
541555
fieldName,
@@ -560,7 +574,9 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
560574

561575
} else {
562576
fieldType = "JBBPAbstractField";
563-
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
577+
if (this.builder.generateFields) {
578+
this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
579+
}
564580

565581
this.getCurrentStruct().getReadFunc().printf("%s = %s;%n",
566582
fieldName,
@@ -941,10 +957,17 @@ public static final class Builder {
941957
* Text to be inserted into custom section of the resut class.
942958
*/
943959
private String mainClassCustomText;
960+
/**
961+
* Flag to generate fields in result class file.
962+
*
963+
* @since 1.4.0
964+
*/
965+
private boolean generateFields;
944966

945967
private Builder(final JBBPParser parser) {
946968
this.srcParser = parser;
947969
this.parserFlags = parser.getFlags();
970+
this.generateFields = true;
948971
}
949972

950973
private void assertNonLocked() {
@@ -1076,6 +1099,18 @@ public Builder setHeadComment(final String text) {
10761099
return this;
10771100
}
10781101

1102+
/**
1103+
* Disable generate fields, useful if some super class extended and its fields should be used instead of generated ones.
1104+
* If disable then all code will be generated but without class fields. By default field generate is enabled.
1105+
*
1106+
* @return the builder instance, must not be null
1107+
* @since 1.4.0
1108+
*/
1109+
public Builder disableGenerateFields() {
1110+
this.generateFields = false;
1111+
return this;
1112+
}
1113+
10791114
/**
10801115
* Build converter with provided parameters. NB! It locks builder parameters and they can't be changed in future.
10811116
*

jbbp/src/test/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJava6ConverterReadWriteTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,45 @@ private byte[] loadResource(final String name) throws Exception {
6161
}
6262
}
6363

64+
public static abstract class TestSuperclass {
65+
protected String str;
66+
protected String[] strarr;
67+
protected float flt;
68+
protected float[] fltarr;
69+
protected double dbl;
70+
protected double[] dblarr;
71+
}
72+
73+
@Test
74+
public void testReadWrite_ExtendsSuperClassAndUseItsFields() throws Exception {
75+
final JBBPParser parser = JBBPParser.prepare("stringj str; stringj [2] strarr; floatj flt; floatj [2] fltarr; doublej dbl; doublej [2] dblarr;");
76+
final String text = JBBPToJava6Converter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassPackage(PACKAGE_NAME).disableGenerateFields().setSuperClass(TestSuperclass.class.getCanonicalName()).setAddGettersSetters(true).build().convert();
77+
System.out.println(text);
78+
final String fullClassName = PACKAGE_NAME + '.' + CLASS_NAME;
79+
final ClassLoader classLoader = saveAndCompile(new JavaClassContent(fullClassName, text));
80+
81+
final Object instance = ReflectUtils.newInstance(classLoader.loadClass(fullClassName));
82+
assertTrue(instance instanceof TestSuperclass);
83+
84+
callRead(instance, new byte[] {
85+
0,
86+
2, 49, 50, 1, 51,
87+
1, 2, 3, 4,
88+
5, 6, 7, 8, 9, 10, 11, 12,
89+
1, 2, 3, 4, 5, 6, 7, 8,
90+
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
91+
});
92+
93+
final TestSuperclass parsed = (TestSuperclass) instance;
94+
95+
assertNull(null, parsed.str);
96+
assertArrayEquals(new String[] {"12", "3"}, parsed.strarr);
97+
assertEquals(2.3879393E-38f, parsed.flt);
98+
assertArrayEquals(new float[] {6.301941E-36f, 1.661634E-33f}, parsed.fltarr);
99+
assertEquals(8.20788039913184E-304d, parsed.dbl);
100+
assertArrayEquals(new double[] {4.0383818836028145E-265d, 1.9074368412237584E-226d}, parsed.dblarr);
101+
}
102+
64103
@Test
65104
public void testReaWrite_StructMappedToInterface_Array_GettersSettersOn() throws Exception {
66105
final JBBPParser parser = JBBPParser.prepare("z { x { y [_] { byte a;}}}");
@@ -158,12 +197,12 @@ public void testReadWite_String_SingleValue() throws Exception {
158197
@Test
159198
public void testReadWite_Bit_SingleValueWhichLengthCalclatedThrouhExpression() throws Exception {
160199
final Object instance = compileAndMakeInstance("ubyte a; ubyte b; bit:(a+b) c;");
161-
final byte[] etalon = new byte[] {1, 2, (byte)0xB4};
200+
final byte[] etalon = new byte[] {1, 2, (byte) 0xB4};
162201

163202
callRead(instance, etalon.clone());
164203

165204
assertEquals(4, getField(instance, "c", Byte.class).intValue());
166-
assertArrayEquals(new byte[]{1, 2, 4}, callWrite(instance));
205+
assertArrayEquals(new byte[] {1, 2, 4}, callWrite(instance));
167206
}
168207

169208
@Test

0 commit comments

Comments
 (0)