File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
main/java/com/igormaznitsa/jbbp/utils
test/java/com/igormaznitsa/jbbp/utils Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -1521,6 +1521,7 @@ protected BinFieldContainer collectAnnotatedFields(final Class<?> annotatedClass
15211521 if ((f .getModifiers () & (Modifier .NATIVE | Modifier .STATIC | Modifier .FINAL | Modifier .PRIVATE | Modifier .TRANSIENT )) == 0 ) {
15221522 final Bin binAnno = f .getAnnotation (Bin .class );
15231523 if (binAnno != null || defautBin != null ) {
1524+ validateAnnotatedField (defautBin , binAnno , f );
15241525 final Class <?> type = f .getType ().isArray () ? f .getType ().getComponentType () : f .getType ();
15251526 if (type .isPrimitive () || type == String .class ) {
15261527 final Bin foundBin = binAnno == null ? defautBin : binAnno ;
@@ -1548,6 +1549,15 @@ protected BinFieldContainer collectAnnotatedFields(final Class<?> annotatedClass
15481549 return result ;
15491550 }
15501551
1552+ protected void validateAnnotatedField (final Bin defaultBin , final Bin fieldBin , final Field field ) {
1553+ final Bin thebin = fieldBin == null ? defaultBin : fieldBin ;
1554+ final boolean emptyExtra = thebin .extra ().trim ().length () == 0 ;
1555+
1556+ if ((thebin .type () == BinType .UNDEFINED && field .getType ().isArray () || thebin .type ().name ().endsWith ("_ARRAY" )) && emptyExtra ) {
1557+ throw new IllegalArgumentException (field .toString () + ": missing array length expression in Bin#extra" );
1558+ }
1559+ }
1560+
15511561 /**
15521562 * Convert an annotated class into its JBBP DSL representation.
15531563 * <b>NB!</b> the method creates structure bases on class name, so that it can't be used for auto-mapping
Original file line number Diff line number Diff line change @@ -498,6 +498,38 @@ class Internal {
498498 assertEquals ("Test{int a;<int b;int c;d[a+b]{short a;short[8] b;}}" , Begin ().AnnotatedClass (Test .class ).End ());
499499 }
500500
501+ @ Test
502+ public void testReportedIssue_21_IAEforEmptyExtraAttributeForArrayField () {
503+ class BreakJBBPDslBuilderChild {
504+ @ Bin (outOrder = 1 , comment = "Reserved" , type = BinType .BYTE )
505+ public byte reserved ;
506+ }
507+
508+ class BreakJBBPDslBuilderParent {
509+ @ Bin (outOrder = 1 )
510+ public BreakJBBPDslBuilderChild [] breakJBBPDslBuilderChildArray ;
511+ }
512+
513+ class BreakJBBPDslBuilderArrayField {
514+ @ Bin (outOrder = 1 , type = BinType .BYTE_ARRAY )
515+ public byte [] bytes ;
516+ }
517+
518+ try {
519+ Begin ().AnnotatedClass (BreakJBBPDslBuilderParent .class ).End ();
520+ fail ();
521+ } catch (IllegalArgumentException ex ) {
522+ assertTrue (ex .getMessage ().contains ("Bin#extra" ));
523+ }
524+
525+ try {
526+ Begin ().AnnotatedClass (BreakJBBPDslBuilderArrayField .class ).End ();
527+ fail ();
528+ } catch (IllegalArgumentException ex ) {
529+ assertTrue (ex .getMessage ().contains ("Bin#extra" ));
530+ }
531+ }
532+
501533 @ Test
502534 public void testReportedIssue_20_NPEforOutBitNumber () throws Exception {
503535 class BreakJBBPDslBuilder {
You can’t perform that action at this time.
0 commit comments