Cannot convert from not generic subclass to generic superclass
I'm trying to get interface instance depending on what type T is. Place or
something else that extends BaseDictionary.
public static <T extends BaseDictionary> IDictionaryDataSource<T>
getEntityDataSourceInstance(Class<T> clazz,
Context cxt) {
if (Place.class.isAssignableFrom(clazz)) return
(IDictionaryDataSource<T>) new PlaceDataSource(cxt);
//here some other types, same lines
return null;
}
public abstract class BaseDictionary{}
public class Place extends BaseDictionary{}
public interface IDictionaryDataSource<T extends BaseDictionary>{}
public abstract class BaseDictionaryDataSource<T extends BaseDictionary>
implements IDictionaryDataSource<T>{}
public class PlaceDataSource extends BaseDictionaryDataSource<Place>{}
And I get
Type mismatch: cannot convert from PlaceDataSource to
IDictionaryDataSource<T>
or
Type safety: Unchecked cast from PlaceDataSource to IDictionaryDataSource<T>
if I cast it like above.
Can you explain why do compile error and warning occur?
It will be called here
public static <T extends BaseDictionary>
DictionaryElementPickerFragment<T> newInstance(Class<T> clazz, Context
cxt){
//somecode here
fragment.setDataSource(DictUtils.getEntityDataSourceInstance(clazz,
cxt));
}
I've tried to find answer here and in google but no success.I would
appreciate any help. Now I think like this
There is no helper method to work around the problem, because the code is
fundamentally wrong.
Thanks in advance.
No comments:
Post a Comment