简体   繁体   English

泛型在Eclipse中编译和运行,但不能在javac中编译

[英]Generics compiles and runs in Eclipse, but doesn't compile in javac

Note : This is a spin-off from Comparable and Comparator contract with regards to null 注意 :这是关于null的Comparable和Comparator合同的衍生产品

This code compiles and runs fine in Eclipse ( 20090920-1017 ) 这段代码在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]"
   }
}

And yet it doesn't compile on javac 1.6.0_17 . 然而它不能在javac 1.6.0_17javac 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? 如果是这样,谁有错误?

This is a confirmed bug: Bug ID 6468354 . 这是一个确认的错误: 错误ID 6468354 Here's an extract of relevance: 这是相关的摘录:

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). 这个问题是由于有时javac的JLS3 15.12.2.8的实现忽略了递归边界,有时候没有(如本例所示)。 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). 这使得后续的子类型test (Integer <: Comparable<? super T> ,其中T是要推断的类型变量)。

Will be fixed after 6369605 将在6369605之后修复

Occured to me on WinXP with 1.6.0_13 as well. 也是在WinXP上以1.6.0_13向我发出的。 Ah well, I'll just stick using Eclipse :) 好吧,我只会坚持使用Eclipse :)

您可以通过显式指定泛型类来解决这个问题:

Comparator<String> namesComp = Stack.<String>nullComparableComparator();

我有一个类似的问题,并从jdk1.6.0_16升级到jdk1.6.0_23,它没有任何代码更改就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM