Java泛型错误:来自命令行编译器的不可转换类型 [英] Java Generics Error: inconvertible types from command line compiler

查看:1074
本文介绍了Java泛型错误:来自命令行编译器的不可转换类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Guice绑定代码使用泛型,从Eclipse的编译器编译和运行良好,但不是来自Java(命令行)编译器。我升级到最新的(1.7.0_01)Java SDK,但仍然会收到以下错误。

  [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> 。这仅仅是因为未经检查的转换(§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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