I’m stepping into an open source project, currently built using Maven2. I imported the project into Eclipse and this bit of code is giving me problems:
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;
}
}
}
Both Function and Predicate have apply methods (they’re from the Google commons):
public interface Function<F, T> {
T apply(@Nullable F from);
...
}
public interface Predicate<T> {
boolean apply(@Nullable T input);
}
Here’s the error I get, in Eclipse only:
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
So, what’s going on here? Is it a compiler flag?
For what it’s worth, I’m running Eclipse 3.6, and maven is building using Java 1.6 on my OSX box:
Version: Helios Release
Build id: 20100617-1415
javac -version
javac 1.6.0_20
The problem here is that the erasures (everything between the angle brackets) are removed at compile time, making the parameters of the two apply methods identical. The return types differ, but the return type is not part of the method’s signature in Java.
Sun’s JDK and Eclipse up to 3.5 don’t complain about this ambiguity (I think they do in fact look at the return types), but Eclipse 3.6 fixed that bug.
It should work in Eclipse 3.5, yet it’s probably only a matter of time until other JDKs will fix this too, so I suggest implementing the methods explicitly or renaming one of them.