java 不可转换的类型,Java泛型错误:来自命令行编译器的不可转换类型

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>>) CategoryDataProvider.class);

[error] ^

[error] required: Class extends ListAdapterDataProvider>>

[error] found: Class

[error] 1 error

[error] {file:/.../compile:compile: javac returned nonzero exit code

Relevant code:

public interface Category extends DatabaseItem {}

public class CategoryDataProvider implements

ListAdapterDataProvider> {}

public class BindListViewHandlerWithSpecificProvider extends AbstractModule {

public BindListViewHandlerWithSpecificProvider(

Class extends ListAdapterDataProvider>>

dataProviderClass) {}

}

@SuppressWarnings("unchecked")

// Error happens here:

final BindListViewHandlerWithSpecificProvider

bindListViewHandlerWithSpecificProvider =

new BindListViewHandlerWithSpecificProvider(

(Class extends ListAdapterDataProvider>>)

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 being a raw type. While a parameterized type C is the subtype of the raw type C (§4.10.2), the inverse is not true: C is not a subtype of C. 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>. 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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值