Generics Axiom disproved?

Hello,

today I was toying with the combination of Generics and varargs (see also my other post: http://forum.java.sun.com/thread.jspa?threadID=5138461). To my own surprise, I came up with the following program which seems to disprove the fundamental claim made by the inventors of Java Generics:

if a Java program compiles without any "unchecked" warnings, it is guaranteed not to fail at runtime with ClassCastException.

Am I overlooking something?

publicclass FailsAtRuntime{

publicstatic <T> T[] getArray(){

return propagate();

}

privatestatic <T> T[] propagate(T... argv){

return argv;

}

publicstaticvoid main(String[] args){

String[] a = getArray();

System.out.println(a);

}

}

[1384 byte] By [McNeppa] at [2007-11-26 18:56:22]
# 1

Clearly your call to propagate is unsafe,

and a warning should be raised here.

public static <T> T[] getArray() {

return propagate(); // it's unsafe here

}

You try to create an array of T.

I've checked with javac (jdk1.7b05) and eclipse 3.3M4

and both report an unchecked warning here.

R閙i

remi_foraxa at 2007-7-9 20:35:14 > top of Java-index,Core,Core APIs...
# 2

> I've checked with javac (jdk1.7b05) and eclipse

> 3.3M4

> and both report an unchecked warning here.

Thanks for checking that out!

So it's the Eclipse compiler (again) that gets this wrong. I'm using release 3.2.1

Good to hear they've fixed it in the next release!

McNeppa at 2007-7-9 20:35:14 > top of Java-index,Core,Core APIs...
# 3
Yes. It's eclipse. JDK1.5_10 and JDK1.6 also give that warning. Even when "enforcing" the type like soFailsAtRuntime.<T>propagate();
stefan.schulza at 2007-7-9 20:35:14 > top of Java-index,Core,Core APIs...