代码之家  ›  专栏  ›  技术社区  ›  n247s

可能的Eclipse错误

  •  1
  • n247s  · 技术社区  · 6 年前

    很好的一天,

    我正在努力解决一个通用类型问题。但似乎Eclipse在抱怨,而没有一个有效的抱怨。

    考虑以下方法

    public static <FR extends FilterResult, T> List<? super WrappedFilterResult<? super T, FR>> filter(String check, Collection<T> elements, Function<? super T, String> converter, Filter<? extends FR> filter, ACComparator<? super WrappedFilterResult<? super T, ? super FR>> comparator)
    {
        // eclipse says 'filter' doesn't accept these arguments
        return filter(check, elements, new ArrayList<>(), converter, filter, comparator);
    
        // doing a self call will result in the same error?
        // return filter(check, elements, converter, filter, comparator);
    
        // calling without returning doesn't solve it either?
        // filter(check, elements, converter, filter, comparator);
        // return null;
    }
    
    // no complaints here
    public static <FR extends FilterResult, T, C extends Collection<? super WrappedFilterResult<? super T, FR>>> C filter(String check, Collection<T> elements, C result, Function<? super T, String> converter, Filter<? extends FR> filter, ACComparator<? super WrappedFilterResult<? super T, ? super FR>> comparator)
    {
        // content
    }
    

    因为第一个方法Eclipse抱怨它不能调用 filter 方法,因为该方法 is not applicable for the arguments . 但即使我自己打电话也会抱怨。

    我想它可能是返回类型,只通过调用和返回就省略了它。 null 但遗憾的是,这也不能解决任何问题。

    对于复杂的方法声明很抱歉,但是我有更多的具有相同类型/数量参数的模拟方法可以正常工作。所以我不知道为什么这行不通。

    信息:

    • 视窗10
    • Eclipse Oxygen.3a释放(4.7.3a)



    我希望这是件小事,我看不到自动取款机,谢谢你的帮助。

    提前谢谢


    编辑

    如果有人需要类声明

    public static class FilterResult {}
    public interface Filter<FR extends FilterResult> {}
    public static class WrappedFilterResult<T, FR extends FilterResult> extends FilterResult {}
    public interface ACComparator<FR extends FilterResult> {}
    

    提交给Bugzilla
    螺纹连杆1 AT the eclipse bugzilla forum 螺纹连杆2 AT the eclipse bugzilla forum

    2 回复  |  直到 6 年前
        1
  •  2
  •   xtratic Rob    6 年前

    我假设这是一个EclipseJDT编译器bug,尽管我没有咨询过JLS,也不想深入研究它。

    我假设的理由有三个:

    1. 你的代码 compiles successfully under javac 8u112

    2. 我希望用自己的参数调用自己的方法应该编译。

    3. 早些时候,我也遇到了一个案件 Eclipse compiler disagreed with other compilers .

    MCVE将再现您的问题:

    public static class FilterResult {}
    public static class WrappedFilterResult<T, FR extends FilterResult> extends FilterResult {}
    public interface ACComparator<FR extends FilterResult> {}
    
    public static <FR extends FilterResult, T>
      void filter1(ACComparator<? super WrappedFilterResult<? super T, ? super FR>> comparator) {
        // both compile fine with normal Java compiler
        // but error with Eclipse JDT compiler (I'm using Eclipse 4.9.0)
        filter1(comparator);
        filter2(comparator);
    }
    
    public static <FR extends FilterResult, T>
      void filter2(ACComparator<? super WrappedFilterResult<? super T, ? super FR>> comparator) {
    }
    

    解决办法:

    拉扯冒犯类型( ACComparator<...etc> 在本例中),in to一个泛型类型参数似乎已经超过了Eclipse的这个问题。

    public static
    < FR extends FilterResult, T,
      A extends ACComparator<? super WrappedFilterResult<? super T, ? super FR>> // <-- here
    >
    void filterSuccess(A comparator) {
        // success!
        filter1(comparator);
        filter2(comparator);
    }
    
        2
  •  1
  •   Alexander Radchenko    6 年前

    好像就是那个虫子。在列表中,IDEA不会在该代码上显示任何错误。

    问题出在这里 new ArrayList<>() 在线中

    return filter(check, elements, new ArrayList<>(), converter, filter, comparator);

    如果我们把它换成 result 变量,定义为

    List<? super WrappedFilterResult<? super T, FR>> result = new ArrayList<>();

    更容易看到 结果 是适当的参数 C 类型,在哪里 C extends Collection<? super WrappedFilterResult<? super T, FR>> .

    但我们应该保护自己不受不受约束的分配…

    无论如何,是否有可能简化此代码?因为它的可读性和可维护性是有争议的…