Java泛型错误:来自命令行编译器的不可转换类型 [英] Java Generics Error: inconvertible types from command line compiler
问题描述
[error] ... \\ 42:错误:不可转换类型
[错误](类<?extends ListAdapterDataProvider< Row extends扩展DatabaseItem>>)CategoryDataProvider.class)
[error] ^
[error] required:Class<?扩展ListAdapterDataProvider< Row<?扩展DatabaseItem>>>>
[error] found:Class< CategoryDataProvider>
[错误] 1错误
[错误] {文件:/.../编译:编译:javac返回非零退出代码
相关代码:
public interface类别扩展DatabaseItem {}
public class CategoryDataProvider implements
ListAdapterDataProvider< Row< Category>> {}
public class BindListViewHandlerWithSpecificProvider extends AbstractModule {
public BindListViewHandlerWithSpecificProvider(
Class<?extends ListAdapterDataProvider<?extends DatabaseItem>>
dataProviderClass){}
@SuppressWarnings(unchecked)
//发生错误:
final BindListViewHandlerWithSpecificProvider
bindListViewHandlerWithSpecificProvider =
new BindListViewHandlerWithSpecificProvider(
(Class<?extends ListAdapterDataProvider< Row<?extends DatabaseItem>>>)
CategoryDataProvider.class);
自己动手做一个upcast downcast:
Class< ...> foo =(Class< ...>)(Object)MyClass.class;
问题是 CDP.class
类型为 Class< CDP>
, CDP
为原始类型。而参数化类型 C< T1,...,Tn>
是原始类型的子类型 C
(§否则不成立: C
不是 C< T1,...,Tn> $ c的子类型$ C>。这仅仅是因为未经检查的转换(§5.1.9)才是真实的。这导致您的问题:您希望
CDP
扩展(如上限><?extends ...>
) LADP< Row <扩展DI>>
。不是这样,因为类型参数包含(§4.5.1.1)是在子类型上定义的,不考虑未经检查的转换。
(或者切换到追逐:javac已经得到了这个权利。)
I have some Guice binding code using generics that compiles and functions fine from Eclipse's compiler, but not from the Java (command-line) compiler. I upgraded to the latest (1.7.0_01) Java SDK but still get the following error.
[error] ...\BindCategorySelectorActivity.java:42: error: inconvertible types
[error] (Class<? extends ListAdapterDataProvider<Row<? extends DatabaseItem>>>) CategoryDataProvider.class);
[error] ^
[error] required: Class<? extends ListAdapterDataProvider<Row<? extends DatabaseItem>>>
[error] found: Class<CategoryDataProvider>
[error] 1 error
[error] {file:/.../compile:compile: javac returned nonzero exit code
Relevant code:
public interface Category extends DatabaseItem {}
public class CategoryDataProvider implements
ListAdapterDataProvider<Row<Category>> {}
public class BindListViewHandlerWithSpecificProvider extends AbstractModule {
public BindListViewHandlerWithSpecificProvider(
Class<? extends ListAdapterDataProvider<Row<? extends DatabaseItem>>>
dataProviderClass) {}
}
@SuppressWarnings("unchecked")
// Error happens here:
final BindListViewHandlerWithSpecificProvider
bindListViewHandlerWithSpecificProvider =
new BindListViewHandlerWithSpecificProvider(
(Class<? extends ListAdapterDataProvider<Row<? extends DatabaseItem>>>)
CategoryDataProvider.class);
Do yourself a favor and do an upcast followed by a downcast:
Class<...> foo = (Class<...>)(Object)MyClass.class;
The issue is that CDP.class
is of type Class<CDP>
, CDP
being a raw type. While a parameterized type C<T1,...,Tn>
is the subtype of the raw type C
(§4.10.2), the inverse is not true: C
is not a subtype of C<T1,...,Tn>
. This only appears to be true due to unchecked conversion (§5.1.9). This is causing your issue: You expect CDP
to "extend" (as in the upper bound of Class<? extends ...>
) LADP<Row<? extends DI>>
. This is not the case because type argument containment (§4.5.1.1) is defined over subtyping and does not consider unchecked conversion.
(Or to cut to the chase: javac has got this one right.)
这篇关于Java泛型错误:来自命令行编译器的不可转换类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!