Java Generics Error: inconvertible types from command line compiler

扶醉桌前 提交于 2019-12-04 02:38:57

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.)

I know the answer is working and accepted but I believe the downcast is not the perfect solution. Also my code cleanup remove the obsolete down cast...

1) the reason eclipse and command line does not yield the same problem is because of eclipse settings. Go to preferences - java - compiler - Errors/Warnings and set Generic types (unchecked generic type operation) to warning. Then you will detect the same problem if you remove the @SuppressWarnings("unchecked")

2) I had a similar issue and a friend show me another solution. To fix the code properly (without the down cast) just change the CategoryDataProvider.class to this:

new CategoryDataProvider<Row<DatabaseItem>>().getClass()

Then put back the @SuppressWarnings("unchecked")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!