关于java:对象到通用类型转换的未经检查警告的定义在哪里?

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.

There is an unchecked conversion from the raw array type G[] to any array type type of the form G[].

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

据我所知,它指出,如果将原始类型强制转换为具有绑定类型参数的泛型类型,则会发生未选中的转换。那么,为什么下面的代码会生成未选中的警告:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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),为什么上面的代码应该生成未选中的强制转换警告,以及在何处定义此行为?


你看错误的部分是为什么它似乎没有意义的上下文中的例子。

你有一个预警unchecked铸造。你看unchecked转换节上(相关)。

"casts湖5.5.2检查和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.


因为当你在一个Object铸造在A编译器没有办法检查(或生成的代码检查),这是在AObject事实和需要A(或任何)。