Object to generic type conversion unchecked warning的定义在哪里?
Where is the definition of unchecked warning for Object to generic type conversion?
JLS 5.1.9 定义未检查转换如下:
Let G name a generic type declaration with n type parameters.
There is an unchecked conversion from the raw class or interface type
G to any parameterized type of the form G<T1,...,Tn>
.
There is an unchecked conversion from the raw array type G[]
to any array type type of the form G<T1,...,Tn>[]
.
Use of an unchecked conversion causes a compile-time unchecked warning
unless G<...>
is a parameterized type in which all type arguments are
unbounded wildcards, or the unchecked warning is suppressed by the
SuppressWarnings annotation
据我所知,如果您将原始类型转换为具有有限类型参数的泛型类型,则会发生未经检查的转换。那么为什么以下代码会生成未经检查的警告:
public class DoubleStar{
public static void print(Object object){
((A<String>)object).print();
}
public static void main(String[] args){
print(new Object());
}
}
class A<T>{
public void print(){
System.out.print("HELLO");
}
}
由于对象不是原始类型 (AFAIK) 为什么上面的代码会生成未经检查的转换警告以及定义此行为的位置?
因为当您将 Object
转换为 A<String>
时,编译器无法检查(或生成代码来检查)Object
确实是 A<String>
而不是 A<Integer>
(或其他)。
您看错了部分,这就是为什么它在您的示例上下文中似乎没有意义。
您收到的警告是未经检查的 cast。您正在查看有关未检查的 转化 (related) 的部分。
See 5.5.2 "Checked Casts and Unchecked Casts"
A cast from a type S to a type T is statically known to be correct if and only if S <: T
(§4.10).
A cast from a type S to a parameterized type (§4.5) T is unchecked
unless at least one of the following conditions holds:
- S <: T
- All of the type arguments (§4.5.1) of T are unbounded wildcards
- T <: S and S has no subtype X other than T where the type arguments of
X are not contained in the type arguments of T.
A cast from a type S to a type variable T is unchecked unless S <: T.
An unchecked cast from S to T is completely unchecked if the cast from
|S| to |T| is statically known to be correct. Otherwise, it is
partially unchecked.
An unchecked cast causes a compile-time unchecked warning, unless
suppressed by the SuppressWarnings annotation (§9.6.3.5).
A cast is checked if it is not statically known to be correct and it
is not unchecked.
If a cast to a reference type is not a compile-time error, there are
several cases:
- The cast is statically known to be correct.
No run-time action is performed for such a cast.
- The cast is a completely unchecked cast.
No run-time action is performed for such a cast.
- The cast is a partially unchecked cast.
Such a cast requires a run-time validity check. The check is performed
as if the cast had been a checked cast between |S| and |T|, as
described below.
- The cast is a checked cast.
Such a cast requires a run-time validity check. If the value at run
time is null, then the cast is allowed. Otherwise, let R be the class
of the object referred to by the run-time reference value, and let T
be the erasure (§4.6) of the type named in the cast operator. A cast
conversion must check, at run time, that the class R is assignment
compatible with the type T, via the algorithm in §5.5.3.
Note that R cannot be an interface when these rules are first applied
for any given cast, but R may be an interface if the rules are applied
recursively because the run-time reference value may refer to an array
whose element type is an interface type.
JLS 5.1.9 定义未检查转换如下:
Let G name a generic type declaration with n type parameters.
There is an unchecked conversion from the raw class or interface type G to any parameterized type of the form
G<T1,...,Tn>
.There is an unchecked conversion from the raw array type
G[]
to any array type type of the formG<T1,...,Tn>[]
.Use of an unchecked conversion causes a compile-time unchecked warning unless
G<...>
is a parameterized type in which all type arguments are unbounded wildcards, or the unchecked warning is suppressed by the SuppressWarnings annotation
据我所知,如果您将原始类型转换为具有有限类型参数的泛型类型,则会发生未经检查的转换。那么为什么以下代码会生成未经检查的警告:
public class DoubleStar{
public static void print(Object object){
((A<String>)object).print();
}
public static void main(String[] args){
print(new Object());
}
}
class A<T>{
public void print(){
System.out.print("HELLO");
}
}
由于对象不是原始类型 (AFAIK) 为什么上面的代码会生成未经检查的转换警告以及定义此行为的位置?
因为当您将 Object
转换为 A<String>
时,编译器无法检查(或生成代码来检查)Object
确实是 A<String>
而不是 A<Integer>
(或其他)。
您看错了部分,这就是为什么它在您的示例上下文中似乎没有意义。
您收到的警告是未经检查的 cast。您正在查看有关未检查的 转化 (related) 的部分。
See 5.5.2 "Checked Casts and Unchecked Casts"
A cast from a type S to a type T is statically known to be correct if and only if S <: T (§4.10).
A cast from a type S to a parameterized type (§4.5) T is unchecked unless at least one of the following conditions holds:
- S <: T
- All of the type arguments (§4.5.1) of T are unbounded wildcards
- T <: S and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T.
A cast from a type S to a type variable T is unchecked unless S <: T.
An unchecked cast from S to T is completely unchecked if the cast from |S| to |T| is statically known to be correct. Otherwise, it is partially unchecked.
An unchecked cast causes a compile-time unchecked warning, unless suppressed by the SuppressWarnings annotation (§9.6.3.5).
A cast is checked if it is not statically known to be correct and it is not unchecked.
If a cast to a reference type is not a compile-time error, there are several cases:
- The cast is statically known to be correct.
No run-time action is performed for such a cast.
- The cast is a completely unchecked cast.
No run-time action is performed for such a cast.
- The cast is a partially unchecked cast.
Such a cast requires a run-time validity check. The check is performed as if the cast had been a checked cast between |S| and |T|, as described below.
- The cast is a checked cast.
Such a cast requires a run-time validity check. If the value at run time is null, then the cast is allowed. Otherwise, let R be the class of the object referred to by the run-time reference value, and let T be the erasure (§4.6) of the type named in the cast operator. A cast conversion must check, at run time, that the class R is assignment compatible with the type T, via the algorithm in §5.5.3.
Note that R cannot be an interface when these rules are first applied for any given cast, but R may be an interface if the rules are applied recursively because the run-time reference value may refer to an array whose element type is an interface type.