JDK 1.7打破了向后兼容性? (泛型) [英] JDK 1.7 breaks backward compatibility? (generics)

查看:134
本文介绍了JDK 1.7打破了向后兼容性? (泛型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了类似的话题,但过于复杂,并不完全相同。所以事情就是这样。这里是(1.6)中罚款的(最小)代码,但不能用1.7 javac编译。

  public class Test {
私有静态类A< T> {};
private static class B {};
私有静态类C {};

B doSomething(A< B> arg){
返回新的B();
}

C doSomething(A< C> arg){
return new C();


1.7上的错误是:

  java:name冲突:doSomething(Test.A< Test.C>)和doSomething(Test.A&Test.B>)具有相同的擦除

我理解类型擦除以及它为什么是错误的代码。我只是不明白,为什么我们可以在我们的项目中编译和运行这个代码,当1.7有问题时。哪里不对? 1.6编译器中的错误是否允许我们这样做?是否有可能使其工作在1.7以外,而不是重写?


  • JDK1.6 javac版本:1.6.0_43

  • JDK1.7 javac版本:1.7.0_25


解决方案

非常正确,在JLS3下,这个代码不应该编译,这是1.6中的一个错误。

对于1.7版本的发布,更新了这个bug并修正了这个bug,结果是以一些向后兼容性问题为代价的更好的类型处理。 / p>

至于让它在1.7下工作,我相信重新分解是您唯一的选择。

I've found similar topics, but overly complicated and not quite the same. So the thing is. Here's the(minimal) code which is fine on 1.6, but doesn't compile with 1.7 javac.

public class Test {
    private static class A<T>{};
    private static class B{};
    private static class C{};

    B doSomething(A<B> arg){
        return new B();
    }

    C doSomething(A<C> arg){
        return new C();
    }
}

On 1.7 the error is this:

java: name clash: doSomething(Test.A<Test.C>) and doSomething(Test.A<Test.B>) have the same erasure

I understand the type erasure and why it's a wrong code. I just don't understand why we can have this code in our project compiling and running in 1.6, when 1.7 have problems with it. What is wrong? Is it a bug in 1.6 compiler that it allows us to do so? Is it possible to make it work in 1.7 other than rewriting?

  • JDK1.6 javac version: 1.6.0_43
  • JDK1.7 javac version: 1.7.0_25

解决方案

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.

这篇关于JDK 1.7打破了向后兼容性? (泛型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