Skip to content

fails to map ubyte[] to int[] field even with BinType.UBYTE_ARRAY #50

@alexander-iam

Description

@alexander-iam

When trying to map a ubyte array from a parsed structure to an int[] field in a class using mapTo(), the library throws a RuntimeException with the message Can't set argument to a mapping field.Even though ubyte values in Java are best represented as int (to avoid sign extension issues), the mapper seems to strictly expect a byte[] array, making it impossible to use int[] for direct mapping.

Caused by: java.lang.IllegalArgumentException: Can not set [I field Main$MyData.v to [B
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.igormaznitsa.jbbp.mapper.JBBPMapper.mapArrayField(JBBPMapper.java:560)

public class Main {

    @ToString
    public static class MyData {
        @Bin(type = BinType.UBYTE_ARRAY)
        public int[] v;
    }

    public static void main(String[] args) throws Exception {
        List<Byte> data = new ArrayList<>();
        data.add(Integer.valueOf(255).byteValue());

        JBBPParser parser = JBBPParser.prepare(
                "ubyte[1] v;"
        );

        byte[] bytes = Bytes.toArray(data);
        MyData obj = (MyData) parser.parse(bytes).mapTo(new MyData());
        if (obj.v[0] == 255) {
            System.out.println("ok");
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions