Repository navigation
Description
(follow-up to #127)
When resolving types, wildcards and raw types are erased to their upper bound (Wrapper<?> and raw Wrapper both become Wrapper<Object>; raw NumWrapper<N extends Number> becomes NumWrapper<Number>). After that, TypeResolver.resolveSubtype() cannot tell them apart from explicitly declared types, so it compares them for equality instead of treating them as upper bounds.
Fails, but should succeed
static class Wrapper<T> { }
static class ArrayWrapper<E> extends Wrapper<E[]> { }
static class IntWrapper extends Wrapper<Integer> { }
static class NumWrapper<N extends Number> { }
static class IntNumWrapper extends NumWrapper<Integer> { }
static class Pair<A,B> { }
static class SamePair<E> extends Pair<E,E> { }
static class RawExt extends Wrapper { } // raw
TypeResolver r = new TypeResolver();
// raw / unbounded wildcard into array type parameter:
// "Type parameter #1/1 differs; expected java.lang.Object, got <0>[]"
r.resolveSubtype(r.resolve(Wrapper.class), ArrayWrapper.class);
r.resolveSubtype(r.resolve(new GenericType<Wrapper<?>>() { }), ArrayWrapper.class);
// bounded wildcard / bounded raw type:
// "Type parameter #1/1 differs; expected java.lang.Number, got java.lang.Integer"
r.resolveSubtype(r.resolve(new GenericType<Wrapper<? extends Number>>() { }), IntWrapper.class);
r.resolveSubtype(r.resolve(NumWrapper.class), IntNumWrapper.class);
// nested wildcard in a repeated type variable binding:
// "Conflicting bindings for type variable `E` of SamePair: List<Object> vs List<String>"
r.resolveSubtype(r.resolve(new GenericType<Pair<List<?>,List<String>>>() { }), SamePair.class);
// subtype extending raw type (unchecked, but assignable):
// "Type parameter #1/1 differs; expected java.lang.String, got java.lang.Object"
r.resolveSubtype(r.resolve(Wrapper.class, String.class), RawExt.class);Succeeds, but arguably should not
Since an explicit Object cannot be told apart from a wildcard, the special case added for #127 (a top-level Object binding of a repeated type variable is compatible with a more specific one) also accepts:
// returns SamePair<String>
r.resolveSubtype(r.resolve(Pair.class, String.class, Object.class), SamePair.class);and the bounds check added for #127 skips all Object type parameters.
Suggested approach
Mark bindings that come from wildcards or raw types (in TypeBindings / ResolvedType), and verify them in resolveSubtype() as upper bounds (assignability) rather than by equality, binding any type placeholders within to their type variables' bounds. That would replace the Object special cases added for #127.
Activity
cowtowncoder commentedon Oct 9, 2026
Two related items deferred from #127:
Bounds of type variables are not verified
resolveSubtype()does not verify that type parameters of the subtype satisfy bounds of its type variables:A check was tried for #127, but it can not be made reliable as long as wildcards are erased to their upper bound:
Wrapper<? extends Number>resolves toWrapper<Number>, so refining it intoIntOnlyWrapper<E extends Integer> extends Wrapper<E>bindsEtoNumber, which would fail the bound check even though Java allows it. Once wildcard/raw-derived bindings are tracked (as suggested above), bounds can be checked for exact bindings only.Self-reference supertype resolves to the referenced type, not its own bindings
(same as in 1.7). Using bindings of the self-reference itself instead would fix this, but breaks raw self-bounded types: bindings of the self-reference within raw
Enumare<Object>(to break the cycle), so it would becomeEnum<Object>instead of rawEnum. These two cases can not currently be told apart.Fix FasterXML#130: `resolveSubtype()` treats wildcard and raw type pa…