JDK 1.7 breaks backward compatibility? (generics)

…衆ロ難τιáo~ 提交于 2019-11-28 13:19:09

You're quite right, under JLS3 this code should never have compiled and this was a bug in 1.6.

For the release of 1.7 much of the underlying type system was updated and this bug was fixed, the result is better type handling at the cost of some backward compatibility issues.

As for getting it to work in 1.7, I believe re-factoring is your only option.

It is one of the bugs in javac that have been fixed in Java 7 - you can find more information in the release notes. I'm afraid your only option is to rewrite that code if you want to switch to Java 7.

Area: Tools
Synopsis: A Class Cannot Define Two Methods with the Same Erased Signature but Two Different Return Types
Description: A class cannot define two methods with the same erased signature, regardless of whether the return types are the same or not. This follows from the JLS, Java SE 7 Edition, section 8.4.8.3. The JDK 6 compiler allows methods with the same erased signature but different return types; this behavior is incorrect and has been fixed in JDK 7.
Example:

class A {
    int m(List<String> ls) { return 0; }
   long m(List<Integer> ls) { return 1; }
}

This code compiles under JDK 5.0 and JDK 6, and is rejected under JDK 7.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!