泛型在Eclipse中编译和运行,但不在javac中编译 [英] Generics compiles and runs in Eclipse, but doesn't compile in javac

查看:842
本文介绍了泛型在Eclipse中编译和运行,但不在javac中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


注意:这是从与空值相关的可比较和比较器合约

此代码编译和运行在Eclipse中( 20090920-1017

This code compiles and runs fine in Eclipse (20090920-1017)

import java.util.*;
public class SortNull {
   static <T extends Comparable<? super T>>
   Comparator<T> nullComparableComparator() {
      return new Comparator<T>() {
         @Override public int compare(T el1, T el2) {
         return
            el1 == null ? -1 :
            el2 == null ? +1 :
            el1.compareTo(el2);
         }
      };
   }
   public static void main(String[] args) {
      List<Integer> numbers = new ArrayList<Integer>(
         Arrays.asList(3, 2, 1, null, null, 0)
      );
      Comparator<Integer> numbersComp = nullComparableComparator();
      Collections.sort(numbers, numbersComp);
      System.out.println(numbers);
      // "[null, null, 0, 1, 2, 3]"

      List<String> names = new ArrayList<String>(
         Arrays.asList("Bob", null, "Alice", "Carol")
      );
      Comparator<String> namesComp = nullComparableComparator();
      Collections.sort(names, namesComp);
      System.out.println(names);
      // "[null, Alice, Bob, Carol]"
   }
}


b $ b

但它不能在 javac 1.6.0_17 上编译。这是错误消息:

And yet it doesn't compile on javac 1.6.0_17. This is the error message:

SortNull.java:17: incompatible types; no instance(s) of type variable(s) T exist
 so that java.util.Comparator<T> conforms
 to java.util.Comparator<java.lang.Integer>
found   : <T>java.util.Comparator<T>
required: java.util.Comparator<java.lang.Integer>
     Comparator<Integer> numbersComp = nullComparableComparator();

SortNull.java:25: incompatible types; no instance(s) of type variable(s) T exist
 so that java.util.Comparator<T> conforms
 to java.util.Comparator<java.lang.String>
found   : <T>java.util.Comparator<T>
required: java.util.Comparator<java.lang.String>
     Comparator<String> namesComp = nullComparableComparator();

2 errors

有人可以解释为什么会出现差异吗?这是一个错误?

Can someone explain why the discrepancy? Is this a bug? If so, who has the bug?

推荐答案

这是一个已确认的错误:错误ID 6468354 。这里是一个相关性的摘要:

This is a confirmed bug: Bug ID 6468354. Here's an extract of relevance:


这个问题是由于这样的事实,有时javac的实现 JLS3 15.12.2.8 忽略递归边界,有时不会案件)。当递归边界包含通配符时,在计算非推断的类型变量时包括这样的边界。这使得随后的子类型 test(Integer< ;:Comparable< ;? super T> 其中 T 是类型变量

This problem is caused by the fact that sometimes javac's implementation of JLS3 15.12.2.8 ignores recursive bounds, sometimes not (as in this case). When recursive bounds contains wildcards, such bounds are included when computing uninferred type variables. This makes subsequent subtyping test (Integer <: Comparable<? super T> where T is a type-variable to be inferred).

将在 6369605

Will be fixed after 6369605

我在WinXP上发现了1.6.0_13好。啊,我只是坚持使用Eclipse:)

Occured to me on WinXP with 1.6.0_13 as well. Ah well, I'll just stick using Eclipse :)

这篇关于泛型在Eclipse中编译和运行,但不在javac中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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