diff --git a/src/main/java/com/fasterxml/jackson/databind/DatabindException.java b/src/main/java/com/fasterxml/jackson/databind/DatabindException.java index 5c58018b48..460c3ce86a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/DatabindException.java +++ b/src/main/java/com/fasterxml/jackson/databind/DatabindException.java @@ -33,4 +33,16 @@ protected DatabindException(String msg, JsonLocation loc) { protected DatabindException(String msg, Throwable rootCause) { this(msg, null, rootCause); } + + /** + * Method called to prepend a reference information in front of + * current path + */ + public abstract void prependPath(Object referrer, String fieldName); + + /** + * Method called to prepend a reference information in front of + * current path + */ + public abstract void prependPath(Object referrer, int index); } diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java index 1ab292e2ed..508e4cd84e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java +++ b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java @@ -32,9 +32,9 @@ public class JsonMappingException final static int MAX_REFS_TO_LIST = 1000; /* - /********************************************************** + /********************************************************************** /* Helper classes - /********************************************************** + /********************************************************************** */ /** @@ -173,9 +173,9 @@ Object writeReplace() { } /* - /********************************************************** + /********************************************************************** /* State/configuration - /********************************************************** + /********************************************************************** */ /** @@ -193,11 +193,11 @@ Object writeReplace() { * @since 2.7 */ protected transient Closeable _processor; - + /* - /********************************************************** + /********************************************************************** /* Life-cycle - /********************************************************** + /********************************************************************** */ /** @@ -326,7 +326,7 @@ public static JsonMappingException from(SerializerProvider ctxt, String msg, Thr *

* NOTE: since 2.9 should usually NOT be used on input-side (deserialization) * exceptions; instead use method(s) of InputMismatchException - * + * * @since 2.1 */ public static JsonMappingException fromUnexpectedIOE(IOException src) { @@ -394,9 +394,9 @@ public static JsonMappingException wrapWithPath(Throwable src, Reference ref) } /* - /********************************************************** + /********************************************************************** /* Accessors/mutators - /********************************************************** + /********************************************************************** */ /** @@ -430,19 +430,18 @@ public StringBuilder getPathReference(StringBuilder sb) * Method called to prepend a reference information in front of * current path */ - public void prependPath(Object referrer, String fieldName) - { - Reference ref = new Reference(referrer, fieldName); - prependPath(ref); + @Override + public void prependPath(Object referrer, String fieldName) { + prependPath(new Reference(referrer, fieldName)); } + /** * Method called to prepend a reference information in front of * current path */ - public void prependPath(Object referrer, int index) - { - Reference ref = new Reference(referrer, index); - prependPath(ref); + @Override + public void prependPath(Object referrer, int index) { + prependPath(new Reference(referrer, index)); } public void prependPath(Reference r) @@ -458,14 +457,14 @@ public void prependPath(Reference r) _path.addFirst(r); } } - + /* - /********************************************************** + /********************************************************************** /* Overridden methods - /********************************************************** + /********************************************************************** */ - @Override // since 2.7.5 + @Override // since 2.8 @JsonIgnore // as per [databind#1368] public Object getProcessor() { return _processor; } @@ -509,9 +508,9 @@ public String toString() } /* - /********************************************************** + /********************************************************************** /* Internal methods - /********************************************************** + /********************************************************************** */ protected void _appendPathDesc(StringBuilder sb) diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java index 95fa25e622..2f70a2ebe9 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java @@ -4,6 +4,7 @@ import java.util.BitSet; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DatabindException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonDeserializer; @@ -209,7 +210,7 @@ protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingExcep // Fourth: default value JsonDeserializer deser = prop.getValueDeserializer(); return deser.getNullValue(_context); - } catch (JsonMappingException e) { + } catch (DatabindException e) { // [databind#2101]: Include property name, if we have it AnnotatedMember member = prop.getMember(); if (member != null) { diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java index 65d5aa0182..b72f7ab5af 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java @@ -138,11 +138,11 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t // Sanity check: did we find "message"? if (throwable == null) { /* 15-Oct-2010, tatu: Can't assume missing message is an error, since it may be - * suppressed during serialization, as per [JACKSON-388]. + * suppressed during serialization. * * Should probably allow use of default constructor, too... */ - //throw new JsonMappingException("No 'message' property found: could not deserialize "+_beanType); + //throw new XxxException("No 'message' property found: could not deserialize "+_beanType); if (hasStringCreator) { throwable = _valueInstantiator.createFromString(ctxt, null); } else { diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java index 22f2c3460a..050840ef6c 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java @@ -790,7 +790,8 @@ public Object findSerializationContentConverter(AnnotatedMember a) { @Override public JavaType refineSerializationType(final MapperConfig config, - final Annotated a, final JavaType baseType) throws JsonMappingException + final Annotated a, final JavaType baseType) + throws JsonMappingException { JavaType type = baseType; final TypeFactory tf = config.getTypeFactory(); @@ -818,15 +819,14 @@ public JavaType refineSerializationType(final MapperConfig config, // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements type = type.withStaticTyping(); } else { - throw new JsonMappingException(null, + throw _databindException( String.format("Cannot refine serialization type %s into %s; types not related", type, serClass.getName())); } } catch (IllegalArgumentException iae) { - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Failed to widen type %s with annotation (value %s), from '%s': %s", - type, serClass.getName(), a.getName(), iae.getMessage()), - iae); + type, serClass.getName(), a.getName(), iae.getMessage())); } } } @@ -853,15 +853,14 @@ public JavaType refineSerializationType(final MapperConfig config, // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements keyType = keyType.withStaticTyping(); } else { - throw new JsonMappingException(null, + throw _databindException( String.format("Cannot refine serialization key type %s into %s; types not related", keyType, keyClass.getName())); } } catch (IllegalArgumentException iae) { - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s", - type, keyClass.getName(), a.getName(), iae.getMessage()), - iae); + type, keyClass.getName(), a.getName(), iae.getMessage())); } } type = ((MapLikeType) type).withKeyType(keyType); @@ -889,15 +888,14 @@ public JavaType refineSerializationType(final MapperConfig config, // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements contentType = contentType.withStaticTyping(); } else { - throw new JsonMappingException(null, + throw _databindException( String.format("Cannot refine serialization content type %s into %s; types not related", contentType, contentClass.getName())); } } catch (IllegalArgumentException iae) { // shouldn't really happen - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s", - type, contentClass.getName(), a.getName(), iae.getMessage()), - iae); + type, contentClass.getName(), a.getName(), iae.getMessage())); } } type = type.withContentType(contentType); @@ -1200,10 +1198,9 @@ public JavaType refineDeserializationType(final MapperConfig config, try { type = tf.constructSpecializedType(type, valueClass); } catch (IllegalArgumentException iae) { - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s", - type, valueClass.getName(), a.getName(), iae.getMessage()), - iae); + type, valueClass.getName(), a.getName(), iae.getMessage())); } } // Then further processing for container types @@ -1218,10 +1215,9 @@ public JavaType refineDeserializationType(final MapperConfig config, keyType = tf.constructSpecializedType(keyType, keyClass); type = ((MapLikeType) type).withKeyType(keyType); } catch (IllegalArgumentException iae) { - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s", - type, keyClass.getName(), a.getName(), iae.getMessage()), - iae); + type, keyClass.getName(), a.getName(), iae.getMessage())); } } } @@ -1235,10 +1231,9 @@ public JavaType refineDeserializationType(final MapperConfig config, contentType = tf.constructSpecializedType(contentType, contentClass); type = type.withContentType(contentType); } catch (IllegalArgumentException iae) { - throw new JsonMappingException(null, + throw _databindException(iae, String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s", - type, contentClass.getName(), a.getName(), iae.getMessage()), - iae); + type, contentClass.getName(), a.getName(), iae.getMessage())); } } } @@ -1565,4 +1560,14 @@ private boolean _primitiveAndWrapper(JavaType baseType, Class refinement) } return false; } + + // @since 2.12 + private JsonMappingException _databindException(String msg) { + return new JsonMappingException(null, msg); + } + + // @since 2.12 + private JsonMappingException _databindException(Throwable t, String msg) { + return new JsonMappingException(null, msg, t); + } } diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/BeanAsArraySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/BeanAsArraySerializer.java index edc6ad5747..85be07695e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/BeanAsArraySerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/BeanAsArraySerializer.java @@ -223,9 +223,9 @@ protected final void serializeAsArray(Object bean, JsonGenerator gen, Serializer String name = (i == props.length) ? "[anySetter]" : props[i].getName(); wrapAndThrow(provider, e, bean, name); } catch (StackOverflowError e) { - JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e); + DatabindException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e); String name = (i == props.length) ? "[anySetter]" : props[i].getName(); - mapE.prependPath(new JsonMappingException.Reference(bean, name)); + mapE.prependPath(bean, name); throw mapE; } } diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java index 9a891caa55..a4fb388b6b 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java @@ -782,10 +782,10 @@ protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvide // 10-Dec-2015, tatu: and due to above, avoid "from" method, call ctor directly: //JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e); - JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e); + DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e); - String name = (i == props.length) ? "[anySetter]" : props[i].getName(); - mapE.prependPath(new JsonMappingException.Reference(bean, name)); + String name = (i == props.length) ? "[anySetter]" : props[i].getName(); + mapE.prependPath(bean, name); throw mapE; } } @@ -830,9 +830,9 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator gen, } catch (StackOverflowError e) { // Minimize call depth since we are close to fail: //JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e); - JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e); + DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e); String name = (i == props.length) ? "[anySetter]" : props[i].getName(); - mapE.prependPath(new JsonMappingException.Reference(bean, name)); + mapE.prependPath(bean, name); throw mapE; } } diff --git a/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java b/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java index 7f64859855..2e951e40d5 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java @@ -929,10 +929,10 @@ public void writeObject(Object value) throws IOException return; } if (_objectCodec == null) { - /* 28-May-2014, tatu: Tricky choice here; if no codec, should we - * err out, or just embed? For now, do latter. - */ -// throw new JsonMappingException("No ObjectCodec configured for TokenBuffer, writeObject() called"); + // 28-May-2014, tatu: Tricky choice here; if no codec, should we + // err out, or just embed? For now, do latter. + +// throw new XxxException("No ObjectCodec configured for TokenBuffer, writeObject() called"); _appendValue(JsonToken.VALUE_EMBEDDED_OBJECT, value); } else { _objectCodec.writeValue(this, value);