Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this generic cast fail?

I'd expect this code to throw a ClassCastException:

public class Generics {
    public static void main(String[] args) {
        method(Integer.class);
    }

    public static <T> T method(Class<T> t) {
        return (T) new String();
    }
}

But it doesn't. Casting String to T doesn't fail, until I use the returned object somehow, like:

public class Generics {
    public static void main(String[] args) {
        method(Integer.class).intValue();
    }

    public static <T> T method(Class<T> t) {
        return (T) new String();
    }
}

Background: I created a Class which uses JAXB to unmarshal an XML file. It looks like this:

public static <T> T unmarshal(File file, Class<? extends T> clazz)

Depending on whether the root-Element is an anonymous type or not, either T or JAXBElement is being returned. JAXBElement of course, can't be casted to T. In my unit test, where I only called unmarshal() without doing something with the result, everything worked fine. In Code, it failed.

Why doesn't it fail directly? Is this a bug? If not, I'd like to understand why.

like image 413
Michel Jung Avatar asked Nov 19 '10 12:11

Michel Jung


2 Answers

If T is not explicitly specified, the type erasure will treat it as Object. Therfore, your String object can be casted...

like image 139
Nicolas Avatar answered Sep 20 '22 09:09

Nicolas


You don't explicitly specified, so the T is Object.

So the look up look like this

public class Generics {

    public static void main(String[] args) {
        Generics.method(Integer.class).intValue();
    }

    public static Object method(Class<Object> t) {
        return (Object) new String();
    }
}

If you specify the generic parameter:

public class Generics {

    public static void main(String[] args) {
        Generics.<Integer>method(Integer.class).intValue();
    }

    public static <T> T method(Class<T> t) {
        return (T) new String();
    }
}

You will get that exception .

like image 24
Damian Leszczyński - Vash Avatar answered Sep 21 '22 09:09

Damian Leszczyński - Vash