Java Code Examples for com.igormaznitsa.jbbp.model.JBBPFieldStruct#findFieldForType()

The following examples show how to use com.igormaznitsa.jbbp.model.JBBPFieldStruct#findFieldForType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testParse_SeveralPrimitiveFields() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("bit:4; bit:4; bool; byte; ubyte; short; ushort; int; long;");
  final JBBPFieldStruct result = parser.parse(new byte[] {0x12, 1, 87, (byte) 0xF3, 1, 2, (byte) 0xFE, 4, 6, 7, 8, 9, (byte) 0xFF, 1, 2, 3, 5, 6, 7, 8, 9});

  assertEquals(2, result.findFirstFieldForType(JBBPFieldBit.class).getAsInt());
  assertEquals(1, result.findLastFieldForType(JBBPFieldBit.class).getAsInt());

  try {
    result.findFieldForType(JBBPFieldBit.class);
    fail("Must throw JBBPTooManyFieldsFoundException");
  } catch (JBBPTooManyFieldsFoundException ex) {
    assertEquals(2, ex.getNumberOfFoundInstances());
  }

  assertTrue(result.findFieldForType(JBBPFieldBoolean.class).getAsBool());
  assertEquals(87, result.findFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0xF3, result.findFieldForType(JBBPFieldUByte.class).getAsInt());
  assertEquals(0x0102, result.findFieldForType(JBBPFieldShort.class).getAsInt());
  assertEquals(0xFE04, result.findFieldForType(JBBPFieldUShort.class).getAsInt());
  assertEquals(0x06070809, result.findFieldForType(JBBPFieldInt.class).getAsInt());
  assertEquals(0xFF01020305060708L, result.findFieldForType(JBBPFieldLong.class).getAsLong());
}
 
Example 2
Source File: JBBPMapper.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
@SuppressWarnings("varargs")
private static void processFieldOfMappedClass(
    final MappedFieldRecord record,
    final JBBPFieldStruct rootStructure,
    final Object instance,
    final JBBPMapperCustomFieldProcessor customFieldProcessor,
    final int flags,
    final Function<Class<?>, Object>... instantiators
) {
  if (record.binAnnotation.custom()) {
    JBBPUtils.assertNotNull(customFieldProcessor, "There is a custom mapping field, in the case you must provide a custom mapping field processor");
    final Object value = customFieldProcessor.prepareObjectForMapping(rootStructure, record.binAnnotation, record.mappingField);
    MappedFieldRecord.setFieldValue(instance, record.setter, record.mappingField, null, value);
  } else {
    final JBBPAbstractField binField;

    if (record.fieldPath.length() == 0) {
      binField = record.fieldName.length() == 0 ? rootStructure.findFieldForType(record.fieldType.getFieldClass()) : rootStructure.findFieldForNameAndType(record.fieldName, record.fieldType.getFieldClass());
    } else {
      binField = rootStructure.findFieldForPathAndType(record.fieldPath, record.fieldType.getFieldClass());
    }

    if (binField == null) {
      if ((flags & FLAG_IGNORE_MISSING_VALUES) != 0) {
        return;
      }
      throw new JBBPMapperException("Can't find value for mapping field [" + record.mappingField + ']', null, record.mappingClass, record.mappingField, null);
    }

    if (record.bitWideField && record.mappedBitNumber != JBBPBitNumber.BITS_8 && ((BitEntity) binField).getBitWidth() != record.mappedBitNumber) {
      throw new JBBPMapperException("Can't map mapping field because wrong field bitness [" + record.mappedBitNumber + "!=" + ((BitEntity) binField).getBitWidth().getBitNumber() + ']', null, record.mappingClass, record.mappingField, null);
    }
    record.proc.apply(record, rootStructure, instance, customFieldProcessor, binField, flags, instantiators);
  }
}