java - 为什么这在 Maven 下编译正常,但在 Eclipse 中给我错误?

标签 java eclipse maven-2

我正在进入一个开源项目,目前使用 Maven2 构建。我将项目导入到 Eclipse 中,这段代码给我带来了问题:

public static enum HierarchyType implements Function<MonetarySummary, SumOfMoney>, Predicate<Txaction> {
    EARNINGS {
        @Override
        public SumOfMoney apply(MonetarySummary summary) {
            return summary.getEarnings();
        }

        @Override
        public boolean apply(Txaction txaction) {
            return txaction.getAmount().signum() > 0;
        }
    },
    SPENDING {
        @Override
        public SumOfMoney apply(MonetarySummary summary) {
            return summary.getSpending();
        }

        @Override
        public boolean apply(Txaction txaction) {
            return txaction.getAmount().signum() < 0;
        }
    },
    NET {
        @Override
        public SumOfMoney apply(MonetarySummary summary) {
            return summary.getNet();
        }

        @Override
        public boolean apply(Txaction txaction) {
            return true;
        }
    }
}

FunctionPredicate 都有 apply 方法(它们来自 Google commons):

public interface Function<F, T> {
   T apply(@Nullable F from);
   ...
}

public interface Predicate<T> {
   boolean apply(@Nullable T input);
}

这是我得到的错误,仅在 Eclipse 中:

Description Resource    Path    Location    Type
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it    
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it    
Name clash: The method apply(F) of type Function<F,T> has the same erasure as apply(T) of type Predicate<T> but does not override it    
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it    
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it    
Name clash: The method apply(T) of type Predicate<T> has the same erasure as apply(F) of type Function<F,T> but does not override it    

那么,这里发生了什么?它是编译器标志吗?

就其值(value)而言,我正在运行 Eclipse 3.6,而 maven 在我的 OSX 机器上使用 Java 1.6 进行构建:

Version: Helios Release
Build id: 20100617-1415

javac -version
javac 1.6.0_20

最佳答案

这里的问题是删除(尖括号之间的所有内容)在编译时被删除,使两个 apply 方法的参数相同。返回类型不同,但返回类型 is not part of the method's signature在 Java 中。

Sun 的 JDK 和 Eclipse 到 3.5 不会提示这种歧义(我认为它们实际上会查看返回类型),但 Eclipse 3.6 fixed that bug .

它应该在 Eclipse 3.5 中工作,但其他 JDK 也将修复此问题可能只是时间问题,因此我建议显式实现这些方法或重命名其中一个方法。

关于java - 为什么这在 Maven 下编译正常,但在 Eclipse 中给我错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265239/

相关文章:

java - 适用于 Lambda 的 AWS 开发工具包 V2 Maven 库是什么?

java - HTML5 - 验证

java - Maven : Multi Module order

java - 为什么选择巴克敏斯特而非Maven?

java - 处理 Fragment 中的 DrawerLayout

java - HashLoginService 和 Jetty9

eclipse - Eclipse Juno启动错误日志文件

maven-2 - 现有 Ant 构建文件的简单 Mavenization

java - 导入 Eclipse 插件

java - eclipse 警告 : "contains markers which may be removed by Sort Members" -- markers?