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 txact开发者_如何学Goion) {
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.
Yep, I think so. I haven't looked at this error in detail, but Eclipse lets you fine-tune a lot of error/warning messages for the Java compiler. You can try finding the one that applies to this message (ask again here on SO if you really can't find it) and turning it down, with luck that will "solve" your problem.
There's probably an underlying problem you should address, as the message isn't issued for the heck of it. But I don't understand generics well enough to fix it.
Something else that may be involved is the java compiler version. Check which compiler version level is set in Eclipse, and which one is called in Maven; they may well be different.
Eclipse has a bug: http://project.cyclos.org/forum/viewtopic.php?p=3326&sid=12349d17b21483ee8884faef3117a9f3
精彩评论