Java Reference
In-Depth Information
How do we compute
H
<
Y
>
? As before, note that the declaration of
G
must in-
troduce a type parameter
S
, and there must be some (non-wildcard) invocation
of
H
,
H
<
U
1
>
, that is a supertype of
G
<
S
>
. However, substituting
? extends
U
for
S
is not generally valid. To see this, assume
U
1
=
T
[]
.
Instead, we produce an invocation of
H
,
H
<? extends
U
1
>[
S
=
U
]
. In the simplest
instance,
U
1
might be
S
, in which case we have
G
<
S
> <:
H
<
S
>, and
G
<? extends
U
> <:
H
<? extends
U
>
=
H
<? extends
S
>[
S
=
U
]
=
V
.
Otherwise, if
A
is of the form
G
<
...,
X
k-1
,
? extends
W
,
X
k+1
, ...
>
, this algorithm
is applied recursively to the constraint
W
>>
U
.
We have
A
=
G
<? extends
W
>
>>
F
=
G
<? extends
U
>
for some type expression
W
.
By the subtyping rules for wildcards it must be the case that
W
>>
U
.
Otherwise, no constraint is implied on
T
j
.
• If
F
has the form
G
<
...,
Y
k-1
,
? super
U
,
Y
k+1
, ...
>
, where
U
is a type expression that
involves
T
j
, then
A
is either:
♦ If
A
is an instance of a non-generic type, then no constraint is implied on
T
j
.
Restricting the analysis to the unary case, we have the constraint
A
>>
F
=
G
<?
super
U
>
.
A
must be a supertype of the generic type
G
. However, since
A
is not
a parameterized type, it cannot depend upon
U
in any way. It is a supertype of
the type
G
<? super
X
>
for every
X
such that
? super
X
is a valid type argument to
G
. No meaningful constraint on
U
can be derived from
A
.
♦ If
A
is an invocation of a generic type declaration
H
, where
H
is either
G
or su-
perclass or superinterface of
G
, then:
If
H
≠
G
, then let
S
1
, ...,
S
n
be the type parameters of
G
, and let
H
<
U
1
, ...,
U
l
>
be the unique invocation of
H
that is a supertype of
G
<
S
1
, ...,
S
n
>
, and let
V
=
H
<? super
U
1
, ...,
? super
U
l
>[
S
k
=
U
]
. Then this algorithm is applied recursively
to the constraint
A
>>
V
.
The treatment here is analogous to the case where
A
=
G
<? extends
U
>
. Here our
example would produce an invocation
H
<? super
U
1
>[
S
=
U
]
.
Otherwise, if
A
is of the form
G
<
...,
X
k-1
,
? super
W
, ...,
X
k+1
, ...
>
, this al-
gorithm is applied recursively to the constraint
W
<<
U
.
We have
A
=
G
<? super
W
>
>>
F
=
G
<? super
U
>
for some type expression
W
. It
must be the case that
W
<<
U
, by the subtyping rules for wildcard types.
Otherwise, no constraint is implied on
T
j
.