Java Reference
In-Depth Information
This follows from the covariant subtype relation among array types. The con-
straint
A
<<
F
in this case means that
A
<<
U
[]
.
A
is therefore necessarily an
array type
V
[]
, or a type variable whose upper bound is an array type
V
[]
- oth-
erwise the relation
A
<<
U
[]
could never hold true. It follows that
V
[]
<<
U
[]
.
Since array subtyping is covariant, it must be the case that
V
<<
U
.
• If
F
has the form
G
<
...,
Y
k-1
,
U
,
Y
k+1
, ...
>
, where
U
is a type expression that in-
volves
T
j
, then if
A
has a supertype of the form
G
<
...,
X
k-1
,
V
,
X
k+1
, ...
>
where
V
is a
type expression, this algorithm is applied recursively to the constraint
V
=
U
.
For simplicity, assume that
G
takes a single type argument. If the method in-
vocation being examined is to be applicable, it must be the case that
A
is a
subtype of some invocation of
G
. Otherwise,
A
<<
F
would never be true.
In other words,
A
<<
F
, where
F
=
G
<
U
>
, implies that
A
<<
G
<
V
>
for some
V
.
Now, since
U
is a type expression (and therefore,
U
is not a wildcard type ar-
gument), it must be the case that
U
=
V
, by the non-variance of ordinary para-
meterized type invocations.
The formulation above merely generalizes this reasoning to generics with an
arbitrary number of type arguments.
• If
F
has the form
G
<
...,
Y
k-1
,
? extends
U
,
Y
k+1
, ...
>
, where
U
involves
T
j
, then if
A
has a supertype that is one of:
♦
G
<
...,
X
k-1
,
V
,
X
k+1
, ...
>
, where
V
is a type expression. Then this algorithm is
applied recursively to the constraint
V
<<
U
.
Again, let's keep things as simple as possible, and consider only the case
where
G
has a single type argument.
A
<<
F
in this case means
A
<<
G
<? extends
U
>
. As above, it must be the case
that
A
is a subtype of some invocation of
G
. However,
A
may now be a sub-
type of either
G
<
V
>
, or
G
<? extends
V
>
, or
G
<? super
V
>
. We examine these cases
in turn. The first variation is described (generalized to multiple arguments) by
the sub-bullet directly above. We therefore have
A
=
G
<
V
>
<<
G
<? extends
U
>
.
The rules of subtyping for wildcards imply that
V
<<
U
.
♦
G
<
...,
X
k-1
,
? extends
V
,
X
k+1
, ...
>
. Then this algorithm is applied recursively to
the constraint
V
<<
U
.
Extending the analysis above, we have
A
=
G
<? extends
V
>
<<
G
<? extends
U
>
.
The rules of subtyping for wildcards again imply that
V
<<
U
.
♦ Otherwise, no constraint is implied on
T
j
.