Skip to content

Commit

Permalink
Fix #58
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 5, 2019
1 parent c63dac1 commit 32b3292
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
endpointType = TypeFactory.unknownType();
}
deser = ctxt.findContextualValueDeserializer(endpointType, property);
} else if (deser instanceof ContextualDeserializer) {
// 04-Sep-2019, tatu: If we already have a deserialize, should contextualize, right?
deser = ((ContextualDeserializer) deser).createContextual(ctxt, property);
}
if ((deser != _endpointDeserializer) || (fieldNames != _fieldNames)) {
return new RangeDeserializer(_rangeType, deser, _defaultBoundType, fieldNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,16 @@
package com.fasterxml.jackson.datatype.guava.deser.util;

import com.google.common.collect.BoundType;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
* A factory for creating Guava {@link Range}s that is compatible with Guava 10 and later.
*
* If Guava 10, 11, 12, or 13 is being used, the factory methods in the com.google.common.collect.Ranges class (a beta
* class that was removed in Guava 14) are used to reflectively instantiate Ranges. If Guava 14 or later is being used,
* the factory methods in the Range class itself (added in Guava 14) are used to instantiate Ranges.
* A factory for creating Guava {@link Range}s that is compatible with Guava 14 and later.
*<p>
* NOTE: up until Jackson 2.9.x, supported versions from Guava 10 and higher; support for
* older versions dropped in Jackson 2.10.
*/
public class RangeFactory
{
private static final String LEGACY_RANGES_CLASS_NAME = "com.google.common.collect.Ranges";

private static final String LEGACY_RANGE_METHOD_NAME = "range";
private static final String LEGACY_DOWN_TO_METHOD_NAME = "downTo";
private static final String LEGACY_UP_TO_METHOD_NAME = "upTo";
private static final String LEGACY_ALL_METHOD_NAME = "all";

private static Method legacyRangeMethod;
private static Method legacyDownToMethod;
private static Method legacyUpToMethod;
private static Method legacyAllMethod;

static
{
initLegacyRangeFactoryMethods();
}

private static void initLegacyRangeFactoryMethods()
{
try {
Class<?> rangesClass = Class.forName(LEGACY_RANGES_CLASS_NAME);
legacyRangeMethod = findMethod(rangesClass, LEGACY_RANGE_METHOD_NAME, Comparable.class, BoundType.class,
Comparable.class, BoundType.class);
legacyDownToMethod = findMethod(rangesClass, LEGACY_DOWN_TO_METHOD_NAME, Comparable.class, BoundType.class);
legacyUpToMethod = findMethod(rangesClass, LEGACY_UP_TO_METHOD_NAME, Comparable.class, BoundType.class);
legacyAllMethod = findMethod(rangesClass, LEGACY_ALL_METHOD_NAME);
} catch (ClassNotFoundException e) {
// ignore
}
}

// returns null if the method is not found
private static Method findMethod(Class<?> clazz, String methodName, Class<?> ... paramTypes)
{
try {
return clazz.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException e) {
return null;
}
}

public static <C extends Comparable<?>> Range<C> open(C lowerEndpoint, C upperEndpoint)
{
return range(lowerEndpoint, BoundType.OPEN, upperEndpoint, BoundType.OPEN);
Expand All @@ -81,12 +35,7 @@ public static <C extends Comparable<?>> Range<C> closed(C lowerEndpoint, C upper
public static <C extends Comparable<?>> Range<C> range(final C lowerEndpoint, final BoundType lowerBoundType,
final C upperEndpoint, final BoundType upperBoundType)
{
return createRange(new Callable<Range<C>>() {
@Override
public Range<C> call() throws Exception {
return Range.range(lowerEndpoint, lowerBoundType, upperEndpoint, upperBoundType);
}
}, legacyRangeMethod, lowerEndpoint, lowerBoundType, upperEndpoint, upperBoundType);
return Range.range(lowerEndpoint, lowerBoundType, upperEndpoint, upperBoundType);
}

public static <C extends Comparable<?>> Range<C> greaterThan(C lowerEndpoint)
Expand All @@ -101,12 +50,7 @@ public static <C extends Comparable<?>> Range<C> atLeast(C lowerEndpoint)

public static <C extends Comparable<?>> Range<C> downTo(final C lowerEndpoint, final BoundType lowerBoundType)
{
return createRange(new Callable<Range<C>>() {
@Override
public Range<C> call() throws Exception {
return Range.downTo(lowerEndpoint, lowerBoundType);
}
}, legacyDownToMethod, lowerEndpoint, lowerBoundType);
return Range.downTo(lowerEndpoint, lowerBoundType);
}

public static <C extends Comparable<?>> Range<C> lessThan(C upperEndpoint)
Expand All @@ -121,59 +65,17 @@ public static <C extends Comparable<?>> Range<C> atMost(C upperEndpoint)

public static <C extends Comparable<?>> Range<C> upTo(final C upperEndpoint, final BoundType upperBoundType)
{
return createRange(new Callable<Range<C>>() {
@Override
public Range<C> call() throws Exception {
return Range.upTo(upperEndpoint, upperBoundType);
}
}, legacyUpToMethod, upperEndpoint, upperBoundType);
return Range.upTo(upperEndpoint, upperBoundType);
}

public static <C extends Comparable<?>> Range<C> all()
{
return createRange(new Callable<Range<C>>() {
@Override
public Range<C> call() throws Exception {
return Range.all();
}
}, legacyAllMethod);
return Range.all();
}

public static <C extends Comparable<?>> Range<C> singleton(final C value)
{
return createRange(new Callable<Range<C>>() {
@Override
public Range<C> call() throws Exception {
return Range.singleton(value);
}
}, legacyRangeMethod, value, BoundType.CLOSED, value, BoundType.CLOSED);
}

private static <C extends Comparable<?>> Range<C> createRange(Callable<Range<C>> rangeCallable, Method legacyRangeFactoryMethod, Object ... params)
{
try {
return rangeCallable.call();
} catch (NoSuchMethodError noSuchMethodError) {
if (legacyRangeFactoryMethod != null) {
return invokeLegacyRangeFactoryMethod(legacyRangeFactoryMethod, params);
} else {
throw noSuchMethodError;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
private static <C extends Comparable<?>> Range<C> invokeLegacyRangeFactoryMethod(Method method, Object... params)
{
try {
//noinspection unchecked
return (Range<C>) method.invoke(null, params);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke legacy Range factory method [" + method.getName() +
"] with params " + Lists.newArrayList(params) + ".", e);
}
return Range.singleton(value);
}

// prevent instantiation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.datatype.guava.failing;
package com.fasterxml.jackson.datatype.guava;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
Expand All @@ -8,7 +8,6 @@
import com.google.common.collect.Range;

import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.guava.ModuleTestBase;

// [datatypes-collections#56]: support naming strategy
public class Range56Test extends ModuleTestBase
Expand Down
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ Jon Freedman (jonfreedman@github)
* Reported #53: (guava) `GuavaImmutableCollectionDeserializer` cannot deserialize
an empty `Optional` from null
(2.10.0)

Philip Leonard (philleonard@github)
* Reported#56, contributed fix: Range property name (de)serialisation doesn't
respect property naming strategy
(2.10.0)
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.10.0 (not yet released)

#56: Range property name (de)serialisation doesn't respect property naming strategy
(reported, fix contributed by Philip L)
#59: Drop support for Guava v10 - v13 to simplify `RangeFactory`

2.10.0.pr2 (31-Aug-2019)

#53: (guava) `GuavaImmutableCollectionDeserializer` cannot deserialize
Expand Down

0 comments on commit 32b3292

Please sign in to comment.