【问题标题】:Mutually self-referencing type parameters compiling under JDK6 but not 7?JDK6下编译而不是7的相互自引用类型参数?
【发布时间】:2012-04-02 13:52:52
【问题描述】:

以下代码使用JDK6编译(我试过1.6.0_24

class XY<A extends XY<A, B>, B extends XY<B, A>> { } 

但是在JDK7下编译(例如1.7.0),我得到这个错误:

XY.java:1: error: type argument B is not within bounds of type-variable A
class XY<A extends XY<A, B>, B extends XY<B, A>> {
                                      ^
  where B,A are type-variables:
    B extends XY<B,A> declared in class XY
    A extends XY<A,B> declared in class XY
1 error

谁能指出这是否是对 Java 泛型的有意更改

【问题讨论】:

  • 使用 Java >= 1.7.0_02 对我来说编译得很好
  • 真的吗?你确定吗?我正在使用 1.7.0_02-b13
  • 这很有趣。它在 Eclipse 中使用 Java 7 特性进行编译。我通过命令行尝试了它,果然我得到了相同的编译器错误。

标签: java generics types


【解决方案1】:

这似乎是来自 OpenJDK 的错误,是由类型变量替换中的错误引起的。

当你切换泛型变量时出现:

class XY<A extends XY<A, B>, B extends XY<B, A>> { } 
class XY<A extends XY<B, A>, B extends XY<A, B>> { } 

它没有出现在这个:

class XY<A extends XY<A, B>, B extends XY<A, B>> { } 
class XY<A extends XY<B, A>, B extends XY<B, A>> { } 

您可以在此处查看另一个示例:http://old.nabble.com/Apparent-generics-compilation-bug-new-to-Java-1.7-td33378164.html

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多