raydac/java-binary-block-parser

"Expression is empty" error when using arrays

timmattison opened this issue · 8 comments

I have a parent class:

import com.igormaznitsa.jbbp.mapper.Bin;

public class BreakJBBPDslBuilderParent {
    @Bin(outOrder = 1)
    public BreakJBBPDslBuilderChild[] breakJBBPDslBuilderChildArray;
}

And a child class:

import com.igormaznitsa.jbbp.mapper.Bin;
import com.igormaznitsa.jbbp.mapper.BinType;

public class BreakJBBPDslBuilderChild {
    @Bin(outOrder = 1, comment = "Reserved", type = BinType.BYTE)
    public byte reserved;
}

If I try to use the JBBPDslBuilder on the parent class like this:

JBBPDslBuilder.Begin().AnnotatedClass(BreakJBBPDslBuilderParent.class).End();

I get an IllegalArgumentException with the message "Expression is empty" that is thrown by this line:

throw new IllegalArgumentException("Expression is empty");

Is this the correct way I'd use the JBBPDslBuilder to work with a structure that has arrays in it?

and what do you use as child class? you have provided duplication of parent class instead of child class

if you have an array field then DSL builder needs information about length of the array and it should be provided through expression in extra attribute

 class BreakJBBPDslBuilder {
      @Bin(outOrder = 1, comment = "Reserved", type = BinType.BIT_ARRAY, extra = "4")
      public byte[] reserved;
    }

I will update that in a minute. But the child class should just have a single byte field in it.

Updated the child object. I was using this with the binary code I committed on another branch and the output I get from that is this:

breakJBBPDslBuilderChildArray [_] { BYTE reserved; } 

To me that makes the most sense. Can this be supported? I'd like to combine efforts here and have this wrapped into your library rather than maintain the external interface I built.

If you check out that branch and want to simply run this in a test to see what it does you can use this JUnit test code:

import org.junit.Test;

import java.io.IOException;

public class BinaryTest {
    @Test
    public void test1() throws IOException {
        System.out.println(new BreakJBBPDslBuilderParent().getFormat());
    }
}

I have improved message in the exception to provide more information what does mean the error and how to fix
Bin annotation can be used not only for DSL builder but also for JBBPOut so that it is important to provide all correct info in Bin annotation for a field

If the field is going to be an array without a fixed size (e.g. a structure that repeats 0 or more times) what is the size I should specify there? Do I use the underscore like the DSL?

if array should be read till end of stream then underscore should be used, extra="_"