Skip to content

Commit a620546

Browse files
committed
refactoring
1 parent b09e4f9 commit a620546

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/JBBPMapper.java

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -272,39 +272,37 @@ public static <T> T map(final JBBPFieldStruct rootStructure, final T instance, f
272272
@SafeVarargs
273273
@SuppressWarnings("varargs")
274274
private static void processFieldOfMappedClass(
275-
final Field mappingField,
276-
final Class<?> mappingClass,
277-
final Bin binAnnotation,
275+
final MappedFieldRecord record,
278276
final JBBPFieldStruct rootStructure,
279277
final Object instance,
280278
final JBBPMapperCustomFieldProcessor customFieldProcessor,
281279
final int flags,
282280
final Function<Class<?>, Object>... instantiators
283281
) {
284-
if (binAnnotation.custom()) {
282+
if (record.binAnnotation.custom()) {
285283
JBBPUtils.assertNotNull(customFieldProcessor, "There is a custom mapping field, in the case you must provide a custom mapping field processor");
286-
final Object value = customFieldProcessor.prepareObjectForMapping(rootStructure, binAnnotation, mappingField);
287-
setFieldValue(instance, mappingField, null, value);
284+
final Object value = customFieldProcessor.prepareObjectForMapping(rootStructure, record.binAnnotation, record.mappingField);
285+
setFieldValue(instance, record.mappingField, null, value);
288286
} else {
289287
final BinType fieldType;
290288

291-
final JBBPBitNumber mappedBitNumber = binAnnotation.outBitNumber();
289+
final JBBPBitNumber mappedBitNumber = record.binAnnotation.outBitNumber();
292290

293-
if (binAnnotation.type() == BinType.UNDEFINED) {
294-
BinType thetype = BinType.findCompatible(mappingField.getType());
291+
if (record.binAnnotation.type() == BinType.UNDEFINED) {
292+
BinType thetype = BinType.findCompatible(record.mappingField.getType());
295293
if (thetype == null) {
296-
throw new JBBPMapperException("Can't find compatible type for a mapping field", rootStructure, mappingClass, mappingField, null);
294+
throw new JBBPMapperException("Can't find compatible type for a mapping field", rootStructure, record.mappingClass, record.mappingField, null);
297295
} else if (mappedBitNumber.getBitNumber() < 8 && !(thetype == BinType.STRUCT || thetype == BinType.STRUCT_ARRAY)) {
298296
thetype = thetype.isArray() ? BinType.BIT_ARRAY : BinType.BIT;
299297
}
300298
fieldType = thetype;
301299
} else {
302-
fieldType = binAnnotation.type();
300+
fieldType = record.binAnnotation.type();
303301
}
304302
final boolean bitWideField = fieldType == BinType.BIT || fieldType == BinType.BIT_ARRAY;
305303

306-
final String fieldName = binAnnotation.name().length() == 0 ? mappingField.getName() : binAnnotation.name();
307-
final String fieldPath = binAnnotation.path();
304+
final String fieldName = record.binAnnotation.name().length() == 0 ? record.mappingField.getName() : record.binAnnotation.name();
305+
final String fieldPath = record.binAnnotation.path();
308306

309307
final JBBPAbstractField binField;
310308

@@ -318,75 +316,75 @@ private static void processFieldOfMappedClass(
318316
if ((flags & FLAG_IGNORE_MISSING_VALUES) != 0) {
319317
return;
320318
}
321-
throw new JBBPMapperException("Can't find value to be mapped to a mapping field [" + mappingField + ']', null, mappingClass, mappingField, null);
319+
throw new JBBPMapperException("Can't find value to be mapped to a mapping field [" + record.mappingField + ']', null, record.mappingClass, record.mappingField, null);
322320
}
323321

324322
if (bitWideField && mappedBitNumber != JBBPBitNumber.BITS_8 && ((BitEntity) binField).getBitWidth() != mappedBitNumber) {
325-
throw new JBBPMapperException("Can't map value to a mapping field for different field bit width [" + mappedBitNumber + "!=" + ((BitEntity) binField).getBitWidth().getBitNumber() + ']', null, mappingClass, mappingField, null);
323+
throw new JBBPMapperException("Can't map value to a mapping field for different field bit width [" + mappedBitNumber + "!=" + ((BitEntity) binField).getBitWidth().getBitNumber() + ']', null, record.mappingClass, record.mappingField, null);
326324
}
327325

328-
if (mappingField.getType().isArray()) {
326+
if (record.mappingField.getType().isArray()) {
329327
if (binField instanceof JBBPAbstractArrayField) {
330328
if (binField instanceof JBBPFieldArrayStruct) {
331329
// structure
332330
final JBBPFieldArrayStruct structArray = (JBBPFieldArrayStruct) binField;
333-
final Class<?> componentType = mappingField.getType().getComponentType();
331+
final Class<?> componentType = record.mappingField.getType().getComponentType();
334332

335-
Object valueArray = getFieldValue(instance, mappingField);
333+
Object valueArray = getFieldValue(instance, record.mappingField);
336334

337335
valueArray = valueArray == null ? Array.newInstance(componentType, structArray.size()) : valueArray;
338336

339337
if (Array.getLength(valueArray) != structArray.size()) {
340-
throw new JBBPMapperException("Can't map an array field for different expected size [" + Array.getLength(valueArray) + "!=" + structArray.size() + ']', binField, mappingClass, mappingField, null);
338+
throw new JBBPMapperException("Can't map an array field for different expected size [" + Array.getLength(valueArray) + "!=" + structArray.size() + ']', binField, record.mappingClass, record.mappingField, null);
341339
}
342340

343341
for (int i = 0; i < structArray.size(); i++) {
344342
final Object curInstance = Array.get(valueArray, i);
345343
if (curInstance == null) {
346-
Array.set(valueArray, i, map(structArray.getElementAt(i), tryMakeInstance(componentType, binField, instance, mappingField, instantiators), customFieldProcessor, instantiators));
344+
Array.set(valueArray, i, map(structArray.getElementAt(i), tryMakeInstance(componentType, binField, instance, record.mappingField, instantiators), customFieldProcessor, instantiators));
347345
} else {
348346
Array.set(valueArray, i, map(structArray.getElementAt(i), curInstance, customFieldProcessor));
349347
}
350348
}
351-
setFieldValue(instance, mappingField, binField, valueArray);
349+
setFieldValue(instance, record.mappingField, binField, valueArray);
352350
} else {
353351
// primitive
354-
mapArrayField(instance, mappingField, (JBBPAbstractArrayField<?>) binField, binAnnotation.bitOrder() == JBBPBitOrder.MSB0);
352+
mapArrayField(instance, record.mappingField, (JBBPAbstractArrayField<?>) binField, record.binAnnotation.bitOrder() == JBBPBitOrder.MSB0);
355353
}
356354
} else {
357-
throw new JBBPMapperException("Can't map a non-array value to an array mapping field", binField, mappingClass, mappingField, null);
355+
throw new JBBPMapperException("Can't map a non-array value to an array mapping field", binField, record.mappingClass, record.mappingField, null);
358356
}
359357
} else {
360358
if (binField instanceof JBBPNumericField) {
361-
mapNumericField(instance, mappingField, (JBBPNumericField) binField, binAnnotation.bitOrder() == JBBPBitOrder.MSB0);
359+
mapNumericField(instance, record.mappingField, (JBBPNumericField) binField, record.binAnnotation.bitOrder() == JBBPBitOrder.MSB0);
362360
} else if (binField instanceof JBBPFieldString) {
363-
if (mappingField.getType().isPrimitive()) {
364-
throw new JBBPMapperException("Can't map a string to a primitive mapping field", binField, mappingClass, mappingField, null);
361+
if (record.mappingField.getType().isPrimitive()) {
362+
throw new JBBPMapperException("Can't map a string to a primitive mapping field", binField, record.mappingClass, record.mappingField, null);
365363
} else {
366-
setFieldValue(instance, mappingField, binField, ((JBBPFieldString) binField).getAsString());
364+
setFieldValue(instance, record.mappingField, binField, ((JBBPFieldString) binField).getAsString());
367365
}
368366
} else if (binField instanceof JBBPFieldStruct) {
369-
if (mappingField.getType().isPrimitive()) {
370-
throw new JBBPMapperException("Can't map a structure to a primitive mapping field", binField, mappingClass, mappingField, null);
367+
if (record.mappingField.getType().isPrimitive()) {
368+
throw new JBBPMapperException("Can't map a structure to a primitive mapping field", binField, record.mappingClass, record.mappingField, null);
371369
} else {
372-
final Object curValue = getFieldValue(instance, mappingField);
370+
final Object curValue = getFieldValue(instance, record.mappingField);
373371
if (curValue == null) {
374-
setFieldValue(instance, mappingField, binField, map((JBBPFieldStruct) binField, tryMakeInstance(mappingField.getType(), binField, instance, mappingField, instantiators), customFieldProcessor));
372+
setFieldValue(instance, record.mappingField, binField, map((JBBPFieldStruct) binField, tryMakeInstance(record.mappingField.getType(), binField, instance, record.mappingField, instantiators), customFieldProcessor));
375373
} else {
376-
setFieldValue(instance, mappingField, binField, map((JBBPFieldStruct) binField, curValue, customFieldProcessor));
374+
setFieldValue(instance, record.mappingField, binField, map((JBBPFieldStruct) binField, curValue, customFieldProcessor));
377375
}
378376
}
379377
} else {
380378
boolean processed = false;
381-
if (mappingField.getType() == String.class && binField instanceof JBBPAbstractArrayField) {
379+
if (record.mappingField.getType() == String.class && binField instanceof JBBPAbstractArrayField) {
382380
final String convertedValue = convertFieldValueToString((JBBPAbstractArrayField<?>) binField);
383381
if (convertedValue != null) {
384-
setFieldValue(instance, mappingField, binField, convertedValue);
382+
setFieldValue(instance, record.mappingField, binField, convertedValue);
385383
processed = true;
386384
}
387385
}
388386
if (!processed) {
389-
throw new JBBPMapperException("Can't map a field for its value incompatibility", binField, mappingClass, mappingField, null);
387+
throw new JBBPMapperException("Can't map a field for its value incompatibility", binField, record.mappingClass, record.mappingField, null);
390388
}
391389
}
392390
}
@@ -449,9 +447,7 @@ public static <T> T map(final JBBPFieldStruct rootStructure, final T instance, f
449447
mappedAnno = fieldAnno == null ? defaultAnno : fieldAnno;
450448

451449
processFieldOfMappedClass(
452-
mappingField,
453-
mappingClass,
454-
mappedAnno,
450+
new MappedFieldRecord(mappingField, mappingClass, mappedAnno),
455451
rootStructure,
456452
instance,
457453
customFieldProcessor,
@@ -463,6 +459,20 @@ public static <T> T map(final JBBPFieldStruct rootStructure, final T instance, f
463459
return instance;
464460
}
465461

462+
private static final class MappedFieldRecord {
463+
final Field mappingField;
464+
final Class<?> mappingClass;
465+
final Bin binAnnotation;
466+
467+
MappedFieldRecord(final Field mappingField,
468+
final Class<?> mappingClass,
469+
final Bin binAnnotation) {
470+
this.mappingField = mappingField;
471+
this.mappingClass = mappingClass;
472+
this.binAnnotation = binAnnotation;
473+
}
474+
}
475+
466476
private static <T> T tryMakeInstance(
467477
final Class<T> type,
468478
final JBBPAbstractField binField,

0 commit comments

Comments
 (0)