Skip to content

Commit ef34d08

Browse files
committed
Bit more work on #2828, trying to reduce coupling to JsonMappingException even in 2.x
1 parent 516c91f commit ef34d08

File tree

8 files changed

+77
-60
lines changed

8 files changed

+77
-60
lines changed

Diff for: src/main/java/com/fasterxml/jackson/databind/DatabindException.java

+12
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,16 @@ protected DatabindException(String msg, JsonLocation loc) {
3333
protected DatabindException(String msg, Throwable rootCause) {
3434
this(msg, null, rootCause);
3535
}
36+
37+
/**
38+
* Method called to prepend a reference information in front of
39+
* current path
40+
*/
41+
public abstract void prependPath(Object referrer, String fieldName);
42+
43+
/**
44+
* Method called to prepend a reference information in front of
45+
* current path
46+
*/
47+
public abstract void prependPath(Object referrer, int index);
3648
}

Diff for: src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java

+23-24
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class JsonMappingException
3232
final static int MAX_REFS_TO_LIST = 1000;
3333

3434
/*
35-
/**********************************************************
35+
/**********************************************************************
3636
/* Helper classes
37-
/**********************************************************
37+
/**********************************************************************
3838
*/
3939

4040
/**
@@ -173,9 +173,9 @@ Object writeReplace() {
173173
}
174174

175175
/*
176-
/**********************************************************
176+
/**********************************************************************
177177
/* State/configuration
178-
/**********************************************************
178+
/**********************************************************************
179179
*/
180180

181181
/**
@@ -193,11 +193,11 @@ Object writeReplace() {
193193
* @since 2.7
194194
*/
195195
protected transient Closeable _processor;
196-
196+
197197
/*
198-
/**********************************************************
198+
/**********************************************************************
199199
/* Life-cycle
200-
/**********************************************************
200+
/**********************************************************************
201201
*/
202202

203203
/**
@@ -326,7 +326,7 @@ public static JsonMappingException from(SerializerProvider ctxt, String msg, Thr
326326
*<p>
327327
* NOTE: since 2.9 should usually NOT be used on input-side (deserialization)
328328
* exceptions; instead use method(s) of <code>InputMismatchException</code>
329-
*
329+
*
330330
* @since 2.1
331331
*/
332332
public static JsonMappingException fromUnexpectedIOE(IOException src) {
@@ -394,9 +394,9 @@ public static JsonMappingException wrapWithPath(Throwable src, Reference ref)
394394
}
395395

396396
/*
397-
/**********************************************************
397+
/**********************************************************************
398398
/* Accessors/mutators
399-
/**********************************************************
399+
/**********************************************************************
400400
*/
401401

402402
/**
@@ -430,19 +430,18 @@ public StringBuilder getPathReference(StringBuilder sb)
430430
* Method called to prepend a reference information in front of
431431
* current path
432432
*/
433-
public void prependPath(Object referrer, String fieldName)
434-
{
435-
Reference ref = new Reference(referrer, fieldName);
436-
prependPath(ref);
433+
@Override
434+
public void prependPath(Object referrer, String fieldName) {
435+
prependPath(new Reference(referrer, fieldName));
437436
}
437+
438438
/**
439439
* Method called to prepend a reference information in front of
440440
* current path
441441
*/
442-
public void prependPath(Object referrer, int index)
443-
{
444-
Reference ref = new Reference(referrer, index);
445-
prependPath(ref);
442+
@Override
443+
public void prependPath(Object referrer, int index) {
444+
prependPath(new Reference(referrer, index));
446445
}
447446

448447
public void prependPath(Reference r)
@@ -458,14 +457,14 @@ public void prependPath(Reference r)
458457
_path.addFirst(r);
459458
}
460459
}
461-
460+
462461
/*
463-
/**********************************************************
462+
/**********************************************************************
464463
/* Overridden methods
465-
/**********************************************************
464+
/**********************************************************************
466465
*/
467466

468-
@Override // since 2.7.5
467+
@Override // since 2.8
469468
@JsonIgnore // as per [databind#1368]
470469
public Object getProcessor() { return _processor; }
471470

@@ -509,9 +508,9 @@ public String toString()
509508
}
510509

511510
/*
512-
/**********************************************************
511+
/**********************************************************************
513512
/* Internal methods
514-
/**********************************************************
513+
/**********************************************************************
515514
*/
516515

517516
protected void _appendPathDesc(StringBuilder sb)

Diff for: src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.BitSet;
55

66
import com.fasterxml.jackson.core.JsonParser;
7+
import com.fasterxml.jackson.databind.DatabindException;
78
import com.fasterxml.jackson.databind.DeserializationContext;
89
import com.fasterxml.jackson.databind.DeserializationFeature;
910
import com.fasterxml.jackson.databind.JsonDeserializer;
@@ -209,7 +210,7 @@ protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingExcep
209210
// Fourth: default value
210211
JsonDeserializer<Object> deser = prop.getValueDeserializer();
211212
return deser.getNullValue(_context);
212-
} catch (JsonMappingException e) {
213+
} catch (DatabindException e) {
213214
// [databind#2101]: Include property name, if we have it
214215
AnnotatedMember member = prop.getMember();
215216
if (member != null) {

Diff for: src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t
138138
// Sanity check: did we find "message"?
139139
if (throwable == null) {
140140
/* 15-Oct-2010, tatu: Can't assume missing message is an error, since it may be
141-
* suppressed during serialization, as per [JACKSON-388].
141+
* suppressed during serialization.
142142
*
143143
* Should probably allow use of default constructor, too...
144144
*/
145-
//throw new JsonMappingException("No 'message' property found: could not deserialize "+_beanType);
145+
//throw new XxxException("No 'message' property found: could not deserialize "+_beanType);
146146
if (hasStringCreator) {
147147
throwable = _valueInstantiator.createFromString(ctxt, null);
148148
} else {

Diff for: src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ public Object findSerializationContentConverter(AnnotatedMember a) {
790790

791791
@Override
792792
public JavaType refineSerializationType(final MapperConfig<?> config,
793-
final Annotated a, final JavaType baseType) throws JsonMappingException
793+
final Annotated a, final JavaType baseType)
794+
throws JsonMappingException
794795
{
795796
JavaType type = baseType;
796797
final TypeFactory tf = config.getTypeFactory();
@@ -818,15 +819,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
818819
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
819820
type = type.withStaticTyping();
820821
} else {
821-
throw new JsonMappingException(null,
822+
throw _databindException(
822823
String.format("Cannot refine serialization type %s into %s; types not related",
823824
type, serClass.getName()));
824825
}
825826
} catch (IllegalArgumentException iae) {
826-
throw new JsonMappingException(null,
827+
throw _databindException(iae,
827828
String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
828-
type, serClass.getName(), a.getName(), iae.getMessage()),
829-
iae);
829+
type, serClass.getName(), a.getName(), iae.getMessage()));
830830
}
831831
}
832832
}
@@ -853,15 +853,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
853853
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
854854
keyType = keyType.withStaticTyping();
855855
} else {
856-
throw new JsonMappingException(null,
856+
throw _databindException(
857857
String.format("Cannot refine serialization key type %s into %s; types not related",
858858
keyType, keyClass.getName()));
859859
}
860860
} catch (IllegalArgumentException iae) {
861-
throw new JsonMappingException(null,
861+
throw _databindException(iae,
862862
String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s",
863-
type, keyClass.getName(), a.getName(), iae.getMessage()),
864-
iae);
863+
type, keyClass.getName(), a.getName(), iae.getMessage()));
865864
}
866865
}
867866
type = ((MapLikeType) type).withKeyType(keyType);
@@ -889,15 +888,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
889888
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
890889
contentType = contentType.withStaticTyping();
891890
} else {
892-
throw new JsonMappingException(null,
891+
throw _databindException(
893892
String.format("Cannot refine serialization content type %s into %s; types not related",
894893
contentType, contentClass.getName()));
895894
}
896895
} catch (IllegalArgumentException iae) { // shouldn't really happen
897-
throw new JsonMappingException(null,
896+
throw _databindException(iae,
898897
String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s",
899-
type, contentClass.getName(), a.getName(), iae.getMessage()),
900-
iae);
898+
type, contentClass.getName(), a.getName(), iae.getMessage()));
901899
}
902900
}
903901
type = type.withContentType(contentType);
@@ -1200,10 +1198,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
12001198
try {
12011199
type = tf.constructSpecializedType(type, valueClass);
12021200
} catch (IllegalArgumentException iae) {
1203-
throw new JsonMappingException(null,
1201+
throw _databindException(iae,
12041202
String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s",
1205-
type, valueClass.getName(), a.getName(), iae.getMessage()),
1206-
iae);
1203+
type, valueClass.getName(), a.getName(), iae.getMessage()));
12071204
}
12081205
}
12091206
// Then further processing for container types
@@ -1218,10 +1215,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
12181215
keyType = tf.constructSpecializedType(keyType, keyClass);
12191216
type = ((MapLikeType) type).withKeyType(keyType);
12201217
} catch (IllegalArgumentException iae) {
1221-
throw new JsonMappingException(null,
1218+
throw _databindException(iae,
12221219
String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s",
1223-
type, keyClass.getName(), a.getName(), iae.getMessage()),
1224-
iae);
1220+
type, keyClass.getName(), a.getName(), iae.getMessage()));
12251221
}
12261222
}
12271223
}
@@ -1235,10 +1231,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
12351231
contentType = tf.constructSpecializedType(contentType, contentClass);
12361232
type = type.withContentType(contentType);
12371233
} catch (IllegalArgumentException iae) {
1238-
throw new JsonMappingException(null,
1234+
throw _databindException(iae,
12391235
String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s",
1240-
type, contentClass.getName(), a.getName(), iae.getMessage()),
1241-
iae);
1236+
type, contentClass.getName(), a.getName(), iae.getMessage()));
12421237
}
12431238
}
12441239
}
@@ -1565,4 +1560,14 @@ private boolean _primitiveAndWrapper(JavaType baseType, Class<?> refinement)
15651560
}
15661561
return false;
15671562
}
1563+
1564+
// @since 2.12
1565+
private JsonMappingException _databindException(String msg) {
1566+
return new JsonMappingException(null, msg);
1567+
}
1568+
1569+
// @since 2.12
1570+
private JsonMappingException _databindException(Throwable t, String msg) {
1571+
return new JsonMappingException(null, msg, t);
1572+
}
15681573
}

