为什么这个Java 8程序不能编译? [英] Why does this Java 8 program not compile?

查看:192
本文介绍了为什么这个Java 8程序不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序在Java 7中(或者在Java 8中使用 -source 7 )进行编译,但无法使用Java 8进行编译:

 界面Iface< T> {} 
class Impl实现了Iface< Impl> {}

class Acceptor< T extends Iface< T>> {
public Acceptor(T obj){}
}

public class Main {
public static void main(String [] args){
Acceptor< ;?> acceptor = new Acceptor<>(new Impl());


$ / code $ / pre
$ b $结果:

  Main.java:10:错误:不兼容的类型:无法推断Acceptor<>的类型参数。 
Acceptor<?> acceptor = new Acceptor<>(new Impl());
$ b b $ b reason:推理变量T具有不兼容的边界
等式约束:Impl
上界:Iface< CAP#1>,Iface< T>
其中T是类型变量:
T扩展Iface< T>在类Acceptor
中声明,其中CAP#1是新鲜类型变量:
CAP#1扩展Iface< CAP#1>从捕获?
1错误

换句话说,这是向后 Java 7和8之间的源代码不兼容。我已经通过 Java SE 8和Java SE 7之间的不兼容性列表,但没有找到符合我的问题的任何东西。

所以,这是一个错误吗?

/ b>

环境:

  $ / usr / lib / jvm / java- 8-oracle / bin / java -version 
Java版本1.8.0
Java™SE运行时环境(build 1.8.0-b132)
Java HotSpot™64- Bit Server VM(构建25.0-b70,混合模式)


解决方案

感谢您的报告。这看起来像一个错误。我会照顾它,并且一旦我们有更多关于这种情况发生的信息,可能会添加更好的答案。我已经提交了此错误记录 JDK-8043926 ,以便对其进行跟踪。


This program compiles fine in Java 7 (or in Java 8 with -source 7), but fails to compile with Java 8:

interface Iface<T> {}
class Impl implements Iface<Impl> {}

class Acceptor<T extends Iface<T>> {
    public Acceptor(T obj) {}
}

public class Main {
    public static void main(String[] args) {
        Acceptor<?> acceptor = new Acceptor<>(new Impl());
    }
}

Result:

Main.java:10: error: incompatible types: cannot infer type arguments for Acceptor<>
        Acceptor<?> acceptor = new Acceptor<>(new Impl());
                                           ^
    reason: inference variable T has incompatible bounds
      equality constraints: Impl
      upper bounds: Iface<CAP#1>,Iface<T>
  where T is a type-variable:
    T extends Iface<T> declared in class Acceptor
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Iface<CAP#1> from capture of ?
1 error

In other words, this is a backwards source incompatibility between Java 7 and 8. I've gone through Incompatibilities between Java SE 8 and Java SE 7 list but did not found anything that would fit my problem.

So, is this a bug?

Environment:

$ /usr/lib/jvm/java-8-oracle/bin/java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

解决方案

Thanks for the report. This looks like a bug. I will take care of it and probably add a better answer once we have more information about why this is happening. I have filed this bug entry JDK-8043926, to track it.

这篇关于为什么这个Java 8程序不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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