import android.util.Log;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
// 如需强制调用者实现数据回调的方法,可做成abstract
public class ModuleApiCallback<T> implements ApiCallback<T> {
protected Type type;
public SearchApiCallback() {
this.type = ClassTypeReflect.getModelClazz(getClass());
}
@Override
public void onErrorResponse(VolleyError volleyError) {
}
@Override
public void onResponse(T t) {
}
}
*/
class ClassTypeReflect {
static Type getModelClazz(Class<?> subclass) {
return getGenericType(0, subclass);
}
private static Type getGenericType(int index, Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if (!(superclass instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) superclass).getActualTypeArguments();
if (index >= params.length || index < 0) {
throw new RuntimeException("Index outof bounds");
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return params[index];
}
}