`

通过反射, 获得Class定义中声明的父类的泛型参数的类型

阅读更多
/**
	 * 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
	 * 如无法找到, 返回Object.class.
	 * 
	 * 如public UserDao extends HibernateDao<User,Long>
	 *
	 * @param clazz clazz The class to introspect
	 * @param index the Index of the generic ddeclaration,start from 0.
	 * @return the index generic declaration, or Object.class if cannot be determined
	 */
	
	public static Class getSuperClassGenricType(final Class clazz, final int index) {

		Type genType = clazz.getGenericSuperclass();

		if (!(genType instanceof ParameterizedType)) {
			logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
			return Object.class;
		}

		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

		if (index >= params.length || index < 0) {
			logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
					+ params.length);
			return Object.class;
		}
		if (!(params[index] instanceof Class)) {
			logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
			return Object.class;
		}

		return (Class) params[index];
	}

 

分享到:
评论
4 楼 snowing11812 2010-10-25  
但这个似乎连注释都和springside的一模一样....
3 楼 maoweiwer 2010-10-23  
zhuyingxi 写道
怎么把springside的工具类都copy过来了

笑话,这样的工具类都写得差不多,就一定是springside出的吗。
2 楼 rere43 2010-10-22  
JAVA反射免费资源下载:
http://www.51px.asia/px/jiangtang.aspx
1 楼 zhuyingxi 2010-10-22  
怎么把springside的工具类都copy过来了

相关推荐

Global site tag (gtag.js) - Google Analytics