Skip to content

Commit af413a0

Browse files
vjkoskelacowtowncoder
authored andcommitted
Declare wrapAndThrows to return an IOException to allow callers to use in order to improve readability and code coverage. It also has the side-effect of ensuring all paths through wrapAndThrow actually throw because they would otherwise be required to return an IOException. (#1871)
1 parent 96c69de commit af413a0

File tree

5 files changed

+41
-44
lines changed

5 files changed

+41
-44
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object bean
285285
try {
286286
prop.deserializeAndSet(p, ctxt, bean);
287287
} catch (Exception e) {
288-
wrapAndThrow(e, bean, prop.getName(), ctxt);
288+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
289289
}
290290
ix = p.nextFieldName(_fieldMatcher);
291291
}
@@ -396,7 +396,7 @@ private final Object _vanillaDeserialize(JsonParser p,
396396
try {
397397
prop.deserializeAndSet(p, ctxt, bean);
398398
} catch (Exception e) {
399-
wrapAndThrow(e, bean, p.currentName(), ctxt);
399+
throw wrapAndThrow(e, bean, p.currentName(), ctxt);
400400
}
401401

402402
// Elem #2
@@ -409,7 +409,7 @@ private final Object _vanillaDeserialize(JsonParser p,
409409
try {
410410
prop.deserializeAndSet(p, ctxt, bean);
411411
} catch (Exception e) {
412-
wrapAndThrow(e, bean, p.currentName(), ctxt);
412+
throw wrapAndThrow(e, bean, p.currentName(), ctxt);
413413
}
414414
ix = p.nextFieldName(_fieldMatcher);
415415
}
@@ -522,7 +522,7 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t
522522
try {
523523
_fieldsByIndex[ix].deserializeAndSet(p, ctxt, bean);
524524
} catch (Exception e) {
525-
wrapAndThrow(e, bean, p.currentName(), ctxt);
525+
throw wrapAndThrow(e, bean, p.currentName(), ctxt);
526526
}
527527
continue;
528528
}
@@ -630,7 +630,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
630630
try {
631631
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
632632
} catch (Exception e) {
633-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
633+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
634634
}
635635
continue;
636636
}
@@ -687,9 +687,7 @@ protected final Object _deserializeWithErrorWrapping(JsonParser p,
687687
try {
688688
return prop.deserialize(p, ctxt);
689689
} catch (Exception e) {
690-
wrapAndThrow(e, _beanType.getRawClass(), prop.getName(), ctxt);
691-
// never gets here, unless caller declines to throw an exception
692-
return null;
690+
throw wrapAndThrow(e, _beanType.getRawClass(), prop.getName(), ctxt);
693691
}
694692
}
695693

@@ -800,7 +798,7 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
800798
try {
801799
prop.deserializeAndSet(p, ctxt, bean);
802800
} catch (Exception e) {
803-
wrapAndThrow(e, bean, prop.getName(), ctxt);
801+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
804802
}
805803
continue;
806804
}
@@ -834,7 +832,7 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
834832
try {
835833
_anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
836834
} catch (Exception e) {
837-
wrapAndThrow(e, bean, propName, ctxt);
835+
throw wrapAndThrow(e, bean, propName, ctxt);
838836
}
839837
}
840838
tokens.writeEndObject();
@@ -865,7 +863,7 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
865863
try {
866864
prop.deserializeAndSet(p, ctxt, bean);
867865
} catch (Exception e) {
868-
wrapAndThrow(e, bean, prop.getName(), ctxt);
866+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
869867
}
870868
continue;
871869
}
@@ -897,9 +895,8 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
897895
try {
898896
_anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
899897
} catch (Exception e) {
900-
wrapAndThrow(e, bean, propName, ctxt);
898+
throw wrapAndThrow(e, bean, propName, ctxt);
901899
}
902-
continue;
903900
}
904901
}
905902
tokens.writeEndObject();
@@ -991,9 +988,8 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
991988
buffer.bufferAnyProperty(_anySetter, propName,
992989
_anySetter.deserialize(b2.asParserOnFirstToken(), ctxt));
993990
} catch (Exception e) {
994-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
991+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
995992
}
996-
continue;
997993
}
998994
}
999995