Diff for: src/main/java/com/fasterxml/jackson/databind/ser/impl/BeanAsArraySerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ protected final void serializeAsArray(Object bean, JsonGenerator gen, Serializer
223223
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
224224
wrapAndThrow(provider, e, bean, name);
225225
} catch (StackOverflowError e) {
226-
JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
226+
DatabindException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
227227
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
228-
mapE.prependPath(new JsonMappingException.Reference(bean, name));
228+
mapE.prependPath(bean, name);
229229
throw mapE;
230230
}
231231
}

Diff for: src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,10 @@ protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvide
782782

783783
// 10-Dec-2015, tatu: and due to above, avoid "from" method, call ctor directly:
784784
//JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
785-
JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
785+
DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
786786

787-
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
788-
mapE.prependPath(new JsonMappingException.Reference(bean, name));
787+
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
788+
mapE.prependPath(bean, name);
789789
throw mapE;
790790
}
791791
}
@@ -830,9 +830,9 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator gen,
830830
} catch (StackOverflowError e) {
831831
// Minimize call depth since we are close to fail:
832832
//JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
833-
JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
833+
DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
834834
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
835-
mapE.prependPath(new JsonMappingException.Reference(bean, name));
835+
mapE.prependPath(bean, name);
836836
throw mapE;
837837
}
838838
}

Diff for: src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,10 @@ public void writeObject(Object value) throws IOException
929929
return;
930930
}
931931
if (_objectCodec == null) {
932-
/* 28-May-2014, tatu: Tricky choice here; if no codec, should we
933-
* err out, or just embed? For now, do latter.
934-
*/
935-
// throw new JsonMappingException("No ObjectCodec configured for TokenBuffer, writeObject() called");
932+
// 28-May-2014, tatu: Tricky choice here; if no codec, should we
933+
// err out, or just embed? For now, do latter.
934+
935+
// throw new XxxException("No ObjectCodec configured for TokenBuffer, writeObject() called");
936936
_appendValue(JsonToken.VALUE_EMBEDDED_OBJECT, value);
937937
} else {
938938
_objectCodec.writeValue(this, value);

0 commit comments

Comments
 (0)