Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit d5ff04f

Browse files
committed
Add bit of baseline support for handling Tables; starting by pre-resolving type parameters
1 parent 16e96e5 commit d5ff04f

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/main/java/com/fasterxml/jackson/datatype/guava/GuavaTypeModifier.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import com.fasterxml.jackson.databind.type.TypeBindings;
77
import com.fasterxml.jackson.databind.type.TypeFactory;
88
import com.fasterxml.jackson.databind.type.TypeModifier;
9-
109
import com.google.common.base.Optional;
1110
import com.google.common.collect.FluentIterable;
1211
import com.google.common.collect.Multimap;
1312
import com.google.common.collect.Range;
13+
import com.google.common.collect.Table;
1414

1515
/**
1616
* We need somewhat hacky support for following Guava types:
@@ -42,18 +42,24 @@ public class GuavaTypeModifier extends TypeModifier
4242
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings context, TypeFactory typeFactory)
4343
{
4444
final Class<?> raw = type.getRawClass();
45+
// First: make Multimaps look more Map-like
4546
if (Multimap.class.isAssignableFrom(raw)) {
46-
JavaType keyType = type.containedType(0);
47-
JavaType contentType = type.containedType(1);
48-
49-
if (keyType == null) {
50-
keyType = TypeFactory.unknownType();
51-
}
52-
if (contentType == null) {
53-
contentType = TypeFactory.unknownType();
47+
JavaType[] types = typeFactory.findTypeParameters(type, Multimap.class);
48+
return typeFactory.constructMapLikeType(raw, _type(types, 0), _type(types, 1));
49+
}
50+
// 14-Sep-2015, tatu: Pre-resolve type parameters for Tables as well
51+
if (Table.class.isAssignableFrom(raw)) {
52+
JavaType[] types = typeFactory.findTypeParameters(type, Table.class);
53+
if (types == null || types.length != 3) {
54+
types = new JavaType[] {
55+
_type(types, 0),
56+
_type(types, 1),
57+
_type(types, 2)
58+
};
5459
}
55-
return typeFactory.constructMapLikeType(type.getRawClass(), keyType, contentType);
60+
return typeFactory.constructParametrizedType(raw, Table.class, types);
5661
}
62+
5763
/* Guava 12 changed the implementation of their {@link FluentIterable} to include a method named "isEmpty." This method
5864
* causes Jackson to treat FluentIterables as a Bean instead of an {@link Iterable}. Serialization of FluentIterables by
5965
* default result in a string like "{\"empty\":true}." This module modifies the JavaType of FluentIterable to be
@@ -108,4 +114,11 @@ public JavaType modifyType(JavaType type, Type jdkType, TypeBindings context, Ty
108114
}
109115
return type;
110116
}
117+
118+
private static JavaType _type(JavaType[] types, int index) {
119+
if (types == null || types.length <= index) {
120+
return TypeFactory.unknownType();
121+
}
122+
return types[index];
123+
}
111124
}

0 commit comments

Comments
 (0)