JAVA泛型应用,获取容器运行时的泛型Class对象。
private Class<Input[]> _inputArrType = null;
/**
* 获取容器运行时的泛型Class对象。
* @return
* @time 2013-8-20 下午1:41:31
*/
@SuppressWarnings("unchecked")
private Class<Input[]> getInputCls() {
if(_inputArrType == null) {
ParameterizedType t = (ParameterizedType) getClass().getGenericSuperclass();
Class<Input> c = (Class<Input>) t.getActualTypeArguments()[0];
_inputArrType = (Class<Input[]>) Array.newInstance(c, 0).getClass();
}
return _inputArrType;
}

本文介绍了一种在Java中通过泛型获取特定类型Class对象的方法。利用反射机制,该方法能够在运行时动态获取容器内存储元素的具体类型。
2397

被折叠的 条评论
为什么被折叠?



