获取子类的泛型参数

用的时候不好找,今天看nutz的dao的源码看到了,摘出来备份

	public static Type[] getTypeParams(Class<?> klass) {
		if (klass == null || "java.lang.Object".equals(klass.getName()))
			return null;
		// 看看父类
		Type superclass = klass.getGenericSuperclass();
		if (null != superclass && superclass instanceof ParameterizedType)
			return ((ParameterizedType) superclass).getActualTypeArguments();

		// 看看接口
		Type[] interfaces = klass.getGenericInterfaces();
		for (Type inf : interfaces) {
			if (inf instanceof ParameterizedType) {
				return ((ParameterizedType) inf).getActualTypeArguments();
			}
		}
		return getTypeParams(klass.getSuperclass());
	}

 只能查找有父类或者实现了接口的类,否则返回null.

ps:看来基础api还有很多不懂啊.

你可能感兴趣的:(DAO)