@@ -1056,7 +1052,7 @@ protected Object deserializeWithExternalTypeId(JsonParser p, DeserializationCont
10561052
try {
10571053
prop.deserializeAndSet(p, ctxt, bean);
10581054
} catch (Exception e) {
1059-
wrapAndThrow(e, bean, prop.getName(), ctxt);
1055+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
10601056
}
10611057
continue;
10621058
}
@@ -1082,7 +1078,7 @@ protected Object deserializeWithExternalTypeId(JsonParser p, DeserializationCont
10821078
try {
10831079
_anySetter.deserializeAndSet(p, ctxt, bean, propName);
10841080
} catch (Exception e) {
1085-
wrapAndThrow(e, bean, propName, ctxt);
1081+
throw wrapAndThrow(e, bean, propName, ctxt);
10861082
}
10871083
continue;
10881084
}
@@ -1123,8 +1119,7 @@ protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, D
11231119
try {
11241120
bean = creator.build(ctxt, buffer);
11251121
} catch (Exception e) {
1126-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
1127-
continue; // never gets here
1122+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
11281123
}
11291124
// if so, need to copy all remaining tokens into buffer
11301125
while (t == JsonToken.FIELD_NAME) {

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ protected void handleUnknownVanilla(JsonParser p, DeserializationContext ctxt,
14951495
// should we consider return type of any setter?
14961496
_anySetter.deserializeAndSet(p, ctxt, bean, propName);
14971497
} catch (Exception e) {
1498-
wrapAndThrow(e, bean, propName, ctxt);
1498+
throw wrapAndThrow(e, bean, propName, ctxt);
14991499
}
15001500
} else {
15011501
// Unknown: let's call handler method
@@ -1636,8 +1636,14 @@ protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationCont
16361636
* <li>"Plain" IOExceptions (ones that are not of type
16371637
* {@link JsonMappingException} are to be passed as is
16381638
*</ul>
1639+
* The method always throws but declares its return type as
1640+
* {@link IOException} in order to allow callers to invoke method as
1641+
* {@code throw wrapAndThrow(...);} thereby ensuring complete code
1642+
* coverage is possible. This also ensures that all call paths within
1643+
* this method throw an exception; otherwise they would be required
1644+
* to return.
16391645
*/
1640-
public void wrapAndThrow(Throwable t, Object bean, String fieldName, DeserializationContext ctxt)
1646+
public IOException wrapAndThrow(Throwable t, Object bean, String fieldName, DeserializationContext ctxt)
16411647
throws IOException
16421648
{
16431649
// Need to add reference information

src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private final Object _vanillaDeserialize(JsonParser p, DeserializationContext ct
294294
try {
295295
builder = prop.deserializeSetAndReturn(p, ctxt, builder);
296296
} catch (Exception e) {
297-
wrapAndThrow(e, builder, prop.getName(), ctxt);
297+
throw wrapAndThrow(e, builder, prop.getName(), ctxt);
298298
}
299299
continue;
300300
}
@@ -343,7 +343,7 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt)
343343
try {
344344
bean = _fieldsByIndex[ix].deserializeSetAndReturn(p, ctxt, bean);
345345
} catch (Exception e) {
346-
wrapAndThrow(e, bean, p.currentName(), ctxt);
346+
throw wrapAndThrow(e, bean, p.currentName(), ctxt);
347347
}
348348
continue;
349349
}
@@ -399,8 +399,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p,
399399
try {
400400
builder = creator.build(ctxt, buffer);
401401
} catch (Exception e) {
402-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
403-
continue; // never gets here
402+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
404403
}
405404
// polymorphic?
406405
if (builder.getClass() != _beanType.getRawClass()) {
@@ -496,7 +495,7 @@ protected final Object _deserialize(JsonParser p,
496495
try {
497496
builder = prop.deserializeSetAndReturn(p, ctxt, builder);
498497
} catch (Exception e) {
499-
wrapAndThrow(e, builder, prop.getName(), ctxt);
498+
throw wrapAndThrow(e, builder, prop.getName(), ctxt);
500499
}
501500
continue;
502501
}
@@ -532,7 +531,7 @@ protected final Object deserializeWithView(JsonParser p, DeserializationContext
532531
try {
533532
bean = prop.deserializeSetAndReturn(p, ctxt, bean);
534533
} catch (Exception e) {
535-
wrapAndThrow(e, bean, prop.getName(), ctxt);
534+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
536535
}
537536
continue;
538537
}
@@ -589,7 +588,7 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
589588
try {
590589
bean = prop.deserializeSetAndReturn(p, ctxt, bean);
591590
} catch (Exception e) {
592-
wrapAndThrow(e, bean, prop.getName(), ctxt);
591+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
593592
}
594593
continue;
595594
}
@@ -614,7 +613,7 @@ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext c
614613
try {
615614
_anySetter.deserializeAndSet(p, ctxt, bean, propName);
616615
} catch (Exception e) {
617-
wrapAndThrow(e, bean, propName, ctxt);
616+
throw wrapAndThrow(e, bean, propName, ctxt);
618617
}
619618
continue;
620619
}
@@ -692,8 +691,7 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p,
692691
try {
693692
builder = creator.build(ctxt, buffer);
694693
} catch (Exception e) {
695-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
696-
continue; // never gets here
694+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
697695
}
698696
if (builder.getClass() != _beanType.getRawClass()) {
699697
return handlePolymorphic(p, ctxt, builder, tokens);
@@ -773,7 +771,7 @@ protected Object deserializeWithExternalTypeId(JsonParser p,
773771
try {
774772
bean = prop.deserializeSetAndReturn(p, ctxt, bean);
775773
} catch (Exception e) {
776-
wrapAndThrow(e, bean, prop.getName(), ctxt);
774+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
777775
}
778776
continue;
779777
}
@@ -798,7 +796,7 @@ protected Object deserializeWithExternalTypeId(JsonParser p,
798796
try {
799797
_anySetter.deserializeAndSet(p, ctxt, bean, propName);
800798
} catch (Exception e) {
801-
wrapAndThrow(e, bean, propName, ctxt);
799+
throw wrapAndThrow(e, bean, propName, ctxt);
802800
}
803801
continue;
804802
} else {

src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayBuilderDeserializer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
145145
try {
146146
builder = prop.deserializeSetAndReturn(p, ctxt, builder);
147147
} catch (Exception e) {
148-
wrapAndThrow(e, builder, prop.getName(), ctxt);
148+
throw wrapAndThrow(e, builder, prop.getName(), ctxt);
149149
}
150150
} else { // just skip?
151151
p.skipChildren();
@@ -223,7 +223,7 @@ protected Object _deserializeNonVanilla(JsonParser p, DeserializationContext ctx
223223
try {
224224
prop.deserializeSetAndReturn(p, ctxt, builder);
225225
} catch (Exception e) {
226-
wrapAndThrow(e, builder, prop.getName(), ctxt);
226+
throw wrapAndThrow(e, builder, prop.getName(), ctxt);
227227
}
228228
continue;
229229
}
@@ -282,7 +282,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p,
282282
try {
283283
builder = prop.deserializeSetAndReturn(p, ctxt, builder);
284284
} catch (Exception e) {
285-
wrapAndThrow(e, builder, prop.getName(), ctxt);
285+
throw wrapAndThrow(e, builder, prop.getName(), ctxt);
286286
}
287287
continue;
288288
}
@@ -295,8 +295,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p,
295295
try {
296296
builder = creator.build(ctxt, buffer);
297297
} catch (Exception e) {
298-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
299-
continue; // never gets here
298+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
300299
}
301300
// polymorphic?
302301
if (builder.getClass() != _beanType.getRawClass()) {

src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayDeserializer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
212212
try {
213213
prop.deserializeAndSet(p, ctxt, bean);
214214
} catch (Exception e) {
215-
wrapAndThrow(e, bean, prop.getName(), ctxt);
215+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
216216
}
217217
} else { // just skip?
218218
p.skipChildren();
@@ -268,7 +268,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object bean
268268
try {
269269
prop.deserializeAndSet(p, ctxt, bean);
270270
} catch (Exception e) {
271-
wrapAndThrow(e, bean, prop.getName(), ctxt);
271+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
272272
}
273273
} else { // just skip?
274274
p.skipChildren();
@@ -339,7 +339,7 @@ protected Object _deserializeNonVanilla(JsonParser p, DeserializationContext ctx
339339
try {
340340
prop.deserializeAndSet(p, ctxt, bean);
341341
} catch (Exception e) {
342-
wrapAndThrow(e, bean, prop.getName(), ctxt);
342+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
343343
}
344344
continue;
345345
}
@@ -398,7 +398,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p, final
398398
try {
399399
prop.deserializeAndSet(p, ctxt, bean);
400400
} catch (Exception e) {
401-
wrapAndThrow(e, bean, prop.getName(), ctxt);
401+
throw wrapAndThrow(e, bean, prop.getName(), ctxt);
402402
}
403403
continue;
404404
}
@@ -411,8 +411,7 @@ protected final Object _deserializeUsingPropertyBased(final JsonParser p, final
411411
try {
412412
bean = creator.build(ctxt, buffer);
413413
} catch (Exception e) {
414-
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
415-
continue; // never gets here
414+
throw wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
416415
}
417416
// [databind#631]: Assign current value, to be accessible by custom serializers
418417
p.setCurrentValue(bean);

0 commit comments

Comments
 (0)