io.prestosql.jdbc.$internal.jackson.datatype.guava.GuavaTypeModifier Maven / Gradle / Ivy
package io.prestosql.jdbc.$internal.jackson.datatype.guava;
import java.io.Serializable;
import java.lang.reflect.Type;
import io.prestosql.jdbc.$internal.jackson.databind.JavaType;
import io.prestosql.jdbc.$internal.jackson.databind.type.*;
import io.prestosql.jdbc.$internal.guava.base.Optional;
import io.prestosql.jdbc.$internal.guava.collect.*;
/**
* We need somewhat hacky support for following Guava types:
*
* - FluentIterable: addition of seeming "empty" property should not prevent serialization as
* basic `Iterable` (with standard Jackson (de)serializer)
*
* - Multimap: can reuse much/most of standard Map support as long as we make sure it is
* recognized as "Map-like" type (similar to how Scala Maps are supported)
*
* - Optional: generic type, simpler, more-efficient to detect parameterization here (although
* not strictly mandatory)
*
Range: same as with Optional, might as well resolve generic type information early on
* -
*
*
*/
public class GuavaTypeModifier extends TypeModifier
implements Serializable
{
static final long serialVersionUID = 1L;
@Override
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, TypeFactory typeFactory)
{
if (type.isReferenceType() || type.isContainerType()) {
return type;
}
final Class raw = type.getRawClass();
// First: make Multimaps look more Map-like
if (raw == Multimap.class) {
return MapLikeType.upgradeFrom(type,
type.containedTypeOrUnknown(0),
type.containedTypeOrUnknown(1));
}
if (raw == Optional.class) {
return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
}
return type;
}
}