Skip to content

Commit c02ff69

Browse files
committed
added generate of newInstance into Java6 converter
1 parent 16024d9 commit c02ff69

File tree

2 files changed

+122
-44
lines changed

2 files changed

+122
-44
lines changed

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
2424
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
2525
import com.igormaznitsa.jbbp.mapper.BinType;
26+
import com.igormaznitsa.jbbp.mapper.JBBPMapper;
2627
import com.igormaznitsa.jbbp.utils.JavaSrcTextBuffer;
2728

2829
import java.util.ArrayList;
@@ -226,7 +227,8 @@ public void visitStart() {
226227
this.structStack.clear();
227228
this.specialMethods.clean();
228229

229-
this.structStack.add(new Struct(null, this.builder.mainClassName, "public"));
230+
final Struct rootStruct = new Struct(null, this.builder.mainClassName, "public");
231+
this.structStack.add(rootStruct);
230232
}
231233

232234
/**
@@ -319,7 +321,26 @@ public void visitEnd() {
319321

320322
buffer.printJavaDocLinesWithIndent("Generated from JBBP script by internal JBBP Class Source Generator");
321323

322-
this.structStack.get(0).write(buffer,
324+
final Struct rootStruct = this.structStack.get(0);
325+
326+
if (this.builder.genNewInstance) {
327+
rootStruct.misc.println(String.format("public Object %s(Class aClass) {", JBBPMapper.MAKE_CLASS_INSTANCE_METHOD_NAME));
328+
rootStruct.misc.incIndent();
329+
330+
for (final Struct c : rootStruct.children) {
331+
rootStruct.misc.indent().println(String.format("if (aClass == %s.class) {", c.className));
332+
rootStruct.misc.incIndent();
333+
rootStruct.misc.indent().println(String.format("return new %s(this);", c.className));
334+
rootStruct.misc.decIndent();
335+
rootStruct.misc.indent().println("}");
336+
}
337+
338+
rootStruct.misc.indent().println("return null;");
339+
rootStruct.misc.decIndent();
340+
rootStruct.misc.println("}");
341+
}
342+
343+
rootStruct.write(buffer,
323344
hasAbstractMethods ? "abstract" : null,
324345
this.builder.superClass,
325346
this.builder.mainClassImplements,
@@ -435,7 +456,24 @@ private String prepFldName(final String fieldName) {
435456

436457
@Override
437458
public void visitStructureEnd(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nullableNameFieldInfo) {
438-
this.structStack.remove(0);
459+
final Struct struct = this.structStack.remove(0);
460+
461+
if (this.builder.genNewInstance) {
462+
struct.misc.println(String.format("public Object %s(Class aClass) {", JBBPMapper.MAKE_CLASS_INSTANCE_METHOD_NAME));
463+
struct.misc.incIndent();
464+
465+
for (final Struct c : struct.children) {
466+
struct.misc.indent().println(String.format("if (aClass == %s.class) {", c.className));
467+
struct.misc.incIndent();
468+
struct.misc.indent().println(String.format("return new %s(this.%s);", c.className, NAME_ROOT_STRUCT));
469+
struct.misc.decIndent();
470+
struct.misc.indent().println("}");
471+
}
472+
473+
struct.misc.indent().println("return null;");
474+
struct.misc.decIndent();
475+
struct.misc.println("}");
476+
}
439477
}
440478

441479
@Override
@@ -1204,6 +1242,13 @@ public static final class Builder {
12041242
*/
12051243
private boolean addBinAnnotations;
12061244

