tags:

views:

1189

answers:

1

I've run into an issue in Java's Generics in which the same code will compile and work fine in Java 6, but will fail to compile because of the same erasure in Java 5. I have a file TestErasure.java that has an overloaded method, called "method":

import java.util.ArrayList;
import java.util.List;

public class TestErasure {
 public static Object method(List<Object> list) {
     System.out.println("method(List<Object> list)");
     return null;
 }

 public static String method(List<String> list) {
     System.out.println("method(List<String> list)");
     return null;
 }

 public static void main(String[] args) {
     method(new ArrayList<Object>()); 
     method(new ArrayList<String>()); 
 }
}

In Java 5, I get the expected compilation error, stating that the erasure of "method" is the same:

$ javac -version
javac 1.5.0_19
$ javac TestErasure.java
TestErasure.java:10: name clash: method(java.util.List<java.lang.String>) and method(java.util.List<java.lang.Object>) have the same erasure
        public static String method(List<String> list) {
                             ^
TestErasure.java:17: method(java.util.List<java.lang.Object>) in TestErasure cannot be applied to (java.util.ArrayList<java.lang.String>)
      method(new ArrayList<String>()); 
            ^
2 errors

However, Java 6 is able to compile and run this same code.

$ javac -version
javac 1.6.0_16
$ javac TestErasure.java
$ java TestErasure
method(List<Object> list)
method(List<String> list)

Based upon my current understanding of erasures (thanks to Jon Skeet and Angelika Langer), I actually expected the compilation error as thrown by Java 5 (unless something changed in how Java handled Generics--which I can not find on the Java 6 release notes). In fact, if I modify the return type of one of the overloaded methods:

public static Object method(List<Object> list) ...
public static Object method(List<String> list) ...

Java 6 also fails to compile because of the same erasures:

$ javac TestErasure.java TestErasure.java:5: name clash: method(java.util.List<java.lang.Object>) and method(java.util.List<java.lang.String>) have the same erasure
     public static Object method(List<Object> list) {
                          ^
TestErasure.java:10: name clash: method(java.util.List<java.lang.String>) and method(java.util.List<java.lang.Object>) have the same erasure
     public static Object method(List<String> list) {
                          ^
2 errors

It appears as if the return type in Java 6 somehow influences the selection of which overloaded method to use?

Can someone shed light on why the first example works in Java 6--it seems to go against the stated handling of overloaded generic methods?

More info:

Per David's suggestion, the original example, complied by javac 1.6, will run under the java 1.5:

$ javac -target 1.5 TestErasure.java
$ java -version
java version "1.5.0_19"
$ java TestErasure 
method(List<Object> list)
method(List<String> list)
+6  A: 

Found these bugs on Sun, which I think is what you're describing:

http://bugs.sun.com/view_bug.do?bug_id=6182950
http://bugs.sun.com/view_bug.do?bug_id=6730568

JRL
This bug seems to be exhibiting in Netbeans 6.9. https://netbeans.org/bugzilla/show_bug.cgi?id=187859
Brian Harris
Changeset db77bf6adb53 (23 October 2008) against the OpenJDK 7 code contains the fix: http://hg.openjdk.java.net/jdk7/build/langtools/rev/db77bf6adb53.
seh