开发者

Unchecked Cast Warning when calling 'Class.forName'

开发者 https://www.devze.com 2023-03-18 10:07 出处:网络
My code is as follows package com.foo; public class TestComposition { 开发者_运维问答public static void main(String[] args) {

My code is as follows

package com.foo;

public class TestComposition {
    开发者_运维问答public static void main(String[] args) {
        try {
            Class<Foo> fooClass = 
                    (Class<Foo>) Class.forName("Foo");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

The assignment within the 'try' block results in a warning stating

Type safety: Unchecked cast from Class<capture#1-of ?> to 
     Class<Foo>

Why is this?


Firstly, if you know the exact class then you don't need Class.forName. Foo.class will do. (Prior to J2SE 5.0, Foo.class actually compiled to doing Class.forName("Foo") and then caching it in a static.)

What you probably want is something like:

Class<? extends Foo> fooClass =
    (Class<? extends Foo>) Class.forName("MyFoo");

(When I say want, casts and particularly reflection are evil.)

As it happens, you there is an appropriate method to do the cast "safely" (assuming Foo is not itself generic). Safely as in not providing a reference to an incompatible object, not as in there are no bug or no unexpected exception.

Class<? extends Foo> fooClass =
    Class.forName("MyFoo").asSubclass(Foo.class);


Well, to start with let's be clear where the problem is - it's in the cast itself. Here's a shorter example:

public class Test {    
    public static void main(String[] args) throws Exception {
        Object x = (Class<Test>) Class.forName("Test");
    }
}

This still has the same problem. The issue is that the cast isn't actually going to test anything - because the cast will be effectively converted to the raw Class type. For Class<T> it's slightly more surprising because in reality the object does know the class involved, but consider a similar situation:

List<?> list = ... get list from somewhere;
List<String> stringList = (List<String>) list;

That cast isn't going to check that it's really a List<String>, because that information is lost due to type erasure.

Now in your case, there's actually a rather simpler solution - if you know the class name at compile-time anyway, just use:

Class<Foo> fooClass = Foo.class;

If you can provide a more realistic example where that's not the case, we can help you determine the most appropriate alternative.


The runtime system has no knowledge about generics and therfore it can only check that something is of type Class, not if it is of type Class<Test>. That is what the warning is about.

A method has specifically been added to the Class class to avoid this warning. You can use this instead of casting yourself: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#asSubclass(java.lang.Class)


If you knew the exact class, you wouldn't need to use Class.forName

Class<Foo> fooClass = Foo.class;

It only makes sense to do this when you don't know the type.

0

精彩评论

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

关注公众号