67,513
社区成员
发帖
与我相关
我的任务
分享
package com.wu.passport.sdk.auth;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Test {
public interface HttpHandle<T> {
}
public class Http {
}
public static void main(String[] args) {
HttpHandle<Http> handle = new HttpHandle<Http>() {
};
debug(handle);
}
static void debug(HttpHandle<?> handle) {
Class<?> clazz = getClassType(handle, 0,0);
System.out.println(clazz.getName());
}
private static Class<?> getClassType(HttpHandle<?> handle, int pos1,int pos2) {
Type[] types = handle.getClass().getGenericInterfaces();
if(types == null || types.length <= pos1){
return null;
}
Type type = types[pos1];
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] genericTypes = parameterizedType.getActualTypeArguments();
if (genericTypes.length >= pos2 + 1) {
return (Class<?>) genericTypes[pos2];
}
}
return null;
}
}
/**
* 获取泛型Class信息
*
* @return
*/
private Class<?> getClassType(int pos) {
Type type = this.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] genericTypes = parameterizedType.getActualTypeArguments();
if (genericTypes.length >= pos + 1) {
return (Class<?>) genericTypes[pos];
}
}
return null;
}