开发者

Java Generics : Casting a raw type to any reifiable type doesn't generate unchecked cast warning

开发者 https://www.devze.com 2023-04-11 19:09 出处:网络
I have the following question regarding the code below: public class GenericBridgeMethods <T> { public static void main(String[] args) {

I have the following question regarding the code below:

public class GenericBridgeMethods <T> {

    public static void main(String[] args) {
        List obj = new ArrayList<Integer>();
        List <?> l1 = (List<?>) obj; // clause 1
        GenericBridgeMethods <?> g1 = (GenericBridgeMethods<?>) obj; // clause 2
   }

}

a. Clause 1 of course won't give an unchecked cast warning

b. Clause 2 also did not give an unchecked cast 开发者_Python百科warning

I noticed that a cast from a raw type (obj) to a ANY reifiable type (like GenericBridgeMethods or GenericBridgeMethods <?>) will not give a unchecked cast warning. If you run this code, a runtime error will occur at clause 2.

Shouldn't the compiler give a warning at clause 2

EDIT 1:

    ArrayList a1 = new ArrayList<Integer>(); // clause 3
    Number n1 = (Number)a1; // clause 4 ERROR
    Comparable c1 = (Comparable)a1; // clause 5

    List l1 = new ArrayList<Integer>(); // clause 6
    Number n2 = (Number)l1; // clause 7
    Comparable c2 = (Comparable)l1; // clause 8

Can anyone explain why only clause 4 has error?


Well, first off in GenericBridgeMethods as you have defined it, T is not a reifiable type. Reifiable means that the type will be encoded into the class and will be available at runtime. That is not true of T.

Clause 2 does not give a runtime warning because it is checked: There will be a runtime check that obj is type-assignable to the GenericBridgeMethods type. Since you've opted for a wildcard as the type parameter, nothing about T needs to be checked.

If on the other hand you did something like this:

GenericBridgeMethods<String> g1 = (GenericBridgeMethods<String>) obj;

that would give you an unchecked assignment warning because the fact that obj is a GenericBridgeMethods of Strings cannot be checked at runtime. However, the same warning would appear if you had done this:

List<String l1 = (List<String>) obj;

Edit

If you're confused as to why the compiler allows you to try to cast a List to a GenericBridgeMethods, the answer is because the compiler can't know the entire hierarchy of GenericBridgeMethods and its subclasses. There could be a subclass of GenericBridgeMethods that implements List, in which case the cast might be legitimate.

You will however get a compile error if you made GenericBridgeMethods a final class (and thus prevented it from having subclasses). In this case, you will get an inconvertable types error.

Just to show you your question has little to do with reifiable types and generics, take a look at this:

public static void main(String[] args) {
   List obj = new ArrayList<Integer>();

   //this is allowed (no warning), even though it will fail at runtime
   CharSequence sequence = (CharSequence) obj; 
}

You can explicitly cast obj to a CharSequence even though you know that it will fail at runtime. The reason is because all the compiler knows is that obj is a type of List. Since List is an interface, there could be an implementation of CharSequence that is also a List, and so the cast must be permitted.

Every explicit cast carries a degree of possibility that it could fail at runtime. Otherwise, it would be a redundant cast and the compiler should allow you to omit the explicit cast.

Edit - Regarding your "edit #1"

ArrayList a1 = new ArrayList<Integer>(); // clause 3
Number n1 = (Number)a1; // clause 4 ERROR
Comparable c1 = (Comparable)a1; // clause 5

List l1 = new ArrayList<Integer>(); // clause 6
Number n2 = (Number)l1; // clause 7
Comparable c2 = (Comparable)l1; // clause 8

You are wondering why only "clause 4" does not compile. I think I explained this already above and in the comments, but I'll go thsough this specific example for you step-by-step.

ArrayList a1 = new ArrayList<Integer>(); // clause 3
Number n1 = (Number)a1; // clause 4 ERROR

Casting a1 to Number does not work because Number and ArrayList are both classes, not interfaces. Because Java does not allow inheritance from multiple classes, for an object to be an instance of both Number and ArrayList, Number would have to be a subclass of ArrayList or vice versa. This is known to not be true at compile time.

ArrayList a1 = new ArrayList<Integer>(); // clause 3
Comparable c1 = (Comparable)a1; // clause 5

Since Comparable is an interface, a subclass of ArrayList might be a Comparable.

List l1 = new ArrayList<Integer>(); // clause 6
Number n2 = (Number)l1; // clause 7

Since List is an interface a subclass of Number could implement List. The compiler does not know when checking the cast that l1 holds an ArrayList.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号