diff --git a/release-notes/VERSION b/release-notes/VERSION index 003f2d8bb7..fcdb5d2c7b 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -11,6 +11,8 @@ Fixes: * [Issue#157]: NPE when registering module twice * [Issue#162]: JsonNodeFactory: work around an old bug with BigDecimal and zero (submitted by fge@github) +* [Issue#166]: Incorrect optimization for `ObjectMapper.convertValue(Class)` + (reported by eric t) ------------------------------------------------------------------------ === History: === diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index acbd1ad22a..3e850f8558 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -2442,11 +2442,6 @@ public T convertValue(Object fromValue, Class toValueType) { // sanity check for null first: if (fromValue == null) return null; - // also, as per [Issue-11], consider case for simple cast - // ... one caveat; while everything is Object.class, let's not take shortcut - if (toValueType != Object.class && toValueType.isAssignableFrom(fromValue.getClass())) { - return (T) fromValue; - } return (T) _convert(fromValue, _typeFactory.constructType(toValueType)); } @@ -2463,17 +2458,6 @@ public T convertValue(Object fromValue, JavaType toValueType) { // sanity check for null first: if (fromValue == null) return null; - // also, as per [Issue-11], consider case for simple cast - /* But with caveats: one is that while everything is Object.class, we don't - * want to "optimize" that out; and the other is that we also do not want - * to lose conversions of generic types. - */ - Class targetType = toValueType.getRawClass(); - if (targetType != Object.class - && !toValueType.hasGenericTypes() - && targetType.isAssignableFrom(fromValue.getClass())) { - return (T) fromValue; - } return (T) _convert(fromValue, toValueType); } @@ -2488,6 +2472,18 @@ public T convertValue(Object fromValue, JavaType toValueType) protected Object _convert(Object fromValue, JavaType toValueType) throws IllegalArgumentException { + // also, as per [Issue-11], consider case for simple cast + /* But with caveats: one is that while everything is Object.class, we don't + * want to "optimize" that out; and the other is that we also do not want + * to lose conversions of generic types. + */ + Class targetType = toValueType.getRawClass(); + if (targetType != Object.class + && !toValueType.hasGenericTypes() + && targetType.isAssignableFrom(fromValue.getClass())) { + return fromValue; + } + /* Then use TokenBuffer, which is a JsonGenerator: * (see [JACKSON-175]) */