我正在使用 java 版本 1.8.0_121。我在 Eclipse 4.7.0 中出现的问题,但在 Eclipse 4.6.3 中没有。这个问题更多的是要弄清楚为什么会存在差异以及是否确实存在错误,而不是修复代码。从 gradle 编译时没有错误。
我有以下界面:
public interface IPredicateFactory
{
public <P extends IPredicate<T>, T> IPredicate<T> createPredicateForClass(
final Class<P> predicateClass);
}
而这段调用接口方法的代码:
public final class PredicateExecutor
{
private IPredicateFactory predicateFactory;
// some more code...
@SuppressWarnings("unchecked")
private final <T> IPredicate<T> createPredicate(
final Class<? extends IPredicate<?>> predicateClass)
{
assert predicateFactory != null : "Unspecified predicate factory";
final IPredicate<?> predicate = predicateFactory.createPredicateForClass(predicateClass);
assert predicate != null : "Predicate factory should not return a null predicate";
return (IPredicate<T>) predicate;
}
}
Eclipse 4.7.0 给了我这个错误:
The method createPredicateForClass(Class<P>) in the type IPredicateFactory is not applicable for the arguments (Class<capture#1-of ? extends IPredicate<?>>)
我知道接口中参数的类型(Class<P>
with <P extends IPredicate<T>>
)和传递对象的类型(Class<? extends IPredicate<?>>
)的定义不同。
我可以通过强制转换或根本不使用通配符来防止错误。但我想知道的是:
- 我的代码中是否存在实际错误?如果是这样,为什么它甚至从 gradle 编译?
- 为什么这个错误只出现在 Eclipse 4.7.0 中?
- 我可以在 java/Eclipse 方面做些什么来防止它发生吗?