我的应用程序正在抛出
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
对于此代码:
public class DsMap<K,V> implements Map<K, V>{
protected Class<K> kClazz;
protected Class<V> vClazz;
// Called during DS.getMap(mapName) method
public DsMap(String name) {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
Type typeK = genericSuperclass.getActualTypeArguments()[0];
Type typeV = genericSuperclass.getActualTypeArguments()[1];
if (typeK instanceof Class) {
this.kClazz = (Class<K>) typeK;
} else if (typeK instanceof ParameterizedType) {
this.kClazz = (Class<K>) ((ParameterizedType)typeK).getRawType();
}
if (typeV instanceof Class) {
this.vClazz = (Class<V>) typeV;
} else if (typeV instanceof ParameterizedType) {
this.vClazz = (Class<V>) ((ParameterizedType)typeV).getRawType();
}
}
}
我想要实现的是在运行时获取K
和的类名:V
这样做:
Map<String,String> dsMap = DS.getMap(mapName);
我将获取andjava.lang.String
的类名,或者获取任何参数化的类名(不实例化and )kClazz
vClazz
Class<K>
Class<V>
我的代码可能有什么问题?