use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.
the class TypeBindings method _resolveBindings.
protected void _resolveBindings(Type t) {
if (t == null)
return;
Class<?> raw;
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Type[] args = pt.getActualTypeArguments();
if (args != null && args.length > 0) {
Class<?> rawType = (Class<?>) pt.getRawType();
TypeVariable<?>[] vars = rawType.getTypeParameters();
if (vars.length != args.length) {
throw new IllegalArgumentException("Strange parametrized type (in class " + rawType.getName() + "): number of type arguments != number of type parameters (" + args.length + " vs " + vars.length + ")");
}
for (int i = 0, len = args.length; i < len; ++i) {
TypeVariable<?> var = vars[i];
String name = var.getName();
if (_bindings == null) {
_bindings = new LinkedHashMap<String, JavaType>();
} else {
// collected earlier (since we descend towards super-classes):
if (_bindings.containsKey(name))
continue;
}
// first: add a placeholder to prevent infinite loops
_addPlaceholder(name);
// then resolve type
_bindings.put(name, _typeFactory._constructType(_classStack, args[i], this));
}
}
raw = (Class<?>) pt.getRawType();
} else if (t instanceof Class<?>) {
raw = (Class<?>) t;
/* [JACKSON-677]: If this is an inner class then the generics are defined on the
* enclosing class so we have to check there as well. We don't
* need to call getEnclosingClass since anonymous classes declare
* generics
*/
Class<?> decl = ClassUtil.getDeclaringClass(raw);
/* 08-Feb-2013, tatu: Except that if context is also super-class, we must
* skip it; context will be checked anyway, and we'd get StackOverflow if
* we went there.
*/
if (decl != null && !decl.isAssignableFrom(raw)) {
_resolveBindings(decl);
}
/* 24-Mar-2010, tatu: Can not have true generics definitions, but can
* have lower bounds ("<T extends BeanBase>") in declaration itself
*/
TypeVariable<?>[] vars = raw.getTypeParameters();
if (vars != null && vars.length > 0) {
JavaType[] typeParams = null;
if (_contextType != null && raw.isAssignableFrom(_contextType.getRawClass())) {
typeParams = _typeFactory.findTypeParameters(_contextType, raw);
}
for (int i = 0; i < vars.length; i++) {
TypeVariable<?> var = vars[i];
String name = var.getName();
Type varType = var.getBounds()[0];
if (varType != null) {
if (_bindings == null) {
_bindings = new LinkedHashMap<String, JavaType>();
} else {
// and no overwriting...
if (_bindings.containsKey(name))
continue;
}
// to prevent infinite loops
_addPlaceholder(name);
if (typeParams != null && typeParams.length > i) {
_bindings.put(name, typeParams[i]);
} else {
_bindings.put(name, _typeFactory._constructType(_classStack, varType, this));
}
}
}
}
} else {
// if (type instanceof WildcardType) {
return;
}
// but even if it's not a parameterized type, its super types may be:
_resolveBindings(ClassUtil.getGenericSuperclass(raw));
for (Type intType : raw.getGenericInterfaces()) {
_resolveBindings(intType);
}
}
use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.
the class TestTypedContainerSerialization method testIssue329.
public void testIssue329() throws Exception {
ArrayList<Animal> animals = new ArrayList<Animal>();
animals.add(new Dog("Spot"));
JavaType rootType = TypeFactory.defaultInstance().constructParametrizedType(Iterator.class, Iterator.class, Animal.class);
String json = mapper.writerFor(rootType).writeValueAsString(animals.iterator());
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
fail("No polymorphic type retained, should be; JSON = '" + json + "'");
}
}
use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.
the class TypeNameIdResolver method construct.
public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType, Collection<NamedType> subtypes, boolean forSer, boolean forDeser) {
// sanity check
if (forSer == forDeser)
throw new IllegalArgumentException();
Map<String, String> typeToId = null;
Map<String, JavaType> idToType = null;
if (forSer) {
typeToId = new HashMap<String, String>();
}
if (forDeser) {
idToType = new HashMap<String, JavaType>();
// 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
// see [databind#1198] for details.
typeToId = new TreeMap<String, String>();
}
if (subtypes != null) {
for (NamedType t : subtypes) {
/* no name? Need to figure out default; for now, let's just
* use non-qualified class name
*/
Class<?> cls = t.getType();
String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
if (forSer) {
typeToId.put(cls.getName(), id);
}
if (forDeser) {
// One more problem; sometimes we have same name for multiple types;
// if so, use most specific
JavaType prev = idToType.get(id);
if (prev != null) {
// Can only override if more specific
if (cls.isAssignableFrom(prev.getRawClass())) {
// nope, more generic (or same)
continue;
}
}
idToType.put(id, config.constructType(cls));
}
}
}
return new TypeNameIdResolver(config, baseType, typeToId, idToType);
}
use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.
the class CollectionLikeType method withHandlersFrom.
@Override
public JavaType withHandlersFrom(JavaType src) {
JavaType type = super.withHandlersFrom(src);
JavaType srcCt = src.getContentType();
if (srcCt != null) {
JavaType ct = _elementType.withHandlersFrom(srcCt);
if (ct != _elementType) {
type = type.withContentType(ct);
}
}
return type;
}
use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.
the class MapLikeType method withHandlersFrom.
@Override
public JavaType withHandlersFrom(JavaType src) {
JavaType type = super.withHandlersFrom(src);
JavaType srcKeyType = src.getKeyType();
// "withKeyType()" not part of JavaType, hence must verify:
if (type instanceof MapLikeType) {
if (srcKeyType != null) {
JavaType ct = _keyType.withHandlersFrom(srcKeyType);
if (ct != _keyType) {
type = ((MapLikeType) type).withKeyType(ct);
}
}
}
JavaType srcCt = src.getContentType();
if (srcCt != null) {
JavaType ct = _valueType.withHandlersFrom(srcCt);
if (ct != _valueType) {
type = type.withContentType(ct);
}
}
return type;
}
Aggregations