现在的位置: 首页 > 综合 > 正文

getGenericClass

2012年04月29日 ⁄ 综合 ⁄ 共 633字 ⁄ 字号 评论关闭
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericsUtils {
/**
* Locates the first generic declaration on a class.
*
* @param clazz The class to introspect
* @return the first generic declaration, or <code>null</code> if cannot be determined
*/
public static Class<?> getGenericClass(Class<?> clazz) {
return getGenericClass(clazz, 0);
}

public static Class<?> getGenericClass(Class<?> clazz, int index) {
Type genType = clazz.getGenericSuperclass();

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

if ((params != null) && (params.length >= (index - 1))) {
return (Class<?>) params[index];
}
}
return null;
}
}

抱歉!评论已关闭.