@@ -51,6 +51,70 @@ public final class JBBPToJava6Converter extends CompiledBlockVisitor {
5151 private static final int FLAG_DETECTED_VAR_FIELDS = 4 ;
5252 private static final int FLAG_ADD_ASSERT_NOT_NEGATIVE_EXPR = 8 ;
5353
54+ private static final Set <String > RESERVED_JAVA_KEYWORDS ;
55+
56+ static {
57+ final Set <String > reserved = new HashSet <String >();
58+
59+ reserved .add ("abstract" );
60+ reserved .add ("assert" );
61+ reserved .add ("boolean" );
62+ reserved .add ("break" );
63+ reserved .add ("byte" );
64+ reserved .add ("case" );
65+ reserved .add ("catch" );
66+ reserved .add ("char" );
67+ reserved .add ("class" );
68+ reserved .add ("continue" );
69+ reserved .add ("default" );
70+ reserved .add ("do" );
71+ reserved .add ("double" );
72+ reserved .add ("else" );
73+ reserved .add ("enum" );
74+ reserved .add ("extends" );
75+ reserved .add ("final" );
76+ reserved .add ("finally" );
77+ reserved .add ("float" );
78+ reserved .add ("for" );
79+ reserved .add ("if" );
80+ reserved .add ("implements" );
81+ reserved .add ("import" );
82+ reserved .add ("instanceof" );
83+ reserved .add ("int" );
84+ reserved .add ("interface" );
85+ reserved .add ("long" );
86+ reserved .add ("native" );
87+ reserved .add ("new" );
88+ reserved .add ("package" );
89+ reserved .add ("private" );
90+ reserved .add ("protected" );
91+ reserved .add ("public" );
92+ reserved .add ("return" );
93+ reserved .add ("short" );
94+ reserved .add ("static" );
95+ reserved .add ("strictfp" );
96+ reserved .add ("super" );
97+ reserved .add ("switch" );
98+ reserved .add ("synchronized" );
99+ reserved .add ("this" );
100+ reserved .add ("throw" );
101+ reserved .add ("throws" );
102+ reserved .add ("transient" );
103+ reserved .add ("try" );
104+ reserved .add ("void" );
105+ reserved .add ("volatile" );
106+ reserved .add ("while" );
107+ reserved .add ("true" );
108+ reserved .add ("null" );
109+ reserved .add ("false" );
110+ reserved .add ("var" );
111+ reserved .add ("const" );
112+ reserved .add ("goto" );
113+
114+ RESERVED_JAVA_KEYWORDS = Collections .unmodifiableSet (reserved );
115+ }
116+
117+
54118 /**
55119 * Name of the field to be used as link to the root structure instance in
56120 * child structures.
@@ -266,7 +330,7 @@ public void visitEnd() {
266330
267331 @ Override
268332 public void visitStructureStart (final int offsetInCompiledBlock , final JBBPNamedFieldInfo nullableNameFieldInfo , final JBBPIntegerValueEvaluator nullableArraySize ) {
269- final String structName = (nullableNameFieldInfo == null ? makeAnonymousStructName () : nullableNameFieldInfo .getFieldName ()).toLowerCase (Locale .ENGLISH );
333+ final String structName = (nullableNameFieldInfo == null ? makeAnonymousStructName () : prepFldName ( nullableNameFieldInfo .getFieldName () )).toLowerCase (Locale .ENGLISH );
270334 final String structBaseTypeName = structName .toUpperCase (Locale .ENGLISH );
271335 final String arraySizeIn = nullableArraySize == null ? null : evaluatorToString (NAME_INPUT_STREAM , offsetInCompiledBlock , nullableArraySize , this .flagSet , true );
272336 final String arraySizeOut = nullableArraySize == null ? null : evaluatorToString (NAME_OUTPUT_STREAM , offsetInCompiledBlock , nullableArraySize , this .flagSet , true );
@@ -339,14 +403,34 @@ private void processSkipRemainingFlagForWriting(final String structFieldName) {
339403 }
340404 }
341405
406+ private String prepFldName (final String fieldName ) {
407+ String result = fieldName ;
408+ if (RESERVED_JAVA_KEYWORDS .contains (fieldName )) {
409+ result = '_' + result ;
410+ } else if (fieldName .startsWith ("_" )) {
411+ if (fieldName .endsWith ("_" )) {
412+ final String withoutUnderscores = fieldName .substring (1 , fieldName .length () - 1 );
413+ if (RESERVED_JAVA_KEYWORDS .contains (withoutUnderscores )) {
414+ result = '_' + result + anonymousFieldCounter .incrementAndGet () + '_' ;
415+ }
416+ } else {
417+ final String withoutUnderscore = fieldName .substring (1 );
418+ if (RESERVED_JAVA_KEYWORDS .contains (withoutUnderscore )) {
419+ result += '_' ;
420+ }
421+ }
422+ }
423+ return result ;
424+ }
425+
342426 @ Override
343427 public void visitStructureEnd (final int offsetInCompiledBlock , final JBBPNamedFieldInfo nullableNameFieldInfo ) {
344428 this .structStack .remove (0 );
345429 }
346430
347431 @ Override
348432 public void visitValField (final int offsetInCompiledBlock , final JBBPNamedFieldInfo nameFieldInfo , final JBBPIntegerValueEvaluator expression ) {
349- final String fieldName = nameFieldInfo .getFieldName ();
433+ final String fieldName = prepFldName ( nameFieldInfo .getFieldName () );
350434 FieldType type = FieldType .VAL ;
351435
352436 registerNamedField (nameFieldInfo , type );
@@ -372,7 +456,7 @@ public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldI
372456
373457 @ Override
374458 public void visitPrimitiveField (final int offsetInCompiledBlock , final int primitiveType , final JBBPNamedFieldInfo nullableNameFieldInfo , final JBBPByteOrder byteOrder , final boolean readWholeStreamAsArray , final boolean altFieldType , final JBBPIntegerValueEvaluator nullableArraySize ) {
375- final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : nullableNameFieldInfo .getFieldName ();
459+ final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : prepFldName ( nullableNameFieldInfo .getFieldName () );
376460 FieldType type = FieldType .findForCode (primitiveType );
377461
378462 if (altFieldType ) {
@@ -440,7 +524,7 @@ private void registerGetterSetter(final String fieldType, final String fieldName
440524
441525 @ Override
442526 public void visitBitField (final int offsetInCompiledBlock , final JBBPNamedFieldInfo nullableNameFieldInfo , final JBBPIntegerValueEvaluator notNullFieldSize , final JBBPIntegerValueEvaluator nullableArraySize ) {
443- final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : nullableNameFieldInfo .getFieldName ();
527+ final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : prepFldName ( nullableNameFieldInfo .getFieldName () );
444528
445529 registerNamedField (nullableNameFieldInfo , FieldType .BIT );
446530
@@ -513,7 +597,7 @@ public void visitCustomField(final int offsetInCompiledBlock, final JBBPFieldTyp
513597
514598 registerNamedField (nullableNameFieldInfo , FieldType .CUSTOM );
515599
516- final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : nullableNameFieldInfo .getFieldName ();
600+ final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : prepFldName ( nullableNameFieldInfo .getFieldName () );
517601 final String fieldModifier = makeModifier (nullableNameFieldInfo );
518602
519603 final String specialFieldName = makeSpecialFieldName ();
@@ -527,7 +611,7 @@ public void visitCustomField(final int offsetInCompiledBlock, final JBBPFieldTyp
527611 if (nullableNameFieldInfo != null ) {
528612 this .specialSection .printf ("private static final JBBPNamedFieldInfo %s = %s;%n" ,
529613 specialFieldName_fieldNameInfo ,
530- "new JBBPNamedFieldInfo(\" " + nullableNameFieldInfo .getFieldName () + "\" ,\" " + nullableNameFieldInfo .getFieldPath () + "\" ," + nullableNameFieldInfo .getFieldOffsetInCompiledBlock () + ")"
614+ "new JBBPNamedFieldInfo(\" " + prepFldName ( nullableNameFieldInfo .getFieldName () ) + "\" ,\" " + nullableNameFieldInfo .getFieldPath () + "\" ," + nullableNameFieldInfo .getFieldOffsetInCompiledBlock () + ")"
531615 );
532616 }
533617
@@ -572,7 +656,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
572656
573657 registerNamedField (nullableNameFieldInfo , FieldType .VAR );
574658
575- final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : nullableNameFieldInfo .getFieldName ();
659+ final String fieldName = nullableNameFieldInfo == null ? makeAnonymousFieldName () : prepFldName ( nullableNameFieldInfo .getFieldName () );
576660 final String fieldModifier = makeModifier (nullableNameFieldInfo );
577661
578662 final String specialFieldName = makeSpecialFieldName ();
@@ -581,7 +665,7 @@ public void visitVarField(final int offsetInCompiledBlock, final JBBPNamedFieldI
581665 if (nullableNameFieldInfo != null ) {
582666 this .specialSection .printf ("private static final JBBPNamedFieldInfo %s = %s;%n" ,
583667 specialFieldName_fieldNameInfo ,
584- "new JBBPNamedFieldInfo(\" " + nullableNameFieldInfo .getFieldName () + "\" ,\" " + nullableNameFieldInfo .getFieldPath () + "\" ," + nullableNameFieldInfo .getFieldOffsetInCompiledBlock () + ")"
668+ "new JBBPNamedFieldInfo(\" " + prepFldName ( nullableNameFieldInfo .getFieldName () ) + "\" ,\" " + nullableNameFieldInfo .getFieldPath () + "\" ," + nullableNameFieldInfo .getFieldOffsetInCompiledBlock () + ")"
585669 );
586670 }
587671
@@ -1364,7 +1448,7 @@ JavaSrcTextBuffer getGettersSetters() {
13641448 }
13651449 }
13661450
1367- private static final class NamedFieldInfo {
1451+ private final class NamedFieldInfo {
13681452
13691453 final JBBPNamedFieldInfo info ;
13701454 final Struct struct ;
@@ -1377,14 +1461,15 @@ private static final class NamedFieldInfo {
13771461 }
13781462
13791463 String makeSrcPath (final Struct currentStruct ) {
1464+ final String fieldName = prepFldName (info .getFieldName ());
13801465 if (this .struct == currentStruct ) {
1381- return "this." + info . getFieldName () ;
1466+ return "this." + fieldName ;
13821467 } else {
13831468 final String structPath = this .struct .getPath ();
13841469 if (currentStruct .isRoot ()) {
1385- return "this." + (structPath .length () == 0 ? "" : structPath + "." ) + info . getFieldName () ;
1470+ return "this." + (structPath .length () == 0 ? "" : structPath + "." ) + fieldName ;
13861471 } else {
1387- return "this." + NAME_ROOT_STRUCT + '.' + (structPath .length () == 0 ? "" : structPath + "." ) + info . getFieldName () ;
1472+ return "this." + NAME_ROOT_STRUCT + '.' + (structPath .length () == 0 ? "" : structPath + "." ) + fieldName ;
13881473 }
13891474 }
13901475 }
0 commit comments