1245+
/**
1246+
* Flage to add newInstance methods into generated classes.
1247+
*
1248+
* @since 2.0.0
1249+
*/
1250+
private boolean genNewInstance;
1251+
12071252
/**
12081253
* The Package name for the result class.
12091254
*/
@@ -1341,13 +1386,26 @@ public Builder setParserFlags(final int value) {
13411386
* Turn on adding of Bin annotations to generated fields.
13421387
*
13431388
* @return the builder instance, must not be null
1389+
* @since 2.0.0
13441390
*/
13451391
public Builder addBinAnnotations() {
13461392
assertNonLocked();
13471393
this.addBinAnnotations = true;
13481394
return this;
13491395
}
13501396

1397+
/**
1398+
* Turn on generate newInstance methods compatible with JBBPMapper.
1399+
*
1400+
* @return the builder instance, must not be null
1401+
* @since 2.0.0
1402+
*/
1403+
public Builder genNewInstance() {
1404+
assertNonLocked();
1405+
this.genNewInstance = true;
1406+
return this;
1407+
}
1408+
13511409
/**
13521410
* Set the package for the generated class.
13531411
*
@@ -1476,6 +1534,7 @@ private static class Struct {
14761534
private final JavaSrcTextBuffer readFunc = new JavaSrcTextBuffer();
14771535
private final JavaSrcTextBuffer writeFunc = new JavaSrcTextBuffer();
14781536
private final JavaSrcTextBuffer gettersSetters = new JavaSrcTextBuffer();
1537+
private final JavaSrcTextBuffer misc = new JavaSrcTextBuffer();
14791538
private final String path;
14801539

14811540
private Struct(final Struct parent, final String className, final String classModifiers) {
@@ -1594,6 +1653,12 @@ void write(
15941653
buffer.println();
15951654
}
15961655

1656+
if (!this.misc.isEmpty()) {
1657+
buffer.println();
1658+
buffer.printLinesWithIndent(this.misc.toString());
1659+
buffer.println();
1660+
}
1661+
15971662
if (customText != null && customText.length() != 0) {
15981663
buffer.printCommentLinesWithIndent("------ Custom section START");
15991664
buffer.printLinesWithIndent(customText);
@@ -1605,6 +1670,10 @@ void write(
16051670

16061671
}
16071672

1673+
JavaSrcTextBuffer getMisc() {
1674+
return this.misc;
1675+
}
1676+
16081677
JavaSrcTextBuffer getWriteFunc() {
16091678
return this.writeFunc;
16101679
}

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

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,24 @@
3232

3333
public class JBBPToJBBPToJava6ConverterCompilationTest extends AbstractJBBPToJava6ConverterTest {
3434

35-
private static String makeSources(final JBBPParser parser, final String classComment, final boolean useSetterGetter, final boolean addBinAnnotations) {
35+
private static String makeSources(
36+
final JBBPParser parser,
37+
final String classComment,
38+
final boolean useSetterGetter,
39+
final boolean addBinAnnotations,
40+
boolean nonStaticInnerClasses) {
3641
final JBBPToJava6Converter.Builder result = JBBPToJava6Converter.makeBuilder(parser)
3742
.setMainClassPackage(PACKAGE_NAME)
3843
.setMainClassName(CLASS_NAME)
3944
.setHeadComment(classComment)
4045
.setAddGettersSetters(useSetterGetter);
4146

47+
if (nonStaticInnerClasses) {
48+
result.doInternalClassesNonStatic();
49+
}
50+
4251
if (addBinAnnotations) {
43-
result.addBinAnnotations();
52+
result.addBinAnnotations().genNewInstance();
4453
}
4554

4655
return result.build()
@@ -55,14 +64,14 @@ private void assertCompilation(final String classSrc) throws Exception {
5564
@Test
5665
public void testVarNamesAsJavaTypes() throws Exception {
5766
final JBBPParser parser = JBBPParser.prepare("ubyte;int integer; int number; int try; int byte; int _byte; int _byte_; int char; int short; int long; int double; int float; int [long+double+char] string;");
58-
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", true, true));
67+
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", true, true, false));
5968
}
6069

6170
@Test
6271
public void testExpression() throws Exception {
6372
final JBBPParser parser = JBBPParser.prepare("bit:8 bitf; var somevar; bool bbb; long aaa; ubyte kkk; {{int lrn; {int [(lrn/aaa*1*(2*somevar-4)&$joomla)/(100%9>>bitf)&56|~kkk^78&bbb];}}}");
64-
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", false, false));
65-
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", true, true));
73+
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", false, false, false));
74+
assertCompilation(makeSources(parser, "some multiline text\nto be added into header", true, true, true));
6675
}
6776

6877
@Test
@@ -206,57 +215,57 @@ public void testZ80snap1() throws Exception {
206215
+ "<short reg_de; <short reg_bc_alt; <short reg_de_alt; <short reg_hl_alt; byte reg_a_alt; byte reg_f_alt; <short reg_iy; <short reg_ix; byte iff; byte iff2;"
207216
+ "emulFlags{bit:2 interruptmode; bit:1 issue2emulation; bit:1 doubleintfreq; bit:2 videosync; bit:2 inputdevice;}"
208217
+ "byte [_] data;");
209-
assertCompilation(makeSources(parser, null, false, false));
210-
assertCompilation(makeSources(parser, null, true, false));
218+
assertCompilation(makeSources(parser, null, false, false, false));
219+
assertCompilation(makeSources(parser, null, true, false, false));
211220
}
212221

213222
@Test
214223
public void testSinglePrimitiveNamedFields() throws Exception {
215224
final JBBPParser parser = JBBPParser.prepare("bit a;byte b;ubyte c;short d;ushort e;bool f;int g;long h;");
216-
assertCompilation(makeSources(parser, null, false, false));
217-
assertCompilation(makeSources(parser, null, true, false));
225+
assertCompilation(makeSources(parser, null, false, false, false));
226+
assertCompilation(makeSources(parser, null, true, false, false));
218227
}
219228

220229
@Test
221230
public void testSinglePrimitiveAnonymousFields() throws Exception {
222231
final JBBPParser parser = JBBPParser.prepare("bit;byte;ubyte;short;ushort;bool;int;long;");
223-
assertCompilation(makeSources(parser, null, false, false));
224-
assertCompilation(makeSources(parser, null, true, false));
232+
assertCompilation(makeSources(parser, null, false, false, false));
233+
assertCompilation(makeSources(parser, null, true, false, false));
225234
}
226235

227236
@Test
228237
public void testSinglePrimitiveAnonymousArrayFields() throws Exception {
229238
final JBBPParser parser = JBBPParser.prepare("bit[1];byte[2];ubyte[3];short[4];ushort[5];bool[6];int[7];long[8];");
230-
assertCompilation(makeSources(parser, null, false, false));
231-
assertCompilation(makeSources(parser, null, true, false));
239+
assertCompilation(makeSources(parser, null, false, false, false));
240+
assertCompilation(makeSources(parser, null, true, false, false));
232241
}
233242

234243
@Test
235244
public void testActions() throws Exception {
236245
final JBBPParser parser = JBBPParser.prepare("reset$$;skip:8;align:22;");
237-
assertCompilation(makeSources(parser, null, false, false));
238-
assertCompilation(makeSources(parser, null, true, false));
246+
assertCompilation(makeSources(parser, null, false, false, false));
247+
assertCompilation(makeSources(parser, null, true, false, false));
239248
}
240249

241250
@Test
242251
public void testStruct() throws Exception {
243252
final JBBPParser parser = JBBPParser.prepare("int;{byte;ubyte;{long;}}");
244-
assertCompilation(makeSources(parser, null, false, false));
245-
assertCompilation(makeSources(parser, null, true, true));
253+
assertCompilation(makeSources(parser, null, false, false, false));
254+
assertCompilation(makeSources(parser, null, true, true, false));
246255
}
247256

248257
@Test
249258
public void testPrimitiveArrayInsideStructArray() throws Exception {
250259
final JBBPParser parser = JBBPParser.prepare("ubyte len; {ubyte[len];} ubyte [_] rest;");
251-
assertCompilation(makeSources(parser, null, false, false));
252-
assertCompilation(makeSources(parser, null, true, false));
260+
assertCompilation(makeSources(parser, null, false, false, false));
261+
assertCompilation(makeSources(parser, null, true, false, false));
253262
}
254263

255264
@Test
256265
public void testExternalValueInExpression() throws Exception {
257266
final JBBPParser parser = JBBPParser.prepare("ubyte len; <int [len*2+$ex] hello;");
258-
assertCompilation(makeSources(parser, null, false, false));
259-
assertCompilation(makeSources(parser, null, true, false));
267+
assertCompilation(makeSources(parser, null, false, false, false));
268+
assertCompilation(makeSources(parser, null, true, false, false));
260269
}
261270

262271
@Test
@@ -278,15 +287,15 @@ public JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder
278287
}
279288
});
280289

281-
assertCompilation(makeSources(parser, null, false, false));
282-
assertCompilation(makeSources(parser, null, true, false));
290+
assertCompilation(makeSources(parser, null, false, false, false));
291+
assertCompilation(makeSources(parser, null, true, false, false));
283292
}
284293

285294
@Test
286295
public void testVarType() throws Exception {
287296
final JBBPParser parser = JBBPParser.prepare("var alpha; var [$$] beta;");
288-
assertCompilation(makeSources(parser, null, false, false));
289-
assertCompilation(makeSources(parser, null, true, false));
297+
assertCompilation(makeSources(parser, null, false, false, false));
298+
assertCompilation(makeSources(parser, null, true, false, false));
290299
}
291300

292301
@Test
@@ -295,36 +304,36 @@ public void testAllVariantsWithLinksToExternalStructures() throws Exception {
295304
+ "byte alpha; ubyte beta; short gamma; ushort delta; bool epsilon; int teta; long longField; var varField;"
296305
+ "floatj flt1; doublej dbl1;"
297306
+ "struct1 { byte someByte; struct2 {bit:3 [34*someByte<<1+$ext] data;} }");
298-
assertCompilation(makeSources(parser, null, false, false));
299-
assertCompilation(makeSources(parser, null, true, false));
307+
assertCompilation(makeSources(parser, null, false, false, false));
308+
assertCompilation(makeSources(parser, null, true, false, false));
300309
}
301310

302311
@Test
303312
public void testValFields() throws Exception {
304313
final JBBPParser parser = JBBPParser.prepare("ubyte a; ubyte b; val:(a+b*2) v; byte [v] data;");
305-
assertCompilation(makeSources(parser, null, false, false));
306-
assertCompilation(makeSources(parser, null, true, false));
314+
assertCompilation(makeSources(parser, null, false, false, false));
315+
assertCompilation(makeSources(parser, null, true, false, false));
307316
}
308317

309318
@Test
310319
public void testStringFields() throws Exception {
311320
final JBBPParser parser = JBBPParser.prepare("stringj str; stringj [5] strarr; stringj [_] all;");
312-
assertCompilation(makeSources(parser, null, false, false));
313-
assertCompilation(makeSources(parser, null, true, false));
321+
assertCompilation(makeSources(parser, null, false, false, false));
322+
assertCompilation(makeSources(parser, null, true, false, false));
314323
}
315324

316325
@Test
317326
public void testStringFieldAsLength_CompilationErrorForStringFieldInArithmeticException() throws Exception {
318327
final JBBPParser parser = JBBPParser.prepare("stringj str; stringj [str] strarr; stringj [_] all;");
319-
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false)));
320-
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false)));
328+
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false, false)));
329+
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false, false)));
321330
}
322331

323332
@Test
324333
public void testStringFieldInExpression_CompilationErrorForStringFieldInArithmeticException() throws Exception {
325334
final JBBPParser parser = JBBPParser.prepare("stringj str; byte a; stringj [str+a] strarr; stringj [_] all;");
326-
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false)));
327-
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false)));
335+
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, false, false, false)));
336+
assertThrows(Exception.class, () -> assertCompilation(makeSources(parser, null, true, false, false)));
328337
}
329338

330339
@Test
@@ -339,15 +348,15 @@ public void testPngParsing() throws Exception {
339348
+ " int crc;"
340349
+ "}"
341350
);
342-
assertCompilation(makeSources(parser, null, false, false));
343-
assertCompilation(makeSources(parser, null, true, false));
351+
assertCompilation(makeSources(parser, null, false, false, false));
352+
assertCompilation(makeSources(parser, null, true, false, false));
344353
}
345354

346355
@Test
347356
public void testPrimitiveFieldsInExpression() throws Exception {
348357
final JBBPParser parser = JBBPParser.prepare("long lfield; int ifield; byte bfield; ggg {ubyte ubfield; short shfield;} ushort ushfield; bit:4 bitfield; byte [bfield*ggg.shfield<<bitfield-ggg.ubfield&ushfield%lfield/ifield] array;");
349-
assertCompilation(makeSources(parser, null, false, false));
350-
assertCompilation(makeSources(parser, null, true, false));
358+
assertCompilation(makeSources(parser, null, false, false, false));
359+
assertCompilation(makeSources(parser, null, true, false, false));
351360
}
352361

353362
@Test
@@ -368,8 +377,8 @@ public JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder
368377
return null;
369378
}
370379
});
371-
assertCompilation(makeSources(parser, null, false, false));
372-
assertCompilation(makeSources(parser, null, true, false));
380+
assertCompilation(makeSources(parser, null, false, false, false));
381+
assertCompilation(makeSources(parser, null, true, false, false));
373382
}
374383

375384
@Test

0 commit comments

Comments
 (0)