Refine search
/** * Method for creating an instance that has same bindings as this object, * plus an indicator for additional type variable that may be unbound within * this context; this is needed to resolve recursive self-references. */ public TypeBindings withUnboundVariable(String name) { int len = (_unboundVariables == null) ? 0 : _unboundVariables.length; String[] names = (len == 0) ? new String[1] : Arrays.copyOf(_unboundVariables, len+1); names[len] = name; return new TypeBindings(_names, _types, names); }
final Class<?> rawBase = baseType.getRawClass(); if (rawBase == subclass) { return baseType; newType = _fromClass(null, subclass, EMPTY_BINDINGS); break; if (baseType.getBindings().isEmpty()) { newType = _fromClass(null, subclass, EMPTY_BINDINGS); break; if (baseType.isContainerType()) { if (baseType.isMapLikeType()) { if ((subclass == HashMap.class) || (subclass == EnumMap.class) || (subclass == TreeMap.class)) { newType = _fromClass(null, subclass, TypeBindings.create(subclass, baseType.getKeyType(), baseType.getContentType())); break; || (subclass == TreeSet.class)) { newType = _fromClass(null, subclass, TypeBindings.create(subclass, baseType.getContentType())); break;
public PlaceholderForType(int ordinal) { super(Object.class, TypeBindings.emptyBindings(), TypeFactory.unknownType(), null, 1, // super-class, super-interfaces, hashCode null, null, false); // value/type handler, as-static _ordinal = ordinal; }
protected JavaType _fromVariable(ClassStack context, TypeVariable<?> var, TypeBindings bindings) { // ideally should find it via bindings: final String name = var.getName(); if (bindings == null) throw new Error("No Bindings!"); JavaType type = bindings.findBoundType(name); if (type != null) { return type; } // but if not, use bounds... note that approach here is simplistic; not taking // into account possible multiple bounds, nor consider upper bounds. if (bindings.hasUnbound(name)) { return CORE_TYPE_OBJECT; } bindings = bindings.withUnboundVariable(name); Type[] bounds = var.getBounds(); return _fromAny(context, bounds[0], bindings); }
public static TypeBindings create(Class<?> erasedType, JavaType[] types) { if (types == null) { types = NO_TYPES; } else switch (types.length) { case 1: return create(erasedType, types[0]); case 2: return create(erasedType, types[0], types[1]); } TypeVariable<?>[] vars = erasedType.getTypeParameters(); String[] names; if (vars == null || vars.length == 0) { names = NO_STRINGS; } else { int len = vars.length; names = new String[len]; for (int i = 0; i < len; ++i) { names[i] = vars[i].getName(); } } // Check here to give better error message if (names.length != types.length) { throw new IllegalArgumentException("Cannot create TypeBindings for class "+erasedType.getName() +" with "+types.length+" type parameter" +((types.length == 1) ? "" : "s")+": class expects "+names.length); } return new TypeBindings(names, types, null); }
@Deprecated // since 2.7; remove from 2.8 public static MapLikeType construct(Class<?> rawType, JavaType keyT, JavaType valueT) { // First: may need to fabricate TypeBindings (needed for refining into // concrete collection types, as per [databind#1102]) TypeVariable<?>[] vars = rawType.getTypeParameters(); TypeBindings bindings; if ((vars == null) || (vars.length != 2)) { bindings = TypeBindings.emptyBindings(); } else { bindings = TypeBindings.create(rawType, keyT, valueT); } return new MapLikeType(rawType, bindings, _bogusSuperClass(rawType), null, keyT, valueT, null, null, false); }
|| Collection.class.isAssignableFrom(subclass)) { if (!baseType.getRawClass().isAssignableFrom(subclass)) { throw new IllegalArgumentException("Class "+subclass.getClass().getName()+" not subtype of "+baseType); JavaType subtype = _fromClass(subclass, new TypeBindings(this, baseType.getRawClass())); Object h = baseType.getValueHandler(); if (h != null) { subtype = subtype.withValueHandler(h);
_addPlaceholder(name); _bindings.put(name, _typeFactory._constructType(args[i], this)); _resolveBindings(raw.getDeclaringClass()); JavaType[] typeParams = null; if (_contextType != null && raw.isAssignableFrom(_contextType.getRawClass())) { typeParams = _typeFactory.findTypeParameters(_contextType, raw); if (_bindings.containsKey(name)) continue; _addPlaceholder(name); // to prevent infinite loops _bindings.put(name, _typeFactory._constructType(varType, this)); _resolveBindings(raw.getGenericSuperclass()); for (Type intType : raw.getGenericInterfaces()) { _resolveBindings(intType);
private String _resolveTypePlaceholders(JavaType sourceType, JavaType actualType) throws IllegalArgumentException List<JavaType> expectedTypes = sourceType.getBindings().getTypeParameters(); List<JavaType> actualTypes = actualType.getBindings().getTypeParameters(); for (int i = 0, len = expectedTypes.size(); i < len; ++i) { JavaType exp = expectedTypes.get(i); JavaType act = actualTypes.get(i); if (!_verifyAndResolvePlaceholders(exp, act)) { if (exp.hasRawClass(Object.class)) { continue;
placeholders[i] = new PlaceholderForType(i); TypeBindings b = TypeBindings.create(subclass, placeholders); JavaType tmpSub = _fromClass(null, subclass, b); JavaType baseWithPlaceholders = tmpSub.findSuperType(baseType.getRawClass()); if (baseWithPlaceholders == null) { // should be found but... throw new IllegalArgumentException(String.format( "Internal error: unable to locate supertype (%s) from resolved subtype %s", baseType.getRawClass().getName(), subclass.getName())); String error = _resolveTypePlaceholders(baseType, baseWithPlaceholders); if (error != null) { throw new IllegalArgumentException("Failed to specialize base type "+baseType.toCanonical()+" as " t = unknownType(); return TypeBindings.create(subclass, typeParams);
private boolean _verifyAndResolvePlaceholders(JavaType exp, JavaType act) { // See if we have an actual type placeholder to resolve; if yes, replace if (act instanceof PlaceholderForType) { ((PlaceholderForType) act).actualType(exp); return true; } // if not, try to verify compatibility. But note that we can not // use simple equality as we need to resolve recursively if (exp.getRawClass() != act.getRawClass()) { return false; } // But we can check type parameters "blindly" List<JavaType> expectedTypes = exp.getBindings().getTypeParameters(); List<JavaType> actualTypes = act.getBindings().getTypeParameters(); for (int i = 0, len = expectedTypes.size(); i < len; ++i) { JavaType exp2 = expectedTypes.get(i); JavaType act2 = actualTypes.get(i); if (!_verifyAndResolvePlaceholders(exp2, act2)) { return false; } } return true; }
@Override public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) { Class<?> resourceContentType = ctxt.getContextualType().getBindings().getTypeParameters().get(0).getRawClass(); return new ResourceDeserializer(resourceContentType, typeResolver, configuration); }
private JavaType constructJavaType(final Type type) { if (type instanceof ParameterizedType) { JavaType[] javaTypeArgs = new JavaType[((ParameterizedType) type).getActualTypeArguments().length]; for (int i = 0; i != ((ParameterizedType) type).getActualTypeArguments().length; ++i) { javaTypeArgs[i] = constructJavaType(((ParameterizedType) type).getActualTypeArguments()[i]); } return mapper.getTypeFactory().constructType(type, TypeBindings.create((Class<?>) ((ParameterizedType) type).getRawType(), javaTypeArgs)); } else { return mapper.getTypeFactory().constructType(type); } }
static AnnotatedType toJavaType(JavaType jacksonType) { if (jacksonType.getRawClass().getTypeParameters().length > 0) { AnnotatedType[] paramTypes = jacksonType.getBindings().getTypeParameters().stream() .map(TypeUtils::toJavaType) .toArray(AnnotatedType[]::new); return TypeFactory.parameterizedAnnotatedClass(jacksonType.getRawClass(), jacksonType.getRawClass().getAnnotations(), paramTypes); } if (jacksonType.isArrayType()) { return TypeFactory.arrayOf(toJavaType(jacksonType.getContentType()), new Annotation[0]); } return GenericTypeReflector.annotate(jacksonType.getRawClass()); } }
protected String getIntentName(JavaType javaType) { Intent intent = getIntentAnnotation(javaType); StringBuilder name = new StringBuilder(intent.value().isEmpty() ? javaType.getRawClass().getSimpleName() : intent.value()); if (javaType.hasGenericTypes()) { for (JavaType slotType : javaType.getBindings().getTypeParameters()) { name.append("_").append(resolveSlotType(slotType.getRawClass()) .getName() .replaceAll("\\.", "_")); } } return name.toString(); }
private JavaType resolve(final JavaType type) { Assert.notNull(type, "type can't be null"); JavaType tmp = type; while(Keeper.class.equals(tmp.getRawClass())) { TypeBindings bindings = tmp.getBindings(); Assert.isTrue(bindings.size() == 1, "Bindings must have one parameter type: " + type); tmp = bindings.getBoundType(0); } return tmp; }
/** * Determine the type of a generic property. * * @param property name of property * @return class of property type * @throws IllegalArgumentException if the generic property is not a {@link TypeVariable} */ public Class reifyPropertyType(PropertyDescriptor property) { Type type = property.getReadMethod().getGenericReturnType(); if (type instanceof TypeVariable<?>) { TypeVariable<?> pt = (TypeVariable<?>) type; return javaType.getBindings().findBoundType(pt.getTypeName()).getRawClass(); } else { return property.getPropertyType(); // throw new IllegalArgumentException("Can not reify property '" + property.getName() + "' type '" + type + "'"); } } }
protected void _resolve() { _resolveBindings(_contextClass); // finally: may have root level type info too if (_contextType != null) { int count = _contextType.containedTypeCount(); if (count > 0) { for (int i = 0; i < count; ++i) { String name = _contextType.containedTypeName(i); JavaType type = _contextType.containedType(i); addBinding(name, type); } } } // nothing bound? mark with empty map to prevent further calls if (_bindings == null) { _bindings = Collections.emptyMap(); } }
protected JavaType getType(TypeBindings bindings, TypeVariable<?>[] typeParams) { // [JACKSON-468] Need to consider local type binding declarations too... if (typeParams != null && typeParams.length > 0) { bindings = bindings.childInstance(); for (TypeVariable<?> var : typeParams) { String name = var.getName(); // to prevent infinite loops, need to first add placeholder ("<T extends Enum<T>>" etc) bindings._addPlaceholder(name); // About only useful piece of information is the lower bound (which is at least Object.class) Type lowerBound = var.getBounds()[0]; JavaType type = (lowerBound == null) ? TypeFactory.unknownType() : bindings.resolveType(lowerBound); bindings.addBinding(var.getName(), type); } } return bindings.resolveType(getGenericType()); }
@Override protected String buildCanonicalName() { StringBuilder sb = new StringBuilder(); sb.append(_class.getName()); final int count = _bindings.size(); if (count > 0) { sb.append('<'); for (int i = 0; i < count; ++i) { JavaType t = containedType(i); if (i > 0) { sb.append(','); } sb.append(t.toCanonical()); } sb.append('>'); } return sb.toString(); }