修复 lambda格式接口获取不到泛型类型 问题

This commit is contained in:
octopusYan 2022-04-09 14:04:32 +08:00
parent 13fa56b76f
commit 269c1c1299
10 changed files with 160 additions and 50 deletions

View File

@ -226,6 +226,11 @@ public class LoginController extends BaseController<StackPane> implements Initia
}
});
}
@Override
public void onFail(Exception e) {
}
});
}
@ -240,6 +245,11 @@ public class LoginController extends BaseController<StackPane> implements Initia
public void onSucceed(String result) {
ProxyUtil.setCsrf(result);
}
@Override
public void onFail(Exception e) {
}
});
}

View File

@ -27,6 +27,7 @@ import top.octopusyan.config.ProxyConfig;
import top.octopusyan.config.ProxyConfig.ProxyServer;
import top.octopusyan.config.ProxyConfig.ProxyType;
import top.octopusyan.http.request.ProxySetup;
import top.octopusyan.manager.http.listener.OnHttpListener;
import top.octopusyan.model.ProxySetupModel;
import top.octopusyan.utils.AlertUtil;
import top.octopusyan.utils.DomainUtil;
@ -147,21 +148,29 @@ public class MainController extends BaseController<StackPane> implements Initial
Arrays.asList(ProxyServer.values()).forEach((server) -> proxyServerComboBox.getItems().add(server.getServerName()));
// 获取用户隧道列表
ProxyUtil.getList(setupList -> {
ProxyUtil.getList(new OnHttpListener<List<ProxySetup>>() {
@Override
public void onSucceed(List<ProxySetup> result) {
if (setupList != null)
proxyList = setupList;
if (result != null)
proxyList = result;
// 如果用户隧道列表不为空
if (proxyList != null && proxyList.size() > 0) {
// 显示隧道列表
initProxyList(proxyList);
// 默认选中第一个(配置隧道设置模型)
proxyListView.getSelectionModel().select(0);
} else {
// 配置隧道设置模型
proxyList = new ArrayList<>();
proxyList.add(proxySetup);
}
}
@Override
public void onFail(Exception e) {
// 如果用户隧道列表不为空
if (proxyList != null && proxyList.size() > 0) {
// 显示隧道列表
initProxyList(proxyList);
// 默认选中第一个(配置隧道设置模型)
proxyListView.getSelectionModel().select(0);
} else {
// 配置隧道设置模型
proxyList = new ArrayList<>();
proxyList.add(proxySetup);
}
});
@ -484,9 +493,17 @@ public class MainController extends BaseController<StackPane> implements Initial
ProxyUtil.delete(proxySetupModel.getId());
// 添加隧道
ProxyUtil.add(result -> {
proxySetup = result;
proxySetupModel.set(proxySetup);
ProxyUtil.add(new OnHttpListener<ProxySetup>() {
@Override
public void onSucceed(ProxySetup result) {
proxySetup = result;
proxySetupModel.set(proxySetup);
}
@Override
public void onFail(Exception e) {
}
}, proxySetupModel.get());
}

View File

@ -148,6 +148,11 @@ public class RegisterController extends BaseController<StackPane> implements Ser
public void onSucceed(String result) {
Platform.runLater(() -> AlertUtil.info("系统已发送一封邮件至您的邮箱,请查收。").show());
}
@Override
public void onFail(Exception e) {
}
});
}
});
@ -167,25 +172,34 @@ public class RegisterController extends BaseController<StackPane> implements Ser
ApplicatonStore.getEmail(),
emailCheckCodeTf.getText()
))
.request(result -> {
// 注册出错
if (JsoupUtil.isAlertSuccess(result)) {
// 注册成功
Platform.runLater(() ->
AlertUtil.info(JsoupUtil.getHtmlMessage(result)).header(null).show()
);
ApplicatonStore.setRegisterSuccess(true);
Platform.runLater(() -> {
try {
jumpTo(new LoginController());
} catch (IOException e) {
e.printStackTrace();
}
});
} else
Platform.runLater(() ->
AlertUtil.error(JsoupUtil.getErrorMessage(result)).header(null).show()
);
.request(new OnHttpListener<String>() {
@Override
public void onSucceed(String result) {
// 注册出错
if (JsoupUtil.isAlertSuccess(result)) {
// 注册成功
Platform.runLater(() ->
AlertUtil.info(JsoupUtil.getHtmlMessage(result)).header(null).show()
);
ApplicatonStore.setRegisterSuccess(true);
Platform.runLater(() -> {
try {
jumpTo(new LoginController());
} catch (IOException e) {
e.printStackTrace();
}
});
} else
Platform.runLater(() ->
AlertUtil.error(JsoupUtil.getErrorMessage(result)).header(null).show()
);
}
@Override
public void onFail(Exception e) {
}
});
}
}

View File

@ -43,6 +43,11 @@ public class Demo {
public void onSucceed(String result) {
}
@Override
public void onFail(Exception e) {
}
});
try {

View File

@ -195,7 +195,7 @@ public class EasyHttp<Param, Result> {
try {
mCallProxy = new CallProxy(createCall());
Response response = mCallProxy.execute();
return (Result) mHandler.requestSucceed(getRequestApi(), response, EasyUtils.getReflectType(responseClass));
return (Result) mHandler.requestSucceed(getRequestApi(), response, mHandler.getType(responseClass));
} catch (Exception e) {
throw mHandler.requestFail(getRequestApi(), e);
}

View File

@ -244,6 +244,7 @@ public class EasyUtils {
if (object == null) {
return Void.class;
}
Type[] types = object.getClass().getGenericInterfaces();
if (types.length > 0) {
// 如果这个监听对象是直接实现了接口
@ -253,6 +254,35 @@ public class EasyUtils {
// 如果这个监听对象是通过类继承
return ((ParameterizedType) object.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
* 获取对象上面的泛型
*/
public static Type getGenericType(Object object) {
if (object == null) {
return Void.class;
}
// 获取接口上面的泛型
Type[] types = object.getClass().getGenericInterfaces();
if (types.length > 0) {
// 如果这个对象是直接实现了接口并且携带了泛型
return ((ParameterizedType) types[0]).getActualTypeArguments()[0];
}
// 获取父类上面的泛型
Type genericSuperclass = object.getClass().getGenericSuperclass();
if (!(genericSuperclass instanceof ParameterizedType)) {
return Void.class;
}
Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
if (actualTypeArguments.length == 0) {
return Void.class;
}
// 如果这个对象是通过类继承并且携带了泛型
return actualTypeArguments[0];
}
/**

View File

@ -40,7 +40,7 @@ public final class NormalCallback extends BaseCallback {
protected void onResponse(Response response) throws Exception {
// 打印请求耗时时间
HttpLog.print("RequestConsuming" + (response.receivedResponseAtMillis() - response.sentRequestAtMillis()) + " ms");
final Object result = mRequestHandler.requestSucceed(mRequestApi, response, EasyUtils.getReflectType(mListener));
final Object result = mRequestHandler.requestSucceed(mRequestApi, response, mRequestHandler.getType(mListener));
EasyUtils.post(() -> {
if (mListener == null) {
return;

View File

@ -1,11 +1,10 @@
package top.octopusyan.manager.http.listener;
import javafx.application.Platform;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import top.octopusyan.utils.AlertUtil;
/**
* 使用时不能为lambda 否则获取不到泛型类型
*
* @author : octopus yan
* @email : octopus_yan@foxmail.com
* @description : 网络请求监听
@ -26,8 +25,7 @@ public interface OnHttpListener<T> {
/**
* 请求出错
*/
default void onFail(Exception e) {
}
void onFail(Exception e);
/**
* 请求结束

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Type;
import okhttp3.Request;
import okhttp3.Response;
import top.octopusyan.manager.http.EasyUtils;
import top.octopusyan.manager.http.api.RequestApi;
public interface IRequestHandler {
@ -36,4 +37,11 @@ public interface IRequestHandler {
* @return 错误对象
*/
<Param, Result> Exception requestFail(RequestApi<Param, Result> api, Exception e);
/**
* 解析泛型
*/
default Type getType(Object object) {
return EasyUtils.getGenericType(object);
}
}

View File

@ -55,7 +55,16 @@ public class ProxyUtil {
EasyHttp.builder()
.api(Api.DeleteProxy())
.pathParam(String.valueOf(id), csrf)
.request(result -> {
.request(new OnHttpListener<Void>() {
@Override
public void onSucceed(Void result) {
}
@Override
public void onFail(Exception e) {
}
});
}
@ -155,15 +164,34 @@ public class ProxyUtil {
EasyHttp.builder()
.api(Api.AddProxy())
.param(setup)
.request(result -> getList(result1 -> {
if (result1 != null && result1.size() > 0) {
for (ProxySetup proxySetup : result1) {
if (Objects.equals(proxySetup.getSort(), proxySetup.getSort())) {
listener.onSucceed(proxySetup);
.request(new OnHttpListener<String>() {
@Override
public void onSucceed(String result) {
getList(new OnHttpListener<List<ProxySetup>>() {
@Override
public void onSucceed(List<ProxySetup> result) {
if (result != null && result.size() > 0) {
for (ProxySetup proxySetup : result) {
if (Objects.equals(proxySetup.getSort(), proxySetup.getSort())) {
listener.onSucceed(proxySetup);
}
}
}
}
}
@Override
public void onFail(Exception e) {
}
});
}
}));
@Override
public void onFail(Exception e) {
}
});
}
public static String getCsrf() {